私は、接続されたUSBデバイスへの接続を開始する完全なGUIを持っています。スタートアップの手順は約3秒かかります。その間、私はプログラムが開始していることをユーザーに知らせるスプラッシュ画面を表示したい。私は "SplashFrame"クラス(JFrameの拡張)を作成し、2つのラベルを持つパネルを作成し、フレームに貼り付けました。新しいものを開く前にJFrameを完全にロードする
しかし、SplashFrameは、他の「メイン」フレームのセットアップ手順が完了した時点でのみ、完全にロードされるという問題があります。これは、意図的にSplashFrameを閉じる前に、どちらのラベルも読み込めないことを意味します。メインフレームを起動する前にSplashFrameを完全にロードするにはどうすればよいですか?
これは私のメインの方法
private static JFrame frame;
private static SplashFrame splash;
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
splash = new SplashFrame();
long startTime = System.currentTimeMillis();
frame = new CFSMainFrame();
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("Total time: " + ((float)totalTime/1000) + " seconds!");
splash.closeFrame();
}
});
}
であり、これは私がやってみました(私は自分自身を日食使用しています)はNetbeansを使用してチュートリアルの膨大な量を見た後、私のsplashFrame
public class SplashFrame extends JFrame{
private static JFrame frame;
public SplashFrame(){
frame = new JFrame("Loading...");
frame.setLayout(new BorderLayout());
frame.setSize(300, 150);
ImageIcon img = new ImageIcon("resources/mini_logo.png");
frame.setIconImage(img.getImage());
JPanel pane = splashPanel();
frame.add(pane);
createAndShowGUI();
}
private JPanel splashPanel(){
JPanel pane = new JPanel(new BorderLayout());
ImageIcon loadingImg = new ImageIcon("resources/logo.png");
JLabel imageLabel = new JLabel();
imageLabel.setIcon(loadingImg);
JLabel loadingLabel = new JLabel("Loading");
loadingLabel.setHorizontalAlignment(SwingConstants.CENTER);
loadingLabel.setVerticalAlignment(SwingConstants.CENTER);
loadingLabel.setPreferredSize(new Dimension(150, 165));
pane.add(imageLabel, BorderLayout.CENTER);
pane.add(loadingLabel, BorderLayout.PAGE_END);
return pane;
}
private void createAndShowGUI(){
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
public void closeFrame(){
frame.setVisible(false);
frame.dispose();
}
[オラクルチュートリアル:どのようにスプラッシュ画面を作成する](https://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html )。 –