其实程序启动提示界面的制作,主要是为了达到更好的用户体验(因为程序启动时需要加载一些播放器信息,反应速度慢的话,用户就不知道程序是否在启动),同时启动提示界面在显示同时加载必要的信息。
在这个模块的制作过程中主要要解决的问题是,如何去构造一个不规则窗口。要解决不规则窗口这个问题的办法有很多种(我的程序里采用了我所知道的做法中相对较好的一种,自认为是比较容易理解和实现的)。具体的思想是:若窗口有装饰则去掉装饰(JWindow没,JFrame等就有),接下来就要使得窗口背景变透明,再接下来就是在透明的窗口里画上自己想要的东西了,具体画什么,怎么画就要自己选择了(自己设计绘制或者通过一些控件让系统自己绘制,在这里本人选择的时候者,通过JLabel让系统绘制)。
下面是我的代码:
package windows.OrdinaryWindow;
import handler.SizeUtils;
import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.Timer;
import source.Resource;
import Library.panel.ImagePanel;
import Library.panel.TextPanel;
import com.sun.awt.AWTUtilities;
public class LoadingMsgWindow extends JWindow {
/**
*
*/
private static final long serialVersionUID = 1L;
private MainWindow mainWindow;
private JLabel center;
private TextPanel anthor;
private ImagePanel versions;
protected Timer timer;
private int i = 0;
public LoadingMsgWindow(MainWindow mainWindow) {
this.mainWindow = mainWindow;
this.setAlwaysOnTop(true);
AWTUtilities.setWindowOpaque(this, false);
this.toFront();
this.initialize();
this.assignComponent();
}
private void initialize() {
this.setLayout(new BorderLayout());
this.setSize(800, 500);
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
this.setLocation(
(int) (SizeUtils.getScreenSize().getWidth() - this.getWidth()) / 2,
(int) (SizeUtils.getScreenSize().getHeight() - this.getHeight()) / 2 - 50);
center = new JLabel(new ImageIcon(
"images\\CommonImages\\ MusicSign1.png"));
center.setIcon(new ImageIcon("images\\CommonImages\\MusicSign" + (i++)
+ ".png"));
anthor = new TextPanel(Resource.string.author);
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
center.setIcon(new ImageIcon("images\\CommonImages\\MusicSign"
+ i + ".png"));
if (mainWindow.isReady()) {
destroy();
mainWindow.start();
} else if (i < 4) {
i++;
timer.restart();
} else
timer.restart();
}
});
timer.setRepeats(false);
timer.start();
}
private void assignComponent() {
center.setLayout(null);
versions = new ImagePanel("images\\versions.png");
versions.setBounds(500, 250, 150, 150);
center.add(versions);
anthor.setBounds(20, 400, 760, 50);
anthor.setStroke(true);
/* 添加显示作者的信息*/
center.add(anthor);
this.add(center);
}
public void start() {
this.setVisible(true);
}
public void destroy() {
this.setVisible(false);
this.dispose();
}
}
效果图: