Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何写一个文本,然后按enter键,一个jbutton就会激活_Java_Swing_Jbutton_Enter - Fatal编程技术网

Java 如何写一个文本,然后按enter键,一个jbutton就会激活

Java 如何写一个文本,然后按enter键,一个jbutton就会激活,java,swing,jbutton,enter,Java,Swing,Jbutton,Enter,我有一个Jtextfield,在其中写入一个结果。然后我想在光标位于Jtextfield时按enter键,并激活一个名为next的jbutton。我必须向构造函数发送以下命令:“getRootPane().setDefaultButton(next);”,在我从设计所有jbuttons和jlabel的字体,到编写jtextfield并按enter之后,一切都没有发生 public class PrakseisGameGUI extends javax.swing.JFrame { int

我有一个Jtextfield,在其中写入一个结果。然后我想在光标位于Jtextfield时按enter键,并激活一个名为next的jbutton。我必须向构造函数发送以下命令:“getRootPane().setDefaultButton(next);”,在我从设计所有jbuttons和jlabel的字体,到编写jtextfield并按enter之后,一切都没有发生

public class PrakseisGameGUI extends javax.swing.JFrame {



int correctAns;
int allAns;
int oldResult;
int flag;

public PrakseisGameGUI() {

    initComponents();

    getRootPane().setDefaultButton(next);
    Global.correctAns = 0;
    Global.allAns = 0;
    oldResult = -500;
    flag = 0;
 }

 /**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")

private void nextActionPerformed(java.awt.event.ActionEvent evt) {                                     
    // TODO add your handling code here:
    String sResult;
    String sCorAns;
    String sAllAns;
    String sOperator;


    int iFirstNum = 0;
    int iSecNum = 0;
    int iOperator = 0;
    int iResult = 0;
    int iCorResult = 0;
    int oldFirstNum = 0;
    int oldSecNum = 0;
    int oldOperator = 0;

    Random rand = new Random();


    JFrame frame = new JFrame();
    JButton btn = new JButton("Next");
    frame.getRootPane().setDefaultButton(btn);




    if(Global.epdi == 1){
        iFirstNum = rand.nextInt(11);
        iSecNum = rand.nextInt(11);
        iOperator = rand.nextInt(2);
    }else if(Global.epdi == 2){
        iFirstNum = rand.nextInt(11);
        iSecNum = rand.nextInt(11);
        iOperator = rand.nextInt(3);
    }else if(Global.epdi == 3){
        iFirstNum = rand.nextInt(20);
        iSecNum = rand.nextInt(11);
        iOperator = rand.nextInt(3);
    }else{
        iFirstNum = rand.nextInt(11);
        iSecNum = rand.nextInt(11);
        iOperator = rand.nextInt(2);

    }
    if(iOperator == 0){
        sOperator = "+";
        iCorResult = iFirstNum + iSecNum;
    }else if(iOperator == 1){
        sOperator = "-";
        iCorResult = iFirstNum - iSecNum;
    }else if(iOperator == 2){
        sOperator = "*";
        iCorResult = iFirstNum * iSecNum;
    }else{
        sOperator = "-----";
        iCorResult = 0;
    }
    firstNum.setText(String.valueOf(iFirstNum));
    operator.setText(sOperator);
    secNum.setText(String.valueOf(iSecNum));
    stableOperator.setText("=");
    slash.setText("/");
    long startTime = System.nanoTime();       

    if((oldResult != -500) && (flag == 1)){

        sResult = result.getText();
        iResult = Integer.parseInt(sResult);

        System.out.format("%d,%d\n",iResult,oldResult);
        if(iResult == oldResult){
            Global.correctAns++;
        }
        //result.setText("");

        oldResult = iCorResult;
        Global.allAns++;
    }else if(flag == 0) {

            oldResult = iCorResult;
            flag =1;
    }

    sCorAns = String.valueOf(Global.correctAns);
    sAllAns = String.valueOf(Global.allAns);
    corAnswer.setText(sCorAns);
    allAnswer.setText(sAllAns);
    if(Global.allAns == 10){
        Global.estimatedTime = System.nanoTime() - startTime;
        System.out.format("%d\n", Global.estimatedTime);
        setVisible(false);
        seeResults seRes = new seeResults();
        seRes.setVisible(true);
    }

}                                    




private void nextKeyPressed(java.awt.event.KeyEvent evt) {                                
    // TODO add your handling code here:
}                               

private void resultActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
}                                      

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {

            new PrakseisGameGUI().setVisible(true);

        }

    });
}
我有一个Jtextfield,我在其中写入一个结果。然后我想在光标位于Jtextfield时按enter键,并激活一个名为next的jbutton

对文本字段和按钮使用相同的ActionListener

ActionListener next = new ActionListener(...);
textField.addActionListener( next );
button.addActionListener( next );

请只张贴相关代码,并尽量减少空行。此外,您能否清楚地指出正在发生什么以及您预期会发生什么。你的问题也给人一种印象,你有一个工作版本,稍作修改后就停止工作了。请明确指出当它停止工作时你改变了什么对不起,但这是我的第一篇文章,我不知道;我真的不知道如何写。让我进一步解释我的问题。我有一个Jtextfield,我在其中写了一个结果。然后我想在光标位于Jtextfield时按enter键,激活一个名为next的jbutton。我必须向我的构造函数发送以下命令:“getRootPane().setDefaultButton(next);”,而且效果很好。在我从设计所有jbutton和jlabel的字体改为编写jtextfield并按enter键后,什么都没有发生。@user2485610您应该编辑原始问题,而不是在评论中提供详细信息。@user2485610不要在评论中发布解释,请编辑您的帖子。要编写适当的示例,请提供一个。该链接将为您提供如何生成一个好的代码示例的建议,该示例将为您提供一个快速而好的答案。我改变了。现在看起来怎么样?