JProgressBar完成后如何打开新窗口

4
我创建了FlashScreen.java,它是由JProgressBar组成的加载屏幕。 我希望在进度条百分比完成后关闭当前窗口并打开新窗口。 我做到了,但在关闭第一个窗口后,在下一个窗口中没有组件。空白窗口正在打开。
以下是代码: FlashScreen.java
    package crimeManagement;

import javax.swing.*;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;  
public class FlashScreen extends JFrame{  
JProgressBar jb;
JLabel lblStat;
int i=0,num=0;  

FlashScreen(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setBounds(new Rectangle(400, 200, 0, 0));
    jb=new JProgressBar(0,2000);
    jb.setBounds(100,219,579,22);  

    jb.setValue(0);  
    jb.setStringPainted(true);  

    getContentPane().add(jb);  
    setSize(804,405);  

    getContentPane().setLayout(null);  

    lblStat = new JLabel("");
    lblStat.setForeground(Color.CYAN);
    lblStat.setHorizontalAlignment(SwingConstants.CENTER);
    lblStat.setHorizontalTextPosition(SwingConstants.CENTER);
    lblStat.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
    lblStat.setBounds(229, 252, 329, 14);
    getContentPane().add(lblStat);

    JLabel lblBackGround = new JLabel("");
    lblBackGround.setHorizontalTextPosition(SwingConstants.CENTER);
    lblBackGround.setHorizontalAlignment(SwingConstants.CENTER);
    lblBackGround.setIcon(new ImageIcon(FlashScreen.class.getResource("/Images/FlashImage.jpg")));
    lblBackGround.setBounds(0, 0, 798, 376);
    getContentPane().add(lblBackGround);


    }  

public void iterate(){  
    while(i<=2000){  
        jb.setValue(i);  
        i=i+20;  
        try{
            Thread.sleep(50);           
            if(i==20)
            {
                lblStat.setText("Loading...");

            }
            if(i==500)
            {
                lblStat.setText("Please Wait...");
            }
            if(i==1000)
            {
                Thread.sleep(100);
                lblStat.setText("Loading Police Station Management System...");
            }
            if(i==1200)
            {
                lblStat.setText("Please Wait...");
            }
            if(i==1600)
            {
                lblStat.setText("Almost Done...");
            }
            if(i==1980)
            {
                lblStat.setText("Done");
            }
            if(i==2000)
            {
                this.dispose();
                LoginPage lp=new LoginPage();
                lp.setVisible(true);
            }

        }
        catch(Exception e){}

    }
} 

public static void main(String[] args) {  
    FlashScreen fs=new FlashScreen();  
    fs.setVisible(true);  
    fs.iterate();  
}  
}  



**LoginPage.java**

package crimeManagement;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.border.*;

public class LoginPage extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JFrame frame;
    private JTextField txtUserName;
    private JPasswordField txtPass;

    public static void main(String[] args) {

                    LoginPage window = new LoginPage();
                    window.frame.setVisible(true);

    }





    public LoginPage() {
        frame = new JFrame();
        frame.setResizable(false);
        frame.getContentPane().setBackground(SystemColor.inactiveCaption);
        frame.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 16));
        frame.setBounds(100, 100, 554, 410);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblLoginType = new JLabel("Login Type");
        lblLoginType.setFont(new Font("Tahoma", Font.PLAIN, 16));
        lblLoginType.setBounds(97, 53, 99, 20);
        frame.getContentPane().add(lblLoginType);

        JLabel lblUsename = new JLabel("User Name");
        lblUsename.setFont(new Font("Tahoma", Font.PLAIN, 16));
        lblUsename.setBounds(97, 177, 99, 26);
        frame.getContentPane().add(lblUsename);

        JLabel lblPaaword = new JLabel("Password");
        lblPaaword.setFont(new Font("Tahoma", Font.PLAIN, 16));
        lblPaaword.setBounds(97, 223, 99, 26);
        frame.getContentPane().add(lblPaaword);

        JPanel panel = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel.getLayout();
        panel.setBackground(SystemColor.inactiveCaptionBorder);
        panel.setBounds(210, 47, 143, 93);
        frame.getContentPane().add(panel);
        TitledBorder tb=new TitledBorder( "Login");
        tb.setTitleJustification(TitledBorder.CENTER);
        tb.setTitlePosition(TitledBorder.CENTER);

        panel.setBorder(BorderFactory.createTitledBorder(tb));


        JRadioButton rdbAdmin = new JRadioButton("Admin");
        rdbAdmin.setBackground(SystemColor.inactiveCaption);
        rdbAdmin.setFont(new Font("Tahoma", Font.PLAIN, 13));
        rdbAdmin.setSelected(true);
        panel.add(rdbAdmin);

        JRadioButton rdbOthers = new JRadioButton("Others");
        rdbOthers.setBackground(SystemColor.inactiveCaption);
        rdbOthers.setFont(new Font("Tahoma", Font.PLAIN, 13));
        panel.add(rdbOthers);

        txtUserName = new JTextField();
        txtUserName.setBackground(UIManager.getColor("TextField.background"));
        txtUserName.setBounds(210, 177, 158, 26);
        frame.getContentPane().add(txtUserName);
        txtUserName.setColumns(30);

        JButton btnLogin = new JButton("Login");
        btnLogin.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnLogin.setBounds(210, 286, 71, 23);
        frame.getContentPane().add(btnLogin);

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        btnExit.setFont(new Font("Tahoma", Font.PLAIN, 14));
        btnExit.setBounds(297, 286, 71, 23);
        frame.getContentPane().add(btnExit);

        txtPass = new JPasswordField();
        txtPass.setBounds(210, 226, 158, 26);
        frame.getContentPane().add(txtPass);
    }

}
1个回答

