简体   繁体   中英

I've created a basic GUI but buttons won't interact with textfield

I've been working on creating a simple GUI through the use of JPanels. I've managed to what I believe fluke a reasonably looking layout and now I would like the user to be able to input values into the Mass textbox and the Acceleration textbox so when they hit calculate they are given a message telling them the Force.

The issue I am having is within the public void that is added to the buttons, I can't seem to figure out how to refer to values within the text field. I've tried to just refer to it generally by:

String mass = txts[1].getText();

However this doesn't recognise txts as existing?

Any help on this would be much appreciated.

Below is the full code in case it helps.

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.event.*;
import java.awt.*;

public class InitScreen {

    public static void createHomeScreen() {

        /*Creates a Java Frame with the Window Name = HomeScreen*/
        JFrame frame = new JFrame("HomeScreen");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);
        frame.getContentPane().setPreferredSize(new Dimension(400,300));
        frame.pack();           

        /*Creates the main JPanel in form of GridLayout*/
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(new EmptyBorder(new Insets(20,20,20,20)));

        /*Creates the first sub panel*/
        JPanel firstPanel = new JPanel();
        firstPanel.setLayout(new GridLayout(1,2,75,100));
        firstPanel.setMaximumSize(new Dimension(300,100));

        /*Creates the buttons in the first sub panel*/
        JButton[] btns = new JButton[2];

        String bText[] = {"Calculate", "Clear"};
        for (int i=0; i<2; i++) {
            btns[i] = new JButton(bText[i]);
            btns[i].setPreferredSize(new Dimension(100, 50));
            btns[i].setActionCommand(bText[i]);
            btns[i].addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    String choice = e.getActionCommand();
                    /*JOptionPane.showMessageDialog(null, "Clicked "+choice);*/
                    JOptionPane.showMessageDialog(null, "Force = ");
                }
            });
            firstPanel.add(btns[i]);

        }

        /*Creates the second sub panel*/
        JPanel secondPanel = new JPanel();
        secondPanel.setLayout(new BorderLayout());

        /*Creates the labels for the second sub panel*/
        JLabel label = new JLabel("Calculate the Force of an Object", SwingConstants.CENTER);
        secondPanel.add(label,BorderLayout.NORTH);

        /*Creates the third sub Panel for entering values*/
        JPanel thirdPanel = new JPanel();
        thirdPanel.setLayout(new GridLayout(3,1,10,10));
        thirdPanel.setMaximumSize(new Dimension(400,100));

        /*Create labels and text fields for third sub panel*/
        String lText[] = {"Mass of Body", "Acceleration of Body", "Force"};
        JLabel[] lbls = new JLabel[3];
        JTextField[] txts = new JTextField[3];
        for (int i=0; i<3; i++) {
            txts[i] = new JTextField();
            lbls[i] = new JLabel(lText[i], SwingConstants.LEFT);
            lbls[i].setPreferredSize(new Dimension(50, 50));
            thirdPanel.add(lbls[i]);
            thirdPanel.add(txts[i]);

        }

        mainPanel.add(secondPanel);
        mainPanel.add(thirdPanel);
        mainPanel.add(firstPanel);

        frame.setContentPane(mainPanel);
        frame.setVisible(true);



    }
    public static void main(final String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createHomeScreen();
            }
        });
    }

}

First drop the reliance on static , and instead, create an instance of InitScreen and call it's createHomeScreen method

public class InitScreen {

    public void createHomeScreen() {
        //...
    }

    public static void main(final String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                InitScreen screen = new InitScreen();
                screen.createHomeScreen();
            }
        });
    }

Now, make txts and instance field of InitScreen and use it within your methods

public class InitScreen {

    private JTextField[] txts;

    public void createHomeScreen() {
        //...
        txts = new JTextField[3];
        //...
    }

This takes txts from a local context to a class instance context, meaning you can now access it from any method within InitScreen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM