本文實(shí)例形式詳述了Java實(shí)現(xiàn)一個(gè)程序運(yùn)行時(shí)的啟動(dòng)窗口效果,如常用的Microsoft Word、 Borland JBuilder 等,這樣的窗口稱為信息窗口。使用信息窗口的好處是可以使用戶在等待軟件主界面出現(xiàn)前的一段時(shí)間中得知軟件運(yùn)行狀態(tài)。本例將演示如何來(lái)實(shí)現(xiàn)信息窗口,當(dāng)打開程序時(shí),信息窗口先顯示,并在窗口上倒計(jì)時(shí),直到“waiting 0”時(shí),關(guān)閉該窗口,顯示程序的主窗口。
該功能的主要實(shí)現(xiàn)方法如下:
一般來(lái)說(shuō),大多數(shù)的信息窗口是沒有標(biāo)題欄的,因此信息窗口不能由繼承JFrame 類來(lái)實(shí)現(xiàn),一種簡(jiǎn)單的做法是通過繼承JWindow 來(lái)實(shí)現(xiàn)(當(dāng)然繼承Window 類也可以,但一個(gè)原則是盡量使用swing 中的界面
類)。另外,本例用到j(luò)ava.awt 包中的MediaTracker 類。使用該類的好處是可以更好地管理程序中要使用的圖片,同時(shí)還可以保證圖片和界面同時(shí)顯示,避免了窗口顯示后很久才顯示圖片的缺點(diǎn)。
具體操作步驟如下:
1.新建一個(gè)Project,取名為JSpleshWindowDemo,其他設(shè)置按默認(rèn)值。
2.新建一個(gè)Application ,取名為JSpleshWindowDemo,主窗口取名為MainFrame,主窗口標(biāo)題取名為JSpleshWindowDemo。
3.先來(lái)編寫信息窗口的代碼。新建一個(gè)新類SpleshWindow.java,繼承java.swing.JWindow類。在SpleshWindow 類中,定義新的屬性,代碼如下:
1
2
|
private String statusStr= null ; //信息窗口中要顯示的信息 private Image logoImg= null ; //信息窗口中的顯示圖片 |
4.向構(gòu)造方法中添加代碼,加載圖片并初始化窗體,實(shí)現(xiàn)代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public SpleshWindow(JFrame owner) { //以JFrame 對(duì)象為參數(shù),可以是信息窗口和主窗口交互 super ( owner ); // 加載圖片 logoImg=getToolkit().getImage( ClassLoader.getSystemResource( "images/splesh.jpg" ) ); // 等待圖片加載完成 java.awt.MediaTracker tracker= new java.awt.MediaTracker( this ); //創(chuàng)建一個(gè)MediaTracker 對(duì)象 tracker.addImage( logoImg , 0 ); //將圖片放入MediaTracker 對(duì)象中,序號(hào)為0 try { //等待直到圖片加載完成 tracker.waitForAll(); } catch ( InterruptedException e ) { e.printStackTrace(); } // 設(shè)置信息窗體在屏幕上的顯示位置 setLocation( getToolkit().getScreenSize().width/ 2 - logoImg.getWidth( this )/ 2 , getToolkit().getScreenSize().height/ 2 - logoImg.getHeight( this )/ 2 ); setSize( logoImg.getWidth( this ) , logoImg.getHeight( this ) ); // 設(shè)置窗口大小 } |
5.編寫設(shè)置顯示信息的方法,代碼如下:
1
2
3
4
|
public void setStatus( String status ){ statusStr=status; paint( getGraphics() ); // 重畫窗口來(lái)更新信息窗口中的顯示信息 } |
6.重置paint()方法來(lái)繪制圖片和顯示信息的方法,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
public void paint(Graphics g) { /**@todo Override this java.awt.Component method*/ super .paint(g); //繪制圖片 if ( logoImg!= null ) g.drawImage( logoImg , 0 , 0 , this ); //繪制顯示信息 if ( statusStr!= null ){ g.setColor(Color.red); g.drawString( statusStr , 240 , getSize().height- 250 ); } } |
7.編寫MainFrame 類,實(shí)現(xiàn)java.lang.Runnable 接口,并定義新的屬性,設(shè)置如下:
1
|
private SpleshWindow spleshWindow= null ; |
8.向MainFrame 類的初始化方法中,添加運(yùn)行信息窗口的代碼,實(shí)現(xiàn)代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this .getContentPane(); contentPane.setLayout(borderLayout1); this .setSize( new Dimension( 400 , 300 )); this .setTitle( "JSpleshWindowDemo" ); //創(chuàng)建新的線程,運(yùn)行信息窗口 Thread t = new Thread( this ); t.start(); // 等待信息窗口顯示 try { t.join(); } catch ( InterruptedException e ){ e.printStackTrace() ; } // 向信息窗體中顯示消息 long x=System.currentTimeMillis(); while ( System.currentTimeMillis()-x < 35000 ) { System.out.print( "Waiting " +( 35000 -System.currentTimeMillis()+x+ " \r" ) ); // you can set status string in splash window spleshWindow.setStatus( "Waiting " +( 35 -( long )(System.currentTimeMillis()/ 1000 )+( long )(x/ 1000 )) ); } //關(guān)閉信息窗體 if ( spleshWindow!= null ) { spleshWindow.dispose(); spleshWindow= null ; } } |
9.編寫MainFrame 類的run()方法,如下所示:
1
2
3
4
5
6
|
public void run() { //新建一個(gè)信息窗體并顯示 spleshWindow= new SpleshWindow( this ); spleshWindow.show(); // throw new java.lang.UnsupportedOperationException("Method run() not yet implemented."); } |