简体   繁体   English

如何在应用程序屏幕中央显示加载对话框?

[英]how to display loading dialog at center of app screen?

I want to know how to display loading dialog at center of app screen. 我想知道如何在应用程序屏幕的中心显示加载对话框。 An indefinite progress bar. 不确定的进度条。

如果您在谈论JDialog,请在其上调用pack()后,调用setLocationRelativeTo(null)使其居中。

From the documentation : 文档中

JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);

This will create the progress bar. 这将创建进度条。 To center it, you should take a look at the different kinds of layout managers in java. 要居中,您应该看一下Java中不同类型的布局管理器 Without any examples of your existing code, it's hard to give a more precise answer to your question. 没有现有代码的任何示例,很难为您的问题提供更准确的答案。

display loading dialog at center of app screen. 在应用程序屏幕的中心显示加载对话框。

dialog.setLocationRelativeTo(...);

An indefinite progress bar. 不确定的进度条。

Read the section from the Swing tutorial on How to Use Progress Bars 阅读Swing教程中有关如何使用进度条的部分

Here's how I typically show a "loading..." progress bar. 这是我通常显示“正在加载...”进度条的方式。 The loading itself must happen on a background thread to make sure the progress bar keeps updating. 加载本身必须在后台线程上进行,以确保进度条不断更新。 The frame with the progress bar will be shown in the center of the screen. 带有进度条的框架将显示在屏幕中央。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM