[英]how to display loading dialog at center of app screen?
我想知道如何在應用程序屏幕的中心顯示加載對話框。 不確定的進度條。
如果您在談論JDialog,請在其上調用pack()后,調用setLocationRelativeTo(null)使其居中。
從文檔中 :
JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
這將創建進度條。 要居中,您應該看一下Java中不同類型的布局管理器 。 沒有現有代碼的任何示例,很難為您的問題提供更准確的答案。
這是我通常顯示“正在加載...”進度條的方式。 加載本身必須在后台線程上進行,以確保進度條不斷更新。 帶有進度條的框架將顯示在屏幕中央。
public static void main(String[] args) throws Throwable {
final JFrame frame = new JFrame("Loading...");
final JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
final JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPane.setLayout(new BorderLayout());
contentPane.add(new JLabel("Loading..."), BorderLayout.NORTH);
contentPane.add(progressBar, BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Runnable runnable = new Runnable() {
public void run() {
// do loading stuff in here
// for now, simulate loading task with Thread.sleep(...)
try {
System.out.println("Doing loading in background step 1");
Thread.sleep(1000);
System.out.println("Doing loading in background step 2");
Thread.sleep(1000);
System.out.println("Doing loading in background step 3");
Thread.sleep(1000);
System.out.println("Doing loading in background step 4");
Thread.sleep(1000);
System.out.println("Doing loading in background step 5");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// when loading is finished, make frame disappear
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.setVisible(false);
}
});
}
};
new Thread(runnable).start();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.