java - 如何从 JTextField 中获取值并将其移至 Int 变量中

标签 java swing jframe jtextfield parseint

我一直在尝试创建一个 JFrame 程序,它需要两个数字和一个 操作(jcombobox 内部)来计算答案。我需要获取数字 1 和 2 的用户输入,并将该值分配给可用于计算答案的 int。 num1 是 int 变量,num1field 是文本字段的名称。

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

是的,num1 int 已经在类的顶部声明了。我在 setText 处收到错误。

感谢您的帮助:)

最佳答案

没有方法JTextField#setText(int),您只能提供String

num1field.setText(String.valueOf(num1));

应该可以工作

您可能想看看How to use Formatted Text FieldsHow to use Spinners这可能会为您提供更好的功能来实现您想要实现的目标

更新了如何计算结果值的示例

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

关于java - 如何从 JTextField 中获取值并将其移至 Int 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18347195/

相关文章:

java - 如何在泛型的使用中允许更精确的返回类型?

java - Java 中有 JFrame 的有效替代品吗?

java - 在根容器中分层多个 GlassPane

java - 路径遍历、文件上传利用

java - Java 对象的生命周期

java - JOptiopane 多选

java - 我如何实现新的 JFrame 功能

java - 使用 JFrame 时出现 NumberFormatException

java - 为什么Gson fromJson会抛出JsonSyntaxException : Expected BEGIN_OBJECT but was BEGIN_ARRAY?

java - 删除按钮的矩形边框