2012-04-29 2 views
3

사용자 이름과 암호의 두 필드가있는 JDialog가 있습니다. 나는 enter를 누르는 것과 같이 계속 될 것과 같이 정상적인 것 같이 모양을 만들고 싶다.계속하려면 Enter 키를 누르십시오

나는 이미 getRootPane().setDefaultButton(myButton);을 시도했지만 작동하지 않는 것 같습니다.

+1

더 나은 도움을 받으려면 [SSCCE] (http://sscce.org/) – mKorbel

답변

5

getRootPane(). setDefaultButton (myButton);을 이미 시도했지만 작동하지 않는 것 같습니다.

당신이 방법이 버튼에 대한 코드를 호출하는 것보다

JButton#doClick();

하지만 더 나은에 액션 청취자를 추가하여이를 달성 할 수 KeyBindings

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.Timer; 
import javax.swing.border.EmptyBorder; 

public class Test { 

    private static final long serialVersionUID = 1L; 
    private JDialog dialog = new JDialog(); 
    private final JPanel contentPanel = new JPanel(); 
    private Timer timer1; 
    private JButton killkButton = new JButton("Kill JDialog"); 
    private JButton okButton = new JButton("OK"); 

    public Test() { 
     contentPanel.setLayout(new FlowLayout()); 
     contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     JPanel buttonPane = new JPanel(); 
     okButton.setActionCommand("OK"); 
     buttonPane.add(okButton); 
     killkButton.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       System.exit(0); 
      } 
     }); 
     killkButton.setActionCommand("Kill JDialog"); 
     buttonPane.add(killkButton); 
     dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); 
     dialog.addWindowListener(new WindowListener() { 

      public void windowOpened(WindowEvent e) { 
      } 

      public void windowClosing(WindowEvent e) { 
       startTimer(); 
      } 

      public void windowClosed(WindowEvent e) { 
      } 

      public void windowIconified(WindowEvent e) { 
      } 

      public void windowDeiconified(WindowEvent e) { 
      } 

      public void windowActivated(WindowEvent e) { 
      } 

      public void windowDeactivated(WindowEvent e) { 
      } 
     }); 
     dialog.setLayout(new BorderLayout()); 
     dialog.getRootPane().setDefaultButton(okButton); 
     dialog.add(buttonPane, BorderLayout.SOUTH); 
     dialog.add(contentPanel, BorderLayout.CENTER); 
     dialog.pack(); 
     dialog.setLocation(100, 100); 
     dialog.setVisible(true); 
     setKeyBindings(); 
    } 

    private void setKeyBindings() { 
     okButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
       KeyStroke.getKeyStroke("ENTER"), "clickENTER"); 
     okButton.getActionMap().put("clickENTER", new AbstractAction() { 

      private static final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       dialog.setVisible(false); 
       startTimer(); 
      } 
     }); 
    } 

    private void startTimer() { 
     timer1 = new Timer(1000, new AbstractAction() { 

      private static final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       SwingUtilities.invokeLater(new Runnable() { 

        @Override 
        public void run() { 
         dialog.setVisible(true); 
        } 
       }); 
      } 
     }); 
     timer1.setDelay(500); 
     timer1.setRepeats(false); 
     timer1.start(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       Test test = new Test(); 
      } 
     }); 
    } 
} 
2
JButton button = ... 
JTextField password = ... 
ActionListener buttonListener = ... 

button.addActionListner(buttonListener); 
password.addActionListener(buttonListener); 

JTextField에서 입력을 누르면 동작 이벤트가 발생합니다.

1

사용된다 당신의 텍스트 필드.

JTextField field1 = new JTextField(); 

field1.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 

     //here is your method to continue 
     continue(); 

     } 

    });  
관련 문제