0

我有一个 Jtextfield,我在里面写了一个结果。然后我想在光标位于 jtextfield 时按 Enter,并激活一个名为 next 的 jbutton。我必须向我的构造函数发送这个命令:“getRootPane().setDefaultButton(next); “,并且工作得很好。在我从所有 jbuttons 和 jlabels 中更改字体,然后当我写我的 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);

        }

    });
}
4

1 回答 1

1

我有一个 Jtextfield,我在里面写了一个结果。然后我想在光标位于 jtextfield 时按 Enter,然后激活一个名为 next 的 jbutton。

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

ActionListener next = new ActionListener(...);
textField.addActionListener( next );
button.addActionListener( next );
于 2013-06-14T15:26:50.317 回答