java—如何从jtextfield获取值并将其移动到int变量中

wz1wpwve  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(442)

我一直在尝试创建一个jframe程序,它使用两个数字和一个操作(在jcombobox中)来计算答案。我需要为数字1和2获取用户输入,并将值赋给一个int,该int可用于计算答案。num1是int变量,num1field是textfield的名称。

num1field.addActionListener(
        new ActionListener(){
            public void actionPerformed(ActionEvent event){
                num1 = Integer.parseInt(num1field.getText());
                num1field.setText(num1);
            }
        }
     );

是的,num1 int已经在类的顶部声明了。我得到了一个错误,上面写着settext。
谢谢你的帮助:)

tcomlyy6

tcomlyy61#

没有办法 JTextField#setText(int) ,您只能提供 String ```
num1field.setText(String.valueOf(num1));

应该有用
您可能希望了解如何使用格式化文本字段以及如何使用微调器,这些微调器可以为您尝试实现的目标提供更好的功能
更新了如何计算结果值的示例

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class QuickCalc {

public static void main(String[] args) {
    new QuickCalc();
}

public QuickCalc() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new TestPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

public class TestPane extends JPanel {

    private JTextField numField1;
    private JTextField numField2;
    private JComboBox cbModifier;
    private JLabel lblResult;

    private JButton equals;

    public TestPane() {

        numField1 = new JTextField(4);
        numField2 = new JTextField(4);
        cbModifier = new JComboBox();
        equals = new JButton("=");
        lblResult = new JLabel("?");

        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
        model.addElement("+");
        model.addElement("-");
        model.addElement("/");
        model.addElement("x");
        cbModifier.setModel(model);

        setLayout(new GridBagLayout());
        add(numField1);
        add(cbModifier);
        add(numField2);
        add(equals);
        add(lblResult);

        equals.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    int num1 = Integer.parseInt(numField1.getText());
                    int num2 = Integer.parseInt(numField2.getText());

                    // Make your calculations here...
                    // Update the lblResult with the resulting value...
                    lblResult.setText(String.valueOf(42));                        
                } catch (NumberFormatException nfe) {
                    nfe.printStackTrace();
                    lblResult.setText("Bad numbers");
                }
            }                    
        });
    }
}

}

相关问题