Java 面板上的错误事件
我有一个带有 4 个按钮的 java 面板。当我单击这些按钮时,会出现一个新框架,并且第一个框架被 setVisibile(false)
隐藏。 在那个新窗口上,我有另一个按钮,但是当我单击它时,我得到了与第一个窗口的第四个按钮相对应的事件。再次单击该按钮即可解决问题,但这当然是不可接受的。 我错过了什么吗?我只是显示框架,
nameOfTheFrame.setVisible(true);
并且每个按钮上都有 MouseListeners。
最后一个按钮的代码很简单:
System.exit(0);
编辑
示例代码:
private void btn_joinGamePressed(java.awt.event.MouseEvent evt) {
GraphicsTools.getInstance().getCreateGame().setVisible(false);
GraphicsTools.getInstance().getMainPanel().setVisible(false);
GraphicsTools.getInstance().getRegistration().setVisible(true);
}
GraphicsTools 是一个单例。
编辑2 更多信息。 我注意到在 MAC 操作系统上运行得很好。该问题仅发生在 Linux 和 Windows 上。
I have a java panel with 4 buttons. When I click on of these buttons, a new frame appears and the first is hidden with setVisibile(false)
.
On that new window, I have another button, but when i click it, I got the event corresponding to the fourth button of the first window. Clicking the button again does the trick, but of course this is not acceptable.
Am I missing something? I just show the frames with
nameOfTheFrame.setVisible(true);
and I have MouseListeners on every button.
The code of the last button is simply:
System.exit(0);
EDIT
Sample code:
private void btn_joinGamePressed(java.awt.event.MouseEvent evt) {
GraphicsTools.getInstance().getCreateGame().setVisible(false);
GraphicsTools.getInstance().getMainPanel().setVisible(false);
GraphicsTools.getInstance().getRegistration().setVisible(true);
}
GraphicsTools is a Singleton.
EDIT 2
Some more informations.
I noticed that on MAC OS works fine. The problem happens only on Linux and Windows.
这一定是由于您的鼠标侦听器而发生的。可能是在您第一次单击时识别旧按钮,该按钮与新按钮位于同一位置(这只是我的猜测)。
将鼠标侦听器更改为动作侦听器。对于按钮来说,如果有动作监听器就足够了。
试试这个。
This must be happening because of your mouse listeners. May be it is identifying the old button in your first click which is in the same location of new button (It is just my guess).
Change the mouse listeners to action listeners. For a button, it is sufficient if you have action listener.
Try this.