对于输入字符串:“”填写文本框时 [英] For input string: "" when textfields are filled out

查看:164
本文介绍了对于输入字符串:“”填写文本框时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写程序,其中有一些文本字段和一个按钮。



我得到了 java.lang.NumberFormatException:对于输入字符串: 运行程序时的文本字段。



我的代码如下:

  button.addActionListener( new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
method();
}
}
);




public void method(){
try {
String string1 = textfield1.getText();
字符串string2 = textfield2.getText();
字符串string3 = textfield3.getText();
if(string1.length()== 0 || string2.length()== 0 || string3.length()== 0){
System.out.println(必填文本字段);
的回报;
}
int号= Integer.parseInt(textfield3.getText());
//做某事
}
catch(NumberFormatException e){
System.out.println( Wrong format);
}
}

编辑:



在此处查看更多代码

解决方案

我已经对您的程序进行了一点测试,并且由于创建了面板并切换了哪个面板而使文本字段出现了问题。 / p>

在构造函数中,您将调用something()方法来创建JTextField。单击该按钮后,您再次调用something()并生成一个新的JTextField,并将其添加到面板中。



因此,您在GUI上有两个JTextField完全相同的位置,但仅引用其中一个(创建的最后一个)。



当您单击将调用method()的按钮时。向隐藏的TextField询问他的文本(这是在我的PC上的工作方式),并且始终为空,因为我只能写到看到的文本中。



一个简单的解决方法是更改​​actionPerformed方法:

  @Override 
public void actionPerformed(ActionEvent e){
if(e.getSource()== button1){
present =某些内容;
button1.setVisible(false);
// something();
visiblePanel();
先前=某物;
}

}

所以我避免了新的创建



此更改后,我可以键入 sadda,然后按一下按钮,然后看到输出 Numberformatexception 。当我输入数字时,看不到任何内容,因此无法进行格式化。


I am writing a program in Java where I got some textfields and a button.

I get a java.lang.NumberFormatException: For input string: "" even though I have filled out all the textfields when running the program.

My code looks something like this:

button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        method();
    }
}
            );




public void method() { 
    try { 
        String string1 = textfield1.getText();
        String string2 = textfield2.getText();
        String string3 = textfield3.getText();
        if ( string1.length() == 0 || string2.length() == 0 || string3.length() == 0) { 
            System.out.println("fill in the required text fields");
            return;
        } 
        int number = Integer.parseInt(textfield3.getText());
        //do something
    }
    catch ( NumberFormatException e ) { 
        System.out.println("Wrong format");
    }
}

EDIT:

See more code here

解决方案

I have tested your program a little bit and you have a problem with the text field, because of the creation of your panel and switching which one is active.

In the constructor you call the something() method which creates the JTextField. When the button is clicked you call again something() and a new JTextField is generated which you also add to the panel.

So you have two JTextFields on the GUI at the exact same position but only a reference to one of them (the last one created).

When you click the button which will call method(). The hidden TextField is asked for his text (this is how it works on my pc) and this is always empty because I can only write into the one I see.

An easy fix to this is to change the method actionPerformed:

@Override
public void actionPerformed( ActionEvent e ) {
    if ( e.getSource() == button1 ) {
        present = something;
        button1.setVisible(false);
        //something();
        visiblePanel();
        previous = something;
    }

}

So I avoid the new creation of the JTextField but visiblePanel() ensures the TextField and second button are shown.

After this change I can type in "sadda" press the button and see the output "Numberformatexception". When I type in a number I see nothing so the formatting works.

这篇关于对于输入字符串:“”填写文本框时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