while 循环忽略事件监听器
因此,当我运行此代码尝试更改背景时,GUI 崩溃并陷入无限 while 循环,忽略事件侦听器。这是代码:
private Panel getPanel1() {
if (panel1 == null) {
panel1 = new Panel();
panel1.setLayout(new GridBagLayout());
while(frame.isVisible()){
panel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
frame.setVisible(false);
}
});
int r = (int) (Math.random()*255);
int g = (int) (Math.random()*255);
int b = (int) (Math.random()*255);
Color c = new Color(r, g, b);
panel1.setBackground(c);
try {
Thread.sleep(4000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
panel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
/*panel1.setVisible(false);
frame.setVisible(false);*/
System.exit(0);
}
});
}
}
return panel1;
}
它没有退出终止程序或事件更改背景的循环,而是只显示面板而不执行其他操作,我必须强制它退出。我应该怎么办?
so when i run this code to try to change the background the GUI crashes and gets stuck in a infinite while loop ignoring the event listeners. here is the code:
private Panel getPanel1() {
if (panel1 == null) {
panel1 = new Panel();
panel1.setLayout(new GridBagLayout());
while(frame.isVisible()){
panel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
frame.setVisible(false);
}
});
int r = (int) (Math.random()*255);
int g = (int) (Math.random()*255);
int b = (int) (Math.random()*255);
Color c = new Color(r, g, b);
panel1.setBackground(c);
try {
Thread.sleep(4000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
panel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
/*panel1.setVisible(false);
frame.setVisible(false);*/
System.exit(0);
}
});
}
}
return panel1;
}
instead of exiting the loop of terminating the program or event changing the background it just displays the panel and does nothing else and i have to force it to quit. what should i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过在循环中调用
sleep
,您可以有效地阻止 UI 线程。在该循环中,您还在每次迭代中添加两个侦听器,这非常奇怪。不要阻塞 UI 线程。让 GUI 框架负责传递事件等。基本上,您需要对 UI 采用基于事件的方法,而不是您当前正在采用的方法,这种方法永远不会让任何事件被发送(如你永远不会将控制权返回给调用者)。
创建面板,添加适当的事件侦听器,然后将其返回给调用者。如果您想每 4 秒更改一次背景颜色,您应该通过一个计时器,这样它就不会阻塞等待 4 秒过去的 UI 线程。
You're effectively blocking the UI thread by calling
sleep
in a loop. In that loop you're also adding two listeners on every iteration too, which is quite bizarre.Don't block the UI thread. Let the GUI framework take care of delivering events etc. Basically you need to take an event-based approach to UI, rather than the approach you currently are taking, which will never let any events get despatched (as you're never returning control to the caller).
Create the panel, add the appropriate event listener, and then just return it to the caller. If you want to change the background colour every 4 seconds, you should do that via a timer so that it's not blocking the UI thread waiting for the 4 seconds to elapse.