朋友问的一个问题,结果自己晕了。
下面的代码希望实现点击一个按钮后,弹出一个对话框,对话框中另外有一个按钮,在点击对话框中按钮之前,原线程一直等待。他想用while(i == 1)实现(点击对话框中按钮后i = 0),但却发现对话框一片空白。
我查了很久,最后发现与while(i == 1)的位置有关,如果不去掉注释,则运行正常。否则,对话框一片空白。个人感觉与GUI线程有关,请大家帮忙看一下。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Test extends JFrame
{
int i = 1;
void testWhile()
{
System.out.println(Thread.currentThread());
new Thread()
{
public void run()
{
System.out.println(Thread.currentThread());
final JFrame info = new JFrame();
JButton b = new JButton("Test");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
i = 0;
System.out.println("dispose");
info.dispose();
}
});
info.add(b);
info.pack();
info.setVisible(true);
while(i == 1)
{
try
{
Thread.sleep(1);
}
catch(Exception ex)
{
}
}
}
}.start();
/*while(i==1)
{
try
{
Thread.sleep(1);
}
catch(Exception ex)
{
}
}*/
}
public Test()
{
JPanel test = new JPanel();
JButton b = new JButton("Test");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
testWhile();
}
});
test.add(b);
add(test);
setPreferredSize(new Dimension(800, 700));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new Test();
}
}
下面的代码希望实现点击一个按钮后,弹出一个对话框,对话框中另外有一个按钮,在点击对话框中按钮之前,原线程一直等待。他想用while(i == 1)实现(点击对话框中按钮后i = 0),但却发现对话框一片空白。
我查了很久,最后发现与while(i == 1)的位置有关,如果不去掉注释,则运行正常。否则,对话框一片空白。个人感觉与GUI线程有关,请大家帮忙看一下。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Test extends JFrame
{
int i = 1;
void testWhile()
{
System.out.println(Thread.currentThread());
new Thread()
{
public void run()
{
System.out.println(Thread.currentThread());
final JFrame info = new JFrame();
JButton b = new JButton("Test");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
i = 0;
System.out.println("dispose");
info.dispose();
}
});
info.add(b);
info.pack();
info.setVisible(true);
while(i == 1)
{
try
{
Thread.sleep(1);
}
catch(Exception ex)
{
}
}
}
}.start();
/*while(i==1)
{
try
{
Thread.sleep(1);
}
catch(Exception ex)
{
}
}*/
}
public Test()
{
JPanel test = new JPanel();
JButton b = new JButton("Test");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
testWhile();
}
});
test.add(b);
add(test);
setPreferredSize(new Dimension(800, 700));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new Test();
}
}
import java.awt.event.*;
import javax.swing.*;public class Test extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L; void testWhile() {
System.out.println(Thread.currentThread());
new Thread() {
public void run() {
System.out.println(Thread.currentThread()); final JFrame info = new JFrame();
JButton b = new JButton("Test");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("dispose");
info.dispose();
}
});
info.add(b);
info.pack();
info.setVisible(true);
}
}.start();
} public Test() {
JPanel test = new JPanel();
JButton b = new JButton("Test");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
testWhile();
}
});
test.add(b);
add(test);
setPreferredSize(new Dimension(800, 700));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
} public static void main(String[] args) {
new Test();
}
}感觉良好
event-dispatching thread:事件分发线程event-dispatching thread是一个执行绘画与事件处理代码的线程,比如paint和actionPerformed方法就会自动在event-dispatching thread中被执行,当actionPerformed中执行流程被阻塞,paint会因为没有被执行而导致界面不刷新