2013-06-14 1 views
0

결과를 내부에 쓰는 Jtextfield가 있습니다. 그런 다음 커서가 jtextfield에있는 동안 enter 키를 누르고 next라는 이름의 jbutton을 활성화합니다. 내 생성자에이 명령 : "getRootPane(). setDefaultButton (다음);"및 잘 작동합니다. 모든 jbuttons 및 jlabels 글꼴을 desing에서 변경 한 다음 jtextfield 및 press를 입력 할 때 아무 일도 일어나지 않습니다.텍스트를 작성한 다음 Enter 키를 누르면 jbutton이 활성화됩니다

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

     } 

    }); 
} 
+0

관련 코드를 게시하고 빈 줄을 잘라내십시오. 또한 일어나는 일과 예상했던 일을 명확하게 나타낼 수 있습니까? 당신의 질문은 또한 당신이 작업 버전을 가지고 있었고 약간의 변경 후에 그것은 작동을 멈추었다는 인상을줍니다. 제발 작동을 멈췄을 때 당신이 무엇을 바꿨는지 분명하게 나타내주십시오. – Robin

+0

죄송합니다.하지만 첫 번째 게시물이었습니다. 정말 어떻게 쓰는지 알고 있습니다. 내 문제에 대해 더 자세히 설명해 드리겠습니다. 나는 내부에 결과를 쓰는 Jtextfield를 가지고 있습니다. . 커서가 jtextfield에있는 동안 enter 키를 누른 다음 next.jbutton이라는 이름의 jbutton을 활성화하려면이 명령을 내 생성자에게 주어야합니다. "getRootPane(). setDefaultButton (next);", 잘 작동합니다. 모든 jbuttons 및 jlabels 글꼴을 desing 및 다음 jtextfield jtextfield writting 때 아무것도 발생하지 않습니다. – user2485610

+0

@ user2485610 댓글에 세부 정보를 입력하는 대신 원래 질문을 수정해야합니다. – 1615903

답변

1

나는 내가 내 커서가 JTextField에있는 동안 Enter 키를 누릅니다 할 inside.Then 결과를 작성하고 다음라는 이름의 JButton를 활성화 JTextField로 있습니다.

텍스트 필드와 버튼에 동일한 ActionListener를 사용하십시오.

ActionListener next = new ActionListener(...); 
textField.addActionListener(next); 
button.addActionListener(next); 
관련 문제