[英]Default button in JFrame is not firing when the enter key is being pressed
I have a JFrame
with three JButtons on it. 我有一个带有三个JButton的
JFrame
。 I have set txtSearch
(a JTextField
component) to have the focus when JFrame
loads. 我已经设置了
txtSearch
(一个JTextField
组件)来在JFrame
加载时获得焦点。 One of the buttons is set as the default button. 其中一个按钮被设置为默认按钮。 This is my code:
这是我的代码:
private void formWindowOpened(java.awt.event.WindowEvent evt)
{
// btnRefresh.setMnemonic(KeyEvent.VK_R); // Even if this line
// is not commented, but
// still the event wouldn't fire.
this.getRootPane().setDefaultButton(btnRefresh);
}
When it loads, the button is just selected, but it did nothing when the Enter key was being pressed. 加载时,只选择了按钮,但按下Enter键时没有任何操作。 How do I correctly implement it?
我该如何正确实现它?
btnRefresh.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRefreshActionPerformed(evt);
}
});
private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(this, "Pressed!");
// Other codes here (Replace by JOptionPane)
}
What component has focus when the JFrame
comes up? 当
JFrame
出现时,什么组件有焦点? I ask because some components "eat" the Enter key event. 我问,因为一些组件“吃掉”Enter键事件。 For example, a
JEditorPane
will do that. 例如,
JEditorPane
会这样做。
Also, when you assign an ActionListener
to JTextField
, the ActionListener
will be called instead of the DefaultButton
for the root pane. 此外,将
ActionListener
分配给JTextField
,将调用ActionListener
而不是根窗格的DefaultButton
。 You must choose either to have an ActionListener
or a DefaultButton
, but you can't have both fire for the same JTextField
. 您必须选择具有
ActionListener
或DefaultButton
,但是您不能同时触发同一个JTextField
。 I'm sure this applies to other components as well. 我确信这也适用于其他组件。
I don't see what you are doing incorrectly from what is posted. 我没有看到你发布的内容不正确。 Here is a short example that works.
这是一个有效的简短示例。 Perhaps it will reveal something useful to you.
也许它会揭示对你有用的东西。
import java.awt.BorderLayout;
public class ExampleFrame extends JFrame
{
private JPanel m_contentPane;
private JTextField m_textField;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
ExampleFrame frame = new ExampleFrame();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ExampleFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
m_contentPane = new JPanel();
m_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
m_contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(m_contentPane);
m_textField = new JTextField();
m_contentPane.add(m_textField, BorderLayout.NORTH);
m_textField.setColumns(10);
JButton btnNewButton = new JButton("Default");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(ExampleFrame.this, "Default.");
}
});
m_contentPane.add(btnNewButton, BorderLayout.CENTER);
JButton btnNewButton_1 = new JButton("Not default");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(ExampleFrame.this, "Not default.");
}
});
m_contentPane.add(btnNewButton_1, BorderLayout.WEST);
m_textField.requestFocus();
getRootPane().setDefaultButton(btnNewButton);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.