如何写文本,然后按Enter键和jbutton激活

时间:2013-06-14 10:19:44

标签: java swing jbutton enter

我有一个Jtextfield,我在里面写了一个结果。然后我想在我的光标在jtextfield中时按Enter键,并激活一个名为next的jbutton。我必须使用我的构造函数这个命令:“getRootPane()。setDefaultButton(接下来),“并且工作得很好。在我从所有jbuttons和jlabels中设置字体后,然后当我写i jtextfield并按下输入时,没有任何事情发生。

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);

        }

    });
}

1 个答案:

答案 0 :(得分:1)

  

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

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

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