[英]JDialog modal problem java gui
我有這段代碼,運行時沒有任何錯誤,但是沒有按照我的計划運行。 我試圖弄清楚為什么當我單擊標記為“重要!”的按鈕時,為什么沒有顯示“繼續”按鈕。
唯一要顯示的是空白的彈出窗口,該窗口是代碼JDialog的一部分,並將其設置為模態和可見的。 我只是想不通。 如果有人可以幫助我,我將不勝感激。
JPanel hehePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,10,20));
JDialog dialog = new JDialog((JFrame)null);
dialog.getContentPane().add(hehePanel,BorderLayout.CENTER);
JButton hButton = new JButton("important!!");
JButton fButton = new JButton(" on construction !!");
JButton exitButton = new JButton("EXIT CAW ");
hehePanel.add(hButton);
hButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final JPanel hehePanel = new JPanel();
final JDialog dialog = new JDialog();
dialog.getContentPane().add(hehePanel,BorderLayout.PAGE_END);
dialog.toFront();
dialog.setModal(true);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
JButton closebutton = new JButton("Continue");
closebutton.setActionCommand("continue");
closebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("continue")) {
dialog.dispose();
}
}
});
hehePanel.add(closebutton);
}
});
hehePanel.add(fButton);
hehePanel.add(exitButton);
在使對話框可見之前以及在pack()
對話框之前 ,需要將所有組件添加到對話框中。
在對話框關閉之前, dialog.setVisible(true)
之后的所有代碼都不會執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.