【问题标题】:How to write a text and then press enter and a jbutton activates如何编写文本然后按回车键并激活 jbutton
【发布时间】:2013-06-10 23:18:49
【问题描述】:

我有一个 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);

        }

    });
}

【问题讨论】:

  • 请只贴相关代码,尽量剪掉空行。此外,您能否清楚地指出正在发生的事情以及您期望发生的事情。您的问题还给人一种印象,即您有一个工作版本,经过一点改动后它就停止工作了。请清楚说明您在停止工作时所做的更改
  • 对此很抱歉,但这是我的第一篇文章,我真的不知道该怎么写。让我解释一下我的问题。我有一个 Jtextfield,我在里面写一个结果。然后我想在光标位于 jtextfield 时按 Enter,并激活一个名为 next 的 jbutton。我必须向我的构造函数发送这个命令:“getRootPane().setDefaultButton(next);”,并且工作良好。在我从设计字体更改之后从所有的 jbuttons 和 jlabels 中,然后当我写我的 jtextfield 并按 Enter 时,什么也没发生。
  • @user2485610 您应该编辑您的原始问题,而不是在 cmets 中提供详细信息。
  • @user2485610 不要在 cmets 中发布解释,而是编辑您的帖子。要编写适当的示例,请提供SSCCE。该链接将为您提供有关如何生成良好代码示例的建议,从而为您提供快速而良好的答案。
  • 我变了。现在怎么样了?

标签: java swing jbutton enter


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 2017-09-26
    • 2013-08-16
    • 2017-06-14
    • 2021-07-30
    • 2012-02-09
    • 2019-08-02
    相关资源
    最近更新 更多