有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

单击按钮后,java需要读取两个JTextfields的输入

我正在编写一个程序,启动一个小程序,演示扩展的欧几里德算法是如何执行的。我有两个jtext字段,这些字段中的值将被输入,并且需要读取/转换为int等。我在实际读取输入值时遇到了问题

更新问题:
在阅读第一个答案后,我对代码进行了更改。我没有使用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中的文本。我发现的每一个例子都表明这应该是可行的,但我无法做到。谢谢


共 (2) 个答案

  1. # 1 楼答案

    解决方案:不要使用DocumentListener,因为这不仅过分,而且是错误的。如果您想要按钮按下时的值,那么在按钮的操作中获取值,而不是从DocumentListener获取值。如果您从按钮的动作侦听器中获取值不起作用,那么让我们找出您做错了什么,并帮助您解决该错误

    事实上,我看到您曾经尝试过这样做,但评论说:

    System.out.println(event.getActionCommand());
    System.out.println(event.getID());
    String quotient = "";
    //nText = nField.getText();  // **** here ****
    //mText = mField.getText();  // **** and here ****
    

    因此,取消对这些行的注释并去掉DocumentListener

    下面我看到的一个问题是,您正在尝试使用==运算符检查字符串是否等效:

    if("Find GCD" == event.getActionCommand()){
        int nInt = Integer.parseInt(nText);
        int mInt = Integer.parseInt(mText);
        int q = mInt/nInt;
        quotient = (Integer.toString(q));
    }
    

    不要这样做,因为这有时会起作用,有时会失败。您并不真正关心这两个字符串是否是相同的对象(这是==运算符测试的内容),而是想知道它们是否包含相同的字符串数据。为此,应使用equals或equalsIgnoreCase方法:

    if ("Find GCD".equals(event.getActionCommand())) {
        int nInt = Integer.parseInt(nText);
        int mInt = Integer.parseInt(mText);
        int q = mInt/nInt;
        quotient = (Integer.toString(q));
    }
    
  2. # 2 楼答案

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

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

    解决方案是去掉类变量