4
您正在显示错误的JFrame。是的,LoginPage扩展了JFrame,并且您将其显示出来,但是您没有向其中添加任何组件,而是将所有组件添加到该类的私有JFrame字段中,名称为frame
一个快速的解决方案是更改您的LoginPage类,使其不再扩展JFrame,然后给这个类一个公共的getFrame()方法:
public JFrame getFrame() {
    return frame;
}

当需要显示它时,调用

this.dispose();
LoginPage lp = new LoginPage();
// lp.setVisible(true);
lp.getFrame().setVisible(true);

虽然如此,你的代码仍存在一些严重的线程问题,最终您需要解决这些问题,包括避免在可能被调用Swing事件线程的代码中调用Thread.sleep()

另外,请查看使用多个JFrames:好还是坏的实践? 以了解为什么在应用程序中显示一堆JFrames经常是不好的实践,并了解如何解决这些问题。

其他问题包括使用空布局。 是的,它们可能似乎是快速创建复杂GUI的简单方法-直到您尝试在另一个平台上显示GUI并发现它们看起来不太好,或者必须增强,调试或更改它,并发现它非常棘手且容易出错。 最好使用布局管理器。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class FlashScreenTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame mainFrame = new JFrame("Main App");
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.add(new MainAppPanel());
            mainFrame.pack();
            mainFrame.setLocationRelativeTo(null);

            FlashScreenPanel dialogPanel = new FlashScreenPanel();
            JDialog dialog = new JDialog(mainFrame, "Flash Screen", ModalityType.APPLICATION_MODAL);
            dialog.add(dialogPanel);
            dialog.pack();
            dialog.setLocationRelativeTo(null);

            dialogPanel.startProgress();
            dialog.setVisible(true);

            mainFrame.setVisible(true);
        });
    }
}

class FlashScreenPanel extends JPanel {
    public static final String LOADING = "Loading...";
    public static final String PLEASE_WAIT = "Please Wait...";
    public static final String LOADING_POLICE_STATION = "Loading Police Station...";
    public static final String ALMOST_DONE = "Almost Done...";
    public static final String DONE = "Done";
    private static final int TIMER_DELAY = 50;

    private JProgressBar jb = new JProgressBar(0, 2000);
    private JLabel statusLabel = new JLabel("", SwingConstants.CENTER);

    public FlashScreenPanel() {
        setPreferredSize(new Dimension(800, 400));

        statusLabel.setForeground(Color.CYAN);
        statusLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
        jb.setStringPainted(true);
        JPanel bottomPanel = new JPanel(new BorderLayout(20, 20));
        bottomPanel.add(jb, BorderLayout.PAGE_START);
        bottomPanel.add(statusLabel, BorderLayout.CENTER);

        int eb = 40;
        setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
        setLayout(new GridLayout(0, 1));
        add(new JLabel()); // dummy component to move prog bar lower
        add(bottomPanel);
    }

    public void startProgress() {
        statusLabel.setText(LOADING);
        new Timer(TIMER_DELAY, new ActionListener() {
            private int i = 0;
            @Override
            public void actionPerformed(ActionEvent e) {
                i += 20;
                jb.setValue(i);
                if (i == 500) {
                    statusLabel.setText(PLEASE_WAIT);
                } else
                if (i == 1000) {
                    statusLabel.setText(LOADING_POLICE_STATION);
                } else
                if (i == 1200) {
                    statusLabel.setText(PLEASE_WAIT);
                } else
                if (i == 1600) {
                    statusLabel.setText(ALMOST_DONE);
                } else
                if (i == 1980) {
                    statusLabel.setText(DONE);
                } else
                if (i == 2000) {
                    ((Timer) e.getSource()).stop();
                    Window win = SwingUtilities.getWindowAncestor(FlashScreenPanel.this);
                    win.dispose();
                }
            }
        }).start();
    }
}

class MainAppPanel extends JPanel {
    public MainAppPanel() {
        setPreferredSize(new Dimension(600, 400));
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接