我试图从JDialog
得到WindowOpened
事件,但它只被触发一次。 为什么windowClosing
正常工作并且WindowOpened
只有一次?有没有什么办法可以每次开启JDialog
的公开活动?JDialog WindowOpened事件只被触发一次
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
public class NewClass extends JDialog {
public void init() {
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
setModal(true);
setSize(100,100);
setLocationRelativeTo(null);
}
public void addListener() {
addWindowListener(
new java.awt.event.WindowAdapter() {
public void windowOpened(WindowEvent e) {
System.out.println("Invoking WindowOpened from JDialog");
}
public void windowClosing(WindowEvent e) {
System.out.println("Invoking WindowClosing from JDialog");
dispose();
}
});
}
public static void main(String args[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(200,70);
final NewClass d = new NewClass();
d.init();
d.addListener();
JButton b = new JButton("Show Dialog");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setVisible(true);
}
});
f.getContentPane().add(b);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
你试过WindowActivated了吗? WindowOpened在首次打开时工作。每次激活窗口时,WindowActivated都可以使用。并将您的对话框更改为JFrame,这样您可以最小化您的窗口。 – 2013-02-28 16:43:26
@GabrielCâmara*“并将您的对话框更改为JFrame ..”*这不是一个好的举措。请参阅[使用多个JFrames,好/坏实践?](http://stackoverflow.com/a/9554657/418556) – 2013-02-28 16:45:53
我同意@AndrewThompson,但他没有说他使用多个窗口。有一个框架没有错......它也可能是一个主框架。 – 2013-02-28 16:49:39