java - 如何编写文本,然后按 Enter 键并激活 j 按钮

标签 java swing jbutton enter

我有一个 Jtextfield,我在其中写入结果。然后我想在光标位于 jtextfield 时按 Enter 键,并激活名为 next 的 jbutton。我必须向我的构造函数执行此命令:“getRootPane().setDefaultButton(接下来);“,并且工作良好。在我从所有 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);

        }

    });
}

最佳答案

I have a Jtextfield where i write a result inside.Then i want to press enter while my cursor is in the jtextfield,and activate a jbutton named next.

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

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

关于java - 如何编写文本,然后按 Enter 键并激活 j 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17106259/

相关文章:

java - Swing 是否具有 Excel 电子表格的导入/导出功能? ( java )

java - 无法在矩形顶部创建按钮

java - 减小 JTable 单元格中自定义渲染组件的大小

java - 使按钮不可调整大小

java - 整理 JUnit 4 动态套件的详细信息

java - application.yml 和 @value

java - 通过单击 JButton 绘制形状

java - 如何对 Spring Security @PreAuthorize(hasRole) 进行单元测试?

java - 编写方法时传递参数数量与传递映射哪个更好?

java - 如何使 JButton 名称的一部分着色?