2013-12-21 198 views
1

我需要弄清楚在這段代碼中我需要調整它來移動「Splash Screen !!!」這一行。到屏幕中間,並可能使它變大。我不確定這是什麼代碼,並且它讓我瘋狂。在Java中的飛濺屏幕

import java.awt.*; 
import javax.swing.*; 

public class SplashScreen extends JWindow { 

private int duration; 

public SplashScreen(int d) { 
    duration = d; 
} 

// A simple little method to show a title screen in the center 
// of the screen for the amount of time given in the constructor 
public void showSplash() { 

    JPanel content = (JPanel)getContentPane(); 
    content.setBackground(Color.blue); 

    // Set the window's bounds, centering the window 
    int width = 700; 
    int height = 450; 
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
    int x = (screen.width-width)/2; 
    int y = (screen.height-height)/2; 
    setBounds(x,y,width,height); 

    // Build the splash screen 
    JLabel label = new JLabel(new ImageIcon("java-tip.gif")); 
    JLabel copyrt = new JLabel 
     ("Splash Screen!!!", JLabel.CENTER); 
    copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12)); 
    content.add(label, BorderLayout.CENTER); 
    content.add(copyrt, BorderLayout.SOUTH); 
    Color oraRed = new Color(200, 50, 20, 255); 
    content.setBorder(BorderFactory.createLineBorder(oraRed, 10)); 

    // Display it 
    setVisible(true); 

    // Wait a little while, maybe while loading resources 
    try { Thread.sleep(duration); } catch (Exception e) {} 

    setVisible(false); 
} 
public void showSplashAndExit() { 
    showSplash(); 
    System.exit(0); 

} 

public static void main(String[] args) { 

    // Throw a nice little title page up on the screen first 
    SplashScreen splash = new SplashScreen(10000); 

    // Normally, we'd call splash.showSplash() and get on 
    // with the program. But, since this is only a test... 
    splash.showSplashAndExit(); 
} 
} 

我不知道爲什麼,但在這個論壇添加代碼功能總是讓它看起來很奇怪,而且沒有正確縮進。

+0

小便!男人你必須有我的帖子出現與速度大聲笑 – Opjeezzeey

+0

我不完全確定如何添加一個搖擺計時器。啓動畫面編譯並運行得非常好。我期待的是完全美容。 – Opjeezzeey

+1

@Opjeezzeey它「出現」運行良好,但相信我們,它不.. – MadProgrammer

回答

3

這可能有幾種方法,但讓我們保持簡單。

基本上,這樣做是將label(背景)圖像添加到內容窗格的中心位置。然後,它適用於一個BorderLayoutlabel,並增加了對copyrtlabel中心位置...

public void showSplash() { 

    JPanel content = (JPanel) getContentPane(); 
    content.setBackground(Color.blue); 

    // Set the window's bounds, centering the window 
    int width = 700; 
    int height = 450; 
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
    int x = (screen.width - width)/2; 
    int y = (screen.height - height)/2; 
    setBounds(x, y, width, height); 

    // Build the splash screen 
    JLabel label = new JLabel(new ImageIcon("java-tip.gif")); 
    JLabel copyrt = new JLabel("Splash Screen!!!", JLabel.CENTER); 

    content.add(label, BorderLayout.CENTER); 

    // Fun starts here...  
//  copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12)); 
//  content.add(copyrt, BorderLayout.SOUTH); 

    label.setLayout(new BorderLayout()); 
    Font font = copyrt.getFont(); 
    copyrt.setFont(font.deriveFont(Font.BOLD, 24f)); 
    label.add(copyrt); 

    Color oraRed = new Color(200, 50, 20, 255); 
    content.setBorder(BorderFactory.createLineBorder(oraRed, 10)); 

    // Display it 
    setVisible(true); 

    // Don't do this, as it will cause the EDT to be stopped. Instead 
    // setup some kind of callback that can tell when the resources are 
    // loaded and start the rest of the application from there... 
    // Wait a little while, maybe while loading resources 
    //try { 
    // Thread.sleep(duration); 
    //} catch (Exception e) { 
    //} 

    //setVisible(false); 
} 

這也是一個不錯的主意做EDT範圍內任何可能阻止其進一步處理事件,像使用sleep或長時間運行/無限循環。

所有UI交互都應該在EDT的上下文中執行,所有長時間運行或潛在阻塞的任務應該在單獨的線程上運行。

看看Concurrency in Swing更多細節

更新

如果我嘗試運行你的榜樣,啓動畫面永遠不會出現,因爲Thread.sleep是防止被顯示在屏幕上。事實上,你「可能」得到它的工作實際上是一種僥倖,因爲你可能從「主」線程調用showSplash方法,而不是EDT。

相反,如果我用SwingWorker替換Thread.sleep,我不僅可以看到啓動畫面,還可以控制進度更新和處理啓動畫面的時間安排等額外好處...

例如...

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Toolkit; 
import javax.swing.BorderFactory; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JWindow; 
import javax.swing.SwingWorker; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class SplashDemo extends JWindow { 

    public static void main(String[] args) { 
     new SplashDemo(); 
    } 

    public SplashDemo() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       showSplash(); 

      } 
     }); 
    } 

    public void showSplash() { 

     JPanel content = (JPanel) getContentPane(); 
     content.setBackground(Color.blue); 

     // Set the window's bounds, centering the window 
     int width = 700; 
     int height = 450; 
     Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
     int x = (screen.width - width)/2; 
     int y = (screen.height - height)/2; 
     setBounds(x, y, width, height); 

     // Build the splash screen 
     JLabel label = new JLabel(new ImageIcon(getClass().getResource("/java_animated.gif"))); 
     JLabel copyrt = new JLabel("Splash Screen!!!", JLabel.CENTER); 

     content.add(label, BorderLayout.CENTER); 

     label.setLayout(new GridBagLayout()); 
     Font font = copyrt.getFont(); 
     copyrt.setFont(font.deriveFont(Font.BOLD, 24f)); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridwidth = GridBagConstraints.REMAINDER; 
     label.add(copyrt, gbc); 

     ImageIcon wait = new ImageIcon(getClass().getResource("/wait.gif")); 
     label.add(new JLabel(wait), gbc); 

     Color oraRed = new Color(200, 50, 20, 255); 
     content.setBorder(BorderFactory.createLineBorder(oraRed, 10)); 

     // Display it 
     setVisible(true); 
     toFront(); 

     new ResourceLoader().execute(); 
    } 

    public class ResourceLoader extends SwingWorker<Object, Object> { 

     @Override 
     protected Object doInBackground() throws Exception { 

      // Wait a little while, maybe while loading resources 
      try { 
       Thread.sleep(5000); 
      } catch (Exception e) { 
      } 

      return null; 

     } 

     @Override 
     protected void done() { 
      setVisible(false); 
     } 


    } 

} 

如果你願意,我用下面的圖片...

enter image description hereenter image description here

+0

我真的只需要啓動畫面。因此,從最後開始應用程序並不是一個擔心。 但是,我只是刪除評論欄,讓你的補充工作? – Opjeezzeey

+0

如果您使用'Thread.sleep',則您的應用程序將在掛起EDT時「掛起」。更好的方法是啓動應用程序並使用像SwingWorker這樣的資源來加載資源。一旦調用完成(在'SwingWorker'上),您可以關閉啓動畫面並打開主應用程序窗口。 – MadProgrammer

+0

好的。非常感謝 :) – Opjeezzeey

1

您正在使用BorderLayout,這意味着您的容器分爲EAST,WEST NORTH,SOUTH和CENTER。您正在向南添加「閃屏」。這就是爲什麼它不居中:

content.add(copyrt, BorderLayout.SOUTH); 

您需要將佈局管理器更改爲其中一個網格佈局。或者,創建多個面板並將您的內容添加到它們,然後添加到父容器。