gpt4 book ai didi

java - 单击按钮后需要读取两个 JTextfield 的输入

转载 作者:行者123 更新时间:2023-11-30 05:03:33 25 4
gpt4 key购买 nike

我正在开发一个程序,该程序启动一个小程序,该小程序将演示扩展欧几里得算法的执行方式。我有两个 JTextFields,将输入值并需要读取/转换为整数等。我在实际读取输入值时遇到困难。

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

这是我当前执行的操作:

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,但只需要显示结果)

现在,单击按钮后,我会在控制台中打印出这些错误:

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

nText = nField.getText();

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

最佳答案

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

这可能是因为您将 nField 定义为类变量和局部变量。问题是您试图引用为 null 的类变量。

解决方案是去掉类变量。

关于java - 单击按钮后需要读取两个 JTextfield 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5864224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com