1

我将所有这些都放在一个文件中。我试图从文本字段 jtfNumber1 和 jtfNumber2 中获取 int,然后根据将哪个按钮按下到 jtfResult 中来显示操作的结果。我有按钮工作。他们目前显示一条测试消息。我坚持从文本字段中获取字符串。

public class Calculator extends JFrame {

    // Create Buttons and fields
    private JButton jbtAdd = new JButton("Add");
    private JButton jbtSub = new JButton("Subtract");
    private JButton jbtMul = new JButton("Multiply");
    private JButton jbtDiv = new JButton("Divide");
    private JTextField jtfNumber1 = new JTextField(8);
    private JTextField jtfNumber2 = new JTextField(8);
    private JTextField jtfResponse = new JTextField(8);

    public Calculator() {
        //Create Panel
        JPanel panel = new JPanel();
        panel.add(new JLabel("Number 1"));
        panel.add(jtfNumber1);
        panel.add(new JLabel("Number 2"));
        panel.add(jtfNumber2);
        panel.add(new JLabel("Result"));
        panel.add(jtfResponse);
        panel.add(jbtAdd);
        panel.add(jbtSub);
        panel.add(jbtMul);
        panel.add(jbtDiv);
        add(panel);
        BtnListener listener = new BtnListener();
        jbtAdd.addActionListener(listener);
        jbtSub.addActionListener(listener);
        jbtMul.addActionListener(listener);
        jbtDiv.addActionListener(listener);
    }

    class JtfListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int x = Intenger.parseInt(jtfNumber1.getText());
        }
    }

    class BtnListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jbtAdd) {
                System.out.println("Test Test Test");
            } else if (e.getSource() == jbtSub) {
                System.out.println("Test Test Test");
            } else if (e.getSource() == jbtMul) {
                System.out.println("Test Test Test");
            } else if (e.getSource() == jbtDiv) {
                System.out.println("Test Test Test");
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new Calculator();
        frame.setTitle("Calculator");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
4

3 回答 3

3

你几乎完成了,actionPerformed方法上的一些变化

public void actionPerformed(ActionEvent e) {
    int int1 = Integer.parseInt(jtfNumber1.getText());
        int int2 = Integer.parseInt(jtfNumber2.getText());
    if (e.getSource() == jbtAdd) {
        jtfResponse.setText(String.valueOf(int1+int2));
    } else if (e.getSource() == jbtSub) {
        jtfResponse.setText(String.valueOf(int1-int2));
    } else if (e.getSource() == jbtMul) {
        jtfResponse.setText(String.valueOf(int1*int2));
    } else if (e.getSource() == jbtDiv) {
        jtfResponse.setText(String.valueOf(int1/int2));
    }
}

您可以直接实施ActionListner到您的班级

public class Calculator extends JFrame implements ActionListener

和覆盖actionPerformed方法。你的主要方法也应该像

public static void main(String[] args) {
    //AWT event dispatching thread
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new Calculator();
            frame.setTitle("Calculator");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    });
}

请参阅为什么需要 SwingUtilities 调用程序

于 2013-07-19T13:14:12.557 回答
1

在您的 actionlistener 中,您需要获取文本并将其转换为 Integer

例子。

if (e.getSource() == jbtAdd) {
            int first = 0;
            int second = 0;
            if (jtfNumber1.getText() != null && !jtfNumber1.getText().equals("")) {
                first = Integer.parseInt(jtfNumber1.getText());
            }
            if (jtfNumber2.getText() != null && !jtfNumber2.getText().equals("")) {
                second = Integer.parseInt(jtfNumber2.getText());
            }
            int result = first + second;
            jtfResponse.setText("" + result);

            System.out.println("Test Test Test");
        } 

其余输入反之亦然。

于 2013-07-19T13:13:20.763 回答
1

你可以简单地使用

Integer.parseInt(jtfNumber1.getText());

如果要将文本字段的值用作整数。

也许这可以帮助你:How to Retrieve value from JTextField in Java Swing?

于 2013-07-19T13:11:06.150 回答