Jprogressbar完成后如何打开新窗口



我创建了Flashscreen.java作为加载屏幕由Jprogressbar组成。我希望在ProgressBar百分比完成后,应关闭当前窗口,并且应该打开新窗口。我做到了,但是在下一个窗口中关闭第一个窗口后,窗口中没有组件。空窗口正在打开。

这是代码: 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);
    }
}

您正在显示错误 jframe。是的,登录页扩展了jframe,是的,您可以显示它,但是您不添加任何组件,而是将所有组件添加到名为frame的类的私有Jframe字段中。

快速解决方案是更改您的登录页类,以免扩展Jframe,然后给该类别提供公共getFrame()方法:

public JFrame getFrame() {
    return frame;
}

并且在想显示时,请致电

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

但是话虽如此,您的代码仍然存在一些严重的线程问题,其中您最终需要修复,包括试图避免在摇摆事件线程中调用的代码中调用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));
    }
}

相关内容

  • 没有找到相关文章

最新更新