单击按钮后需要读取两个JTextfields的输入 [英] Need to read input of two JTextfields after a button is clicked

查看:89
本文介绍了单击按钮后需要读取两个JTextfields的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,该程序将启动一个applet,该applet将演示Extended-Euclid算法的性能.我有两个JTextField,需要输入值并需要将其读取/转换为int等.我在实际读取输入值时遇到了麻烦.

I'm working on a program that launches an applet that will demonstrate how the Extended-Euclid algorithm performs. I have two JTextFields that values will be entered in and need to be read/converted to ints, etc. I'm having trouble actually reading the input values to start with.

更新的问题:
阅读第一个答案后,我对代码进行了更改.按照建议,我没有使用DocumentListener来执行actionPerformed方法中的所有操作,但是当我尝试进行测试时,仍然出现错误.

Updated question:
I've made changes to my code after reading the first answer. Instead of using a DocumentListener I'm doing everything in my actionPerformed method as suggested but when I try testing I'm still getting errors.

这是我目前的动作

public void actionPerformed(ActionEvent event) {
    System.out.println(event.getActionCommand());
    String quotient = "";
    nText = nField.getText();
    mText = mField.getText();

    if("Find GCD".equals(event.getActionCommand())){
        int nInt = Integer.parseInt(nText);
        int mInt = Integer.parseInt(mText);
        int q = mInt/nInt;
        quotient = (Integer.toString(q));
    }
    else quotient = "n/a";
    //NOT the gcd, just to see if this will display
    gcd.setText(quotient);
    gcd.setEditable(false);
}

(注意:gcd是另一个JTextField,但只需要显示结果即可)

(note: gcd is another JTextField, but just needs to display the outcome)

现在,单击我的按钮后,我正在控制台中打印出以下错误:

Now after clicking my button, I'm getting these errors printed out in the console:

线程"AWT-EventQueue-1"中的异常java.lang.NullPointerException 在EuclidApplet.actionPerformed(EuclidApplet.java:87) 在javax.swing.AbstractButton.fireActionPerformed(未知来源) 在javax.swing.AbstractButton $ Handler.actionPerformed(未知来源) 在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源) 在javax.swing.DefaultButtonModel.setPressed(未知来源) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源) 在java.awt.Component.processMouseEvent(未知来源) 在javax.swing.JComponent.processMouseEvent(未知来源) 在java.awt.Component.processEvent(未知来源) 在java.awt.Container.processEvent(未知来源) 在java.awt.Component.dispatchEventImpl(未知来源) 在java.awt.Container.dispatchEventImpl(未知来源) 在java.awt.Component.dispatchEvent(未知来源) 在java.awt.LightweightDispatcher.retargetMouseEvent(未知来源) 在java.awt.LightweightDispatcher.processMouseEvent(未知来源) 在java.awt.LightweightDispatcher.dispatchEvent(未知来源) 在java.awt.Container.dispatchEventImpl(未知来源) 在java.awt.Window.dispatchEventImpl(未知来源) 在java.awt.Component.dispatchEvent(未知来源) 在java.awt.EventQueue.dispatchEventImpl(未知源) 在java.awt.EventQueue.access $ 000(未知源) 在java.awt.EventQueue $ 1.run(未知源) 在java.awt.EventQueue $ 1.run(未知源) 在java.security.AccessController.doPrivileged(本机方法) 在java.security.AccessControlContext $ 1.doIntersectionPrivilege(来源不明) 在java.security.AccessControlContext $ 1.doIntersectionPrivilege(来源不明) 在java.awt.EventQueue $ 2.run(未知源) 在java.awt.EventQueue $ 2.run(未知源) 在java.security.AccessController.doPrivileged(本机方法) 在java.security.AccessControlContext $ 1.doIntersectionPrivilege(来源不明) 在java.awt.EventQueue.dispatchEvent(未知来源) 在java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源) 在java.awt.EventDispatchThread.pumpEventsForFilter(未知来源) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源) 在java.awt.EventDispatchThread.pumpEvents(未知来源) 在java.awt.EventDispatchThread.pumpEvents(未知来源) 在java.awt.EventDispatchThread.run(未知来源)

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException at EuclidApplet.actionPerformed(EuclidApplet.java:87) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

我在设置的行上收到NullPointerException

I'm getting a NullPointerException at the line where I set

nText = nField.getText();

但是我仍然不确定为什么我无法获取插入到我的JTextField中的文本.我发现的每个示例都表明这应该可行,但我无法理解.谢谢.

but I'm still not sure why I'm not able to get the text that's been inserted into my JTextField. Every example I've found shows that this should work but I can't get it to. Thanks.

推荐答案

我在设置nText = nField.getText();的行上收到NullPointerException.

I'm getting a NullPointerException at the line where I set nText = nField.getText();

那可能是因为您将nField定义为类变量和局部变量.问题在于您正在尝试引用为null的类变量.

That probably because you defined nField as both a class variable and a local variable. The problem is that you are trying to reference the class variable which is null.

解决方案是摆脱类变量.

The solution is to get rid of the class variable.

这篇关于单击按钮后需要读取两个JTextfields的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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