java 如何使用 JButton 从 JTextField 获取 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17914979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 19:35:41  来源:igfitidea点击:

how to get int from JTextField with a JButton

javaswingjbuttonjtextfield

提问by ChildProdigy

I'm trying to get an int from my JTextField with the click of my JButton but I can't figure out how to do so. I'm trying to get the int and set it to a variable so I can use it in my program further down.

我试图通过单击我的 JButton 从我的 JTextField 获取一个 int,但我不知道如何做到这一点。我正在尝试获取 int 并将其设置为一个变量,以便我可以在我的程序中进一步使用它。

Here is the code(this is the whole method):

这是代码(这是整个方法):

    JFrame presets = new JFrame("Presets");
    presets.setVisible(true);
    presets.setSize(500, 500);

    JPanel gui = new JPanel(new BorderLayout(2,2));

    JPanel labelFields = new JPanel(new BorderLayout(2,2));
    labelFields.setBorder(new TitledBorder("Presets"));

    JPanel labels = new JPanel(new GridLayout(0,1,1,1));

    JPanel fields = new JPanel(new GridLayout(0,1,1,1));

    labels.add(new JLabel("Place values on Cat.2/Cat.3 at"));
    JTextField f1 = new JTextField(10);
    String text = f1.getText();
    int first = Integer.parseInt(text);
    labels.add(new JLabel("and place follow up value at"));
    fields.add(new JTextField(10));


    labelFields.add(labels, BorderLayout.CENTER);
    labelFields.add(fields, BorderLayout.EAST);

    JPanel guiCenter = new JPanel(new BorderLayout(2,2));

    JPanel submit = new JPanel(new FlowLayout(FlowLayout.CENTER));

    submit.add( new JButton("Submit") );
    guiCenter.add( submit, BorderLayout.NORTH );

    gui.add(labelFields, BorderLayout.NORTH);
    gui.add(guiCenter, BorderLayout.CENTER);

    JOptionPane.showMessageDialog(null, gui);

回答by newuser

Try this,

试试这个,

      String getText()

      Returns the text contained in this TextComponent.

So, convert your String to Integer as:

因此,将您的 String 转换为 Integer 为:

try {
    int integerValue = Integer.parseInt(jTextField.getText());
}
catch(NumberFormatException ex)
{
    System.out.println("Exception : "+ex);
}

回答by Paniz

if you want to get the f1 text when the submit pressed, use this code:

如果您想在按下提交时获取 f1 文本,请使用以下代码:

   .
   .
   .
    JPanel submit = new JPanel(new FlowLayout(FlowLayout.CENTER));
    JButton button = new JButton("Submit");
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            int first = Integer.parseInt(f1.getText().trim());
        }
    });
    submit.add(button);
    guiCenter.add(submit, BorderLayout.NORTH);
      .
      .
      .

回答by a question

Probably you want the entered data as int. write it in the button action

可能您希望输入的数据为int. 把它写在按钮动作中

JButton button = new JButton("Submit");
button.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent arg0) {
    try {
      int myInt=Integer.parseInt(jtextfield.getText());
      System.out.println("Integer is: "+myInt);
      //do some stuff
    }
    catch (NumberFormatException ex) {
      System.out.println("Not a number");
      //do you want
   }
  }
});

Remember Integer.parseIntthrows NumberFormatExceptionwhich must be caught. see java docs

记住必须抓住的Integer.parseInt投掷NumberFormatException。请参阅Java 文档