我試圖從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