我创建了一个基本的GUI,但按钮不会与textfield

时间:2015-09-08 03:12:44

标签: java swing button jpanel

我一直致力于通过使用JPanel创建一个简单的GUI。我已经设法达到了我认为具有合理外观的布局,现在我希望用户能够将值输入到Mass文本框和加速文本框中,这样当他们点击计算时会给他们一条消息,告诉他们Force

我遇到的问题是在添加到按钮的 public void 中,我似乎无法弄清楚如何在文本字段中引用值。我试图通过以下方式来引用它:

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

然而,这并没有将txts识别为现有的?

对此的任何帮助都将非常感激。

以下是有用的完整代码。

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

}

1 个答案:

答案 0 :(得分:1)

首先放弃对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();
            }
        });
    }

现在,制作txtsInitScreen的实例字段,并在您的方法中使用

public class InitScreen {

    private JTextField[] txts;

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

这会将txts从本地上下文转移到类实例上下文,这意味着您现在可以从InitScreen

中的任何方法访问它