该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ModalTest {
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
JButton b = new JButton("POPUP");
final JDialog d = new ModalDialog(f, "test");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d.setVisible(true);
System.out.println(SwingUtilities.isEventDispatchThread());
System.out.println(Thread.currentThread().getName());
}
});
f.add(b);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}
class ModalDialog extends JDialog {
ModalDialog(JFrame parent, String msg) {
super(parent, true);
setTitle(msg);
setSize(400, 300);
setLocationRelativeTo(getParent());
JButton b = new JButton(new AbstractAction("click") {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(SwingUtilities.isEventDispatchThread());
System.out.println(Thread.currentThread().getName());
}
});
add(b);
}
}