首先,我对Java Swing的经验不足,因此请忽略我的错误。
现在来解决问题。
我正在尝试在桌面应用程序中实现启动屏幕。所以我成功地做了很少的启动画面设计。

这是代码:

开机画面:

package com.openbravo.pos.forms;
public class demoform1 extends javax.swing.JFrame {

public demoform1() {
    initComponents();
    System.out.println("demoform1........");
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767));
    defaultpanel = new javax.swing.JPanel();
    title = new java.awt.Label();
    loading = new java.awt.Label();
    loadingnum = new javax.swing.JProgressBar();

    setUndecorated(true);

    defaultpanel.setBackground(new java.awt.Color(1, 231, 238));
    defaultpanel.setForeground(new java.awt.Color(1, 1, 1));
    defaultpanel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

    title.setAlignment(java.awt.Label.CENTER);
    title.setFont(new java.awt.Font("URW Chancery L", 3, 36)); // NOI18N
    title.setForeground(new java.awt.Color(254, 254, 254));
    title.setName("agile"); // NOI18N
    title.setText("Agile infoways pvt. ltd.");

    loading.setAlignment(java.awt.Label.CENTER);
    loading.setFont(new java.awt.Font("URW Chancery L", 3, 24)); // NOI18N
    loading.setForeground(new java.awt.Color(254, 254, 254));
    loading.setName(""); // NOI18N
    loading.setText("99%");

    loadingnum.setBackground(new java.awt.Color(247, 253, 22));
    loadingnum.setForeground(new java.awt.Color(1, 1, 1));

    javax.swing.GroupLayout defaultpanelLayout = new javax.swing.GroupLayout(defaultpanel);
    defaultpanel.setLayout(defaultpanelLayout);
    defaultpanelLayout.setHorizontalGroup(
        defaultpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(defaultpanelLayout.createSequentialGroup()
            .addGap(107, 107, 107)
            .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(114, Short.MAX_VALUE))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, defaultpanelLayout.createSequentialGroup()
            .addGap(0, 0, Short.MAX_VALUE)
            .addComponent(loading, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(294, 294, 294))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, defaultpanelLayout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(loadingnum, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addGap(277, 277, 277))
    );
    defaultpanelLayout.setVerticalGroup(
        defaultpanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(defaultpanelLayout.createSequentialGroup()
            .addGap(62, 62, 62)
            .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(67, 67, 67)
            .addComponent(loading, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(loadingnum, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
            .addGap(64, 64, 64))
    );

    title.getAccessibleContext().setAccessibleName("loading");
    loading.getAccessibleContext().setAccessibleName("loading");
    loadingnum.getAccessibleContext().setAccessibleName("loadingnumber");
    loadingnum.getAccessibleContext().setAccessibleDescription("");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(defaultpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(defaultpanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    setBounds(0, 0, 711, 379);
}// </editor-fold>
// Variables declaration - do not modify
public javax.swing.JPanel defaultpanel;
private javax.swing.Box.Filler filler1;
public java.awt.Label loading;
public javax.swing.JProgressBar loadingnum;
public java.awt.Label title;
// End of variables declaration
}


我在主根应用程序中调用初始屏幕的代码:

        demoform1 demo1 = new demoform1();
        try {
            demo1.setVisible(true);
            for (int i = 0; i <= 100; i++) {
                Thread.sleep(40);
                demo1.loading.setText(Integer.toString(i) + "%");
                demo1.loadingnum.setValue(i);
                if (i == 100) {
                    demo1.setVisible(false);
                    demo1 = null;
                }
            }
        } catch (Exception e) {
      }


所以问题是我的启动画面执行得很好,但是没有收到可接受的输出。

预期的初始屏幕:

java - Java Swing启动画面配置错误-LMLPHP

当前启动画面:

java - Java Swing启动画面配置错误-LMLPHP

如果有人可以提供帮助,那将是一种荣幸。

最佳答案

我看到您正在使用许多Swing类,而您只需要使用JavaFX和Scene Builder来完成此初始屏幕。

您可以使用预加载器检查以下一项:
https://docs.oracle.com/javafx/2/deployment/preloaders.htm

这个想法基本上是做一个初始屏幕,然后:

public class Main {
   public static void main(String[] args) {
      LauncherImpl.launchApplication(Main.class, SplashScreen.class, args);
   }
}


使用代码,您可以执行以下操作:

JWindow window = new JWindow();

        demoform1 df = new demoform1();

        window.getContentPane().add(df.getContentPane());

        window.setBounds(600,600,600,600);
        window.setLocationRelativeTo(null);

        window.setVisible(true);
        try {
             for (int i = 0; i <= 100; i++) {
                    Thread.sleep(40);
                    df.loading.setText(Integer.toString(i) + "%");
                    df.loadingnum.setValue(i);
                    if (i == 100) {
                        window.setVisible(false);
                        JFrame frame = new JFrame();
                        frame.add(new JLabel("It Worked!!!!!"));
                        frame.setVisible(true);
                        frame.setSize(300,100);
                        window.dispose();
                    }
                }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


这将完成加载效果,然后显示带有简单消息的简单JFrame。

我不是Swing专家,但我希望这对您有所帮助。

09-27 06:12