有沒有足夠的去從你的問題。
如果連接到任何東西或者如何啓動/使用Thread
,則不會顯示如何使用啓動畫面。
所以這個問題可能是什麼...
除了一切VishalK已經指出的那樣,我想補充public static Thread DrawSplash = new Thread(new Splasher())
是一個壞主意。您不應該使用static
Thread
s是線程是不可重入的,也就是說,您可以運行兩次相同的線程。
這是一個小例子證明了「衰落」閃屏,採用了多項搖擺Timer
小號
這裏假設你正在使用的Java 7,有去使它成爲Java 6的工作,我我沒有發佈該代碼。
public class TestSplashScreen01 {
public static void main(String[] args) {
new TestSplashScreen01();
}
public TestSplashScreen01() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
SplashScreen splash = new SplashScreen();
splash.start();
}
});
}
public class SplashScreen extends JWindow {
private SplashPane splash;
public SplashScreen() {
setBackground(new Color(0, 0, 0, 0));
splash = new SplashPane();
add(splash);
pack();
setLocationRelativeTo(null);
}
public void start() {
splash.start();
}
public class SplashPane extends JPanel {
private BufferedImage splash;
private Timer timer;
private float alpha = 0f;
private int duration = 1000;
private long startTime = -1;
public SplashPane() {
try {
splash = ImageIO.read(getClass().getResource("/res/SokahsScreen.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
timer = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fadeOut();
}
});
timer.setRepeats(false);
}
protected void fadeOut() {
Timer fadeInTimer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
long now = System.currentTimeMillis();
long runTime = now - startTime;
alpha = 1f - ((float) runTime/(float) duration);
if (alpha <= 0.01f) {
alpha = 0f;
((Timer) (e.getSource())).stop();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
dispose();
}
});
}
repaint();
}
});
startTime = System.currentTimeMillis();
fadeInTimer.setRepeats(true);
fadeInTimer.setCoalesce(true);
fadeInTimer.start();
}
protected void fadeIn() {
Timer fadeInTimer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
long now = System.currentTimeMillis();
long runTime = now - startTime;
alpha = (float) runTime/(float) duration;
if (alpha >= 1f) {
alpha = 1f;
((Timer) (e.getSource())).stop();
timer.start();
}
repaint();
}
});
startTime = System.currentTimeMillis();
fadeInTimer.setRepeats(true);
fadeInTimer.setCoalesce(true);
fadeInTimer.start();
}
public void start() {
if (!SplashScreen.this.isVisible()) {
alpha = 0f;
SplashScreen.this.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
fadeIn();
}
});
}
}
@Override
public Dimension getPreferredSize() {
return splash == null ? super.getPreferredSize() : new Dimension(splash.getWidth(), splash.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (splash != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(alpha));
int x = (getWidth() - splash.getWidth())/2;
int y = (getHeight() - splash.getHeight())/2;
g2d.drawImage(splash, x, y, this);
g2d.dispose();
}
}
}
}
}
NB:
這個例子的問題是,一旦你打電話開始時,程序將繼續執行,這將需要某種聽者告訴有關方面,當閃屏已完成。
或者,你可以使用一個未加裝飾的模式JDialog
我們不知道你如何實際使用這個類。發佈證明問題的[SSCCE](http://sscce.org/)。另外,爲什麼不使用JLabel來顯示圖像而不是自定義繪畫? – camickr 2013-02-16 21:04:57
http://i.imgur.com/oVXif7c.png究竟如何,我會用一個JLabel – Jerba 2013-02-16 21:14:11
@Jerba'#的JLabel setIcon' – MadProgrammer 2013-02-16 22:09:41