我希望在創建它後關閉的JFrame。
(不帶按鈕)。
但在構造函數上
不,你不知道。您不想嘗試顯示Swing GUI對象,然後在完全構建之前關閉它。我想認爲你想要做的(你的問題不完全清楚)是顯示窗口,顯示一個JOptionPane,然後關閉GUI窗口。如果是這樣,你需要在調用GUI對象的構造函數的代碼中執行此操作,而不是在構造函數中執行此操作,以便處理完全實現的對象。因此,例如,像這樣:
ClientSideForm clientForm = new ClientSideForm();
clientForm.setVisible(true);
JOptionPane.showMessageDialog(rootPane, "Terjadi Kesalahan Dalam Membuka Aplikasi Utama",
"Kesalahan", JOptionPane.ERROR_MESSAGE);
clientForm.dispose();
旁註:
- 按鈕有效,因爲即使是在由ClientSideForm構造函數調用的方法創建的,即使它的ActionListener可能已經被附在你的代碼的相同部分,它不會在構造函數中執行它的動作,但是隻有在按下按鈕刺激其ActionListener動作後ClientSideForm對象完全創建並顯示後,纔會執行它的動作。
- 如果您的GUI具有多個顯示的JFrame然後消失,請考慮修改更方便用戶的結構,因爲大多數用戶不希望將一堆窗口推送給他們。請看The Use of Multiple JFrames, Good/Bad Practice?,看看爲什麼這很重要,並看看可用的替代方案。
- 如果您想要在設定的時間段內顯示並關閉GUI,請使用Swing Timer。
- 通過讓類擴展JFrame,迫使您創建和顯示JFrame,而且通常需要更多靈活性時,您可能會將自己描繪在一個角落。事實上,我敢打賭,我已經創建了大多數Swing GUI代碼,而且我已經看到了而不是擴展了JFrame,事實上很少有人會想要這樣做。更常見的是,您的GUI類將專門用於創建JPanel,然後可將其放置到JFrame或JDialogs或JTabbedPanes中,或在需要時通過CardLayouts進行交換。這將大大增加您的GUI編碼的靈活性。
例如,下面的代碼將顯示一個GUI,一半的時間將顯示一個JOptionPane錯誤消息,然後在錯誤消息關閉後關閉GUI,另一半時間將顯示2秒鐘的GUI,然後自動關閉:
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class TestPanel extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = 400;
private static final int TIMER_DELAY = 2 * 1000; // 2 seconds
public TestPanel() {
JLabel label = new JLabel("Test GUI");
label.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 100));
setPreferredSize(new Dimension(PREF_W, PREF_H));
setLayout(new GridBagLayout());
add(label);
}
// this code is called from a main method, but could be called anywhere, from
// a JButton's action listener perhaps. If so, then I'd probably not create
// a new JFrame but rather a JDialog, and place my TestPanel within it
private static void createAndShowGui() {
TestPanel mainPanel = new TestPanel();
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
if (Math.random() > 0.5) {
// 50% chance of this happening
String message = "Error In Opening Main App";
int type = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(frame, message, "Error", type);
frame.dispose();
} else {
// run timer
new Timer(TIMER_DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose(); // dispose gui when time's up
((Timer) e.getSource()).stop(); // and stop the timer
}
}).start();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
所以 - 什麼時候是它應該關閉,哪些事件是觸發其關閉?請告訴你正在嘗試做什麼的細節。在完全創建之前,您真的不想在其自己的構造函數中關閉GUI對象。 –
此外,你會想要閱讀:[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/questions/9554636) –
你的目標是顯示JFrame,然後顯示JOptionPane,然後當JOptionPane不再可見,請處理JFrame?如果是這樣,在GUI對象JFrame完全創建之前,而不是在調用構造函數的代碼中的其他位置之前,這不應該在JFrame的構造函數中完成。 –