Home > Mobile >  Am puzzled on how I would be able to pass what is given inside the textfield for size into my size i
Am puzzled on how I would be able to pass what is given inside the textfield for size into my size i

Time:03-11

package SOS;

import java.awt.Graphics;
import javax.swing.*;

public class BoardPanel extends JPanel {
    
    public static void main(String[] args) {
        JFrame frame = new JFrame("SOS");
        frame.setSize(600, 600);
        
        JLabel sizelabel = new JLabel("Board size:");
        frame.add(sizelabel);
        sizelabel.setBounds(500, 100, 30, 30);

        JTextField boardsize = new JTextField();
        boardsize.setBounds(500, 100, 30, 30);
        frame.add(boardsize);
        String value = boardsize.getText();

        BoardPanel panel = new BoardPanel();
        frame.add(panel);
        
        frame.setVisible(true);
    }
    
    private static final long serialVersionUID = 1L;
    
    static final int size = Integer.parseInt(boardsize.getText());;
    
    static final int cols = size;
    static final int rows = size;
    
    static final int originX = 73/size;
    static final int originY = 97/size;
    static final int cellSize = 300/size;
    }
}

Just having trouble figuring out how I would be able to pass String value = boardsize.getText(); to static final int size = Integer.parseInt(boardsize.getText());; I tried figuring out ways how to make it not inside the main but I got lost.

I am new to java as well so this whole scoping issue/global variable problem is new to me here.

CodePudding user response:

The code in your question does not compile because boardsize is a local variable in method main and so cannot be accessed from outside of that method. Hence the following line does not compile:

static final int size = Integer.parseInt(boardsize.getText());

Refer to this Web page, entitled Variables, from Oracle's Java tutorials.

The first Swing code examples on the Internet all used classes that extended either JPanel or JFrame. This is no longer required. Hence BoardPane does not need to extend JPanel.

There is a series of steps that you need to follow when writing any GUI application – not just a Swing application.

First you create the application window. Then you add components to the window. Then you write code that is executed when the user performs a certain action, for example when she enters text into a JTextComponent or when she clicks on a JButton. The code that gets executed in response to a user action is referred to as an event listener.

Here is a rewrite of your application. The user enters a number into the JTextField and presses the ENTER key. This will invoke method createBoard. Note that I use a method reference. I assume that you want to create some sort of board so I created a grid of JLabels.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class BordPane {
    private JFrame frame;

    private void createAndDisplayGui() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createTopPanel(), BorderLayout.PAGE_START);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void createBoard(ActionEvent event) {
        Object source = event.getSource();
        if (source instanceof JTextField) {
            JTextField textField = (JTextField) source;
            String text = textField.getText();
            int dimension = Integer.parseInt(text);
            JPanel board = new JPanel(new GridLayout(dimension, dimension));
            for (int row = 0; row < dimension; row  ) {
                for (int col = 0; col < dimension; col  ) {
                    JLabel square = new JLabel("   ");
                    square.setBackground(Color.white);
                    square.setOpaque(true);
                    square.setBorder(BorderFactory.createLineBorder(Color.black));
                    board.add(square);
                }
            }
            frame.add(board, BorderLayout.CENTER);
            frame.pack();
        }
    }

    private JPanel createTopPanel() {
        JPanel topPanel = new JPanel();
        JLabel label = new JLabel("Board size:");
        topPanel.add(label);
        JTextField boardSize = new JTextField(6);
        boardSize.addActionListener(this::createBoard);
        topPanel.add(boardSize);
        return topPanel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new BordPane().createAndDisplayGui());
    }
}

I recommend that you go through the entire Creating a GUI With Swing trail in Oracle's Java tutorials.

  • Related