简体   繁体   English

如何访问JTextField中的用户输入?

[英]How to access user input in a JTextField?

For a school project I am writing a program where I need to retrieve user input from a JTextField. 对于一个学校项目,我正在编写一个程序,需要从JTextField检索用户输入。 I've tried using the getText() method, but it is returning an empty string error. 我尝试使用getText()方法,但是它返回一个空字符串错误。

String s = input.getText();
    if(changeNum.equals("")){
        firstNum = s;
    }
    if(!changeNum.equals("")){
        secondNum = s;
    }

firstNum and secondNum are both strings. firstNum和secondNum都是字符串。 The error that is being returned is: 返回的错误是:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String

It's being returned at this line: 它在此行返回:

Double firstN = Double.parseDouble(firstNum);

What do I do? 我该怎么办? Thank you! 谢谢!

Edit: Forgot to add that input is the name of my JTextField, in case that is confusing. 编辑:忘记添加该输入是我的JTextField的名称,以防造成混淆。

Change all your varibles to double (firstNum etc.) and change your code like : 将所有变量更改为double(firstNum等),然后将代码更改为:

Note : if you not using negatIive numbers 注意: 如果您不使用负数

double s = null;
 try{
      s =  Double.parseDouble(input.getText());
   }catch(NumberF... e){e.getMesssge();}
     if(s > 0){
       if(changeNum >= 0){
               firstNum = s;
        }
         else {
                secondNum = s;
           }
 }
  //if you have to convert it to String use :

String str =String.valueOf(your double variable);

This is how I like to do it (in a general sense). 这就是我喜欢这样做的方式(一般而言)。 Let's say I have a panel with three text boxes and I want to get the value of each one (each represents a different type). 假设我有一个包含三个文本框的面板,我想获取每个文本框的值(每个文本框代表一种不同的类型)。 This is what I'd do: 这就是我要做的:

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.BoxLayout;

public class Panel extends JPanel
{
  JTextField         textBox;
  JTextField         intBox;
  JTextField         floatBox;
  JTextField         dblBox;

  public Panel()
  {
    textBox  = new JTextField("Hello");
    intBox   = new JTextField("10");
    floatBox = new JTextField("3");
    dblBox   = new JTextField("3.14");    

    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    this.add(textBox);    
    this.add(intBox);
    this.add(floatBox);
    this.add(dblBox);
  }

  public String getText()
  {
    return textBox.getText();
  }

  public int getInt()
  {
    try
    {
      return Integer.parseInt( intBox.getText() );
    }
    catch(NumberFormatException execp) {  }
    return -1;
  }

  public float getFloat()
  {
    try
    {
      return Float.parseFloat( floatBox.getText() );
    }
    catch(NumberFormatException execp) {  }
    return -2.0f;
  }

  public double getDouble()
  {
    try
    {
      return Double.parseDouble( dblBox.getText() );
    }
    catch(NumberFormatException execp) {  }
    return -2.0;
  }
}

Another option is to make Panel implement ActionListener, and dblBox.addActionListener(this) . 另一个选择是使Panel实现ActionListener和dblBox.addActionListener(this) Then you can get your JTextField to call a method from your parent whenever you change a number, parse the data, and write error messages in a JLabel if the casting throws an exception, or to private members if it succeeded. 然后,您可以让您的JTextField每次更改数字,解析数据并在JLabel中将错误消息(如果强制转换抛出异常)写入错误时,从父级调用方法,或者如果成功则将其传递给私有成员。 Then use getters to read the private members. 然后使用获取器读取私有成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM