I have the following two classes:
我有以下两门课:
#1
# 1
public class LobbyView extends JPanel
{
private final JButton sendGameRequestButton = new JButton();
public JButton getSendGameRequestButton()
{
return sendGameRequestButton;
}
LobbyView()
{
sendGameRequestButton.setPreferredSize(new Dimension(15, 20));
sendGameRequestButton.setText("Send game request");
sendGameRequestButton.addMouseListener(new LobbyListener(this));
sendGameRequestButton.setEnabled(false);
}
}
#2
# 2
public class LobbyListener implements MouseListener
{
LobbyView lobbyView;
public LobbyListener(LobbyView sentLobbyView)
{
lobbyView = sentLobbyView;
}
@Override
public void mouseClicked(MouseEvent e)
{
if (e.getButton() == 1)
{
if (e.getSource() == lobbyView.getSendGameRequestButton())
{
System.out.println("You pushed the disabled button");
}
}
}
Even though I disabled the JButton in the LobbyView constructor, I can still click it and get the message "You pushed the disabled button".
即使我禁用了游标视图构造函数中的JButton,我仍然可以单击它并获取“您按下禁用按钮”的消息。
Does component.setEnabled(false) actually DISABLE a component, or just gray it out to make it LOOK disabled?
组件。setenabled (false)实际上是禁用组件,还是只是让它看起来是禁用的?
1 个解决方案
#1
5
Even though I disabled the JButton in the LobbyView constructor, I can still click it
即使我禁用了游标视图构造函数中的JButton,我仍然可以单击它。
That is correct. You should NOT be using a MouseListner. The MouseListener works independent of the state of the button.
这是正确的。您不应该使用MouseListner。MouseListener独立于按钮的状态。
Instead you should be using an ActionListener
. Read the section from the Swing tutorial on How to Use Buttons for more information. Or there is also a section on How to Write an Action Listener
.
相反,应该使用ActionListener。阅读Swing教程中的章节,了解如何使用按钮获取更多信息。或者也有一节关于如何编写一个动作监听器。
#1
5
Even though I disabled the JButton in the LobbyView constructor, I can still click it
即使我禁用了游标视图构造函数中的JButton,我仍然可以单击它。
That is correct. You should NOT be using a MouseListner. The MouseListener works independent of the state of the button.
这是正确的。您不应该使用MouseListner。MouseListener独立于按钮的状态。
Instead you should be using an ActionListener
. Read the section from the Swing tutorial on How to Use Buttons for more information. Or there is also a section on How to Write an Action Listener
.
相反,应该使用ActionListener。阅读Swing教程中的章节,了解如何使用按钮获取更多信息。或者也有一节关于如何编写一个动作监听器。