繁体   English   中英

我已经创建了基本的GUI,但是按钮不会与文本字段交互

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

我一直在通过使用JPanels创建一个简单的GUI。 我设法使布局看起来合理,现在,我希望用户能够在“质量”文本框和“加速度”文本框中输入值,以便当他们单击计算时,会得到一条消息,告诉他们力。

我遇到的问题是在按钮上添加的公共空白内,我似乎无法弄清楚如何在文本字段中引用值。 我试图通过以下方式来一般地引用它:

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

但是,这不能将txt识别为现有文本吗?

任何帮助,将不胜感激。

下面是完整的代码,以防万一。

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();
            }
        });
    }

}

首先,放弃对static的依赖,而是创建一个InitScreen实例,并将其称为createHomeScreen方法。

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();
            }
        });
    }

现在,让txts和实例字段InitScreen和你的方法中使用它

public class InitScreen {

    private JTextField[] txts;

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

txts从本地上下文转移到类实例上下文,这意味着您现在可以从InitScreen任何方法访问它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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