2016-11-25 78 views
2

我创建了FlashScreen.java作为加载屏幕,由JProgressBar组成。 我想在完成进度条百分比后关闭当前窗口并打开新窗口。 我做到了,但是在下一个窗口中关闭第一个窗口后,窗口中没有组件。空的窗口正在打开。如何在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); 
    } 

} 

回答

2

你显示错误的JFrame。是的,LoginPage扩展了JFrame,并且是显示它,但不添加任何组件,而是将所有组件添加到名为frame的类的专用JFrame字段。

一个快速的解决办法是改变你的LoginPage类,以便它不会扩展的JFrame,然后给这个类中的公共getFrame()方法:

public JFrame getFrame() { 
    return frame; 
} 

和想要表现出来的时候,调用

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

但有这样的说明,您的代码仍然存在一些严重的线程问题,您最终需要解决这些问题,包括尝试避免在可能在Swing事件线程上调用的代码中调用Thread.sleep()

此外,请查看The Use of Multiple JFrames: Good or Bad Practice?,了解为什么在您的应用中显示一堆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)); 
    } 
}