我已经完成了下面的代码,但是有些我无法将焦点设置在 JDialog 上:( loginDialog.requestFocusInWindow() 返回 false。有没有办法让 JDialog 获得焦点?LoginDialog.this.txt_PASSWORD.requestFocusInWindow() 也返回 false .
Esc 按钮按下事件也不起作用
this.loginDialog = new JDialog();
this.loginDialog.setTitle(applicationName+Keys.BLANK+Keys.DASH+Keys.BLANK+Messages.getMessage(IMessageKeys.LOGIN));
this.loginDialog.setModal(true);
this.loginDialog.setLayout(new BorderLayout());
this.loginPanel=getLoginPane();
this.buttonPanel=getButtonPanel();
this.infoLabel.setText(Keys.BLANK);
this.loginDialog.add(this.infoLabel,BorderLayout.NORTH);
this.loginDialog.add(this.loginPanel,BorderLayout.CENTER);
this.loginDialog.add(this.buttonPanel,BorderLayout.SOUTH);
this.loginDialog.setSize(370, 236);
this.loginDialog.setResizable(false);
this.loginDialog.setLocationRelativeTo(null);
objLogger.debug("Login dialog init method call end"); //$NON-NLS-1$
this.loginDialog.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
LoginDialog.this.loginDialog.requestFocus();
LoginDialog.this.loginDialog.requestFocusInWindow();
LoginDialog.this.txt_PASSWORD.addNotify();
LoginDialog.this.txt_PASSWORD.requestFocusInWindow();
LoginDialog.this.txt_PASSWORD.requestFocus();
}
@Override
public void windowClosing(WindowEvent e) {
close();
}
});
KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true);
this.loginDialog.getRootPane().getInputMap().put(ks, GenePanelConstants.CLOSE_ACTION);
this.loginDialog.getRootPane().getActionMap().put( GenePanelConstants.CLOSE_ACTION, new AbstractAction() {
private static final long serialVersionUID = 2871751669355251894L;
@Override
public void actionPerformed(ActionEvent ae) {
close();
}
});
this.txt_PASSWORD.requestFocusInWindow();
this.txt_PASSWORD.requestFocus();
this.loginDialog.setAlwaysOnTop(true);
this.loginDialog.setVisible(true);