如何从JTextfield获取文本并将其用作int

时间:2013-07-19 13:02:54

标签: java swing jtextfield calculator parseint

我将所有这些都放在一个文件中。我试图从文本字段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);
    }
}

3 个答案:

答案 0 :(得分: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);
        }
    });
}

参见 Why SwingUtilities invokelater is needed

答案 1 :(得分:1)

您只需使用

即可
Integer.parseInt(jtfNumber1.getText());

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

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

答案 2 :(得分: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");
        } 

其余输入反之亦然。