1 package com.sshtools.ui.swing; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Color ; 5 import java.awt.Image ; 6 import java.awt.event.ActionEvent ; 7 import java.awt.event.ActionListener ; 8 import javax.swing.ImageIcon ; 9 import javax.swing.JComponent ; 10 import javax.swing.JFrame ; 11 import javax.swing.JLabel ; 12 import javax.swing.JPanel ; 13 import javax.swing.JWindow ; 14 import javax.swing.SwingConstants ; 15 import javax.swing.SwingUtilities ; 16 import javax.swing.Timer ; 17 import javax.swing.border.Border ; 18 19 29 public class SplashWindow 30 extends JWindow 31 implements ActionListener { 32 33 private JPanel mainPanel; 35 private Timer timer; 36 private boolean appDismissed; 37 private boolean timerExpired; 38 39 46 public SplashWindow(JFrame parent, Image img, int timeout) { 47 this(parent, img, timeout, null); 48 } 49 50 58 public SplashWindow(JFrame parent, Image img, int timeout, 59 JComponent accessoryComponent) { 60 super(parent); 61 ImageIcon image = new ImageIcon (img); 63 int w = image.getIconWidth() + 5; 64 int h = image.getIconHeight() + 5; 65 mainPanel = new JPanel (new BorderLayout ()); 67 mainPanel.setOpaque(false); 68 if (accessoryComponent != null) { 70 mainPanel.add(accessoryComponent, BorderLayout.SOUTH); 71 } 73 JLabel p = new JLabel (image); 74 p.setBorder(null); 75 mainPanel.add(p, BorderLayout.CENTER); 76 getContentPane().setLayout(new BorderLayout ()); 78 getContentPane().add(mainPanel); 79 timer = new Timer (0, null); 81 timer.setRepeats(false); 82 timer.setInitialDelay(timeout); 83 pack(); 85 UIUtil.positionComponent(SwingConstants.CENTER, this); 86 } 87 88 93 public void setBackground(Color b) { 94 getContentPane().setBackground(b); 95 mainPanel.setBackground(b); 96 } 97 98 103 public void setBorder(Border b) { 104 mainPanel.setBorder(b); 105 UIUtil.positionComponent(SwingConstants.CENTER, this); 106 } 107 108 113 public Border getBorder() { 114 return mainPanel.getBorder(); 115 } 116 117 120 public void dismiss() { 121 if (!timerExpired) { 122 appDismissed = true; 123 } 124 else { 125 dismissImpl(); 126 } 127 } 128 129 private void dismissImpl() { 130 SwingUtilities.invokeLater(new Runnable () { 131 public void run() { 132 dispose(); 133 134 } 135 }); 136 } 137 138 143 public void actionPerformed(ActionEvent evt) { 144 if (appDismissed) { 145 dismissImpl(); 146 } 147 else { 148 timerExpired = true; 149 } 150 } 151 152 155 public void show() { 156 timer.addActionListener(this); 157 timer.start(); 158 super.show(); 159 } 160 161 164 public void hide() { 165 timer.stop(); 166 timer.removeActionListener(this); 167 super.hide(); 168 } 169 } | Popular Tags |