Java實現軟體運行時啟動資訊視窗的方法_java

來源:互聯網
上載者:User

本文執行個體形式詳述了Java實現一個程式運行時的啟動視窗效果,如常用的Microsoft Word、 Borland JBuilder 等,這樣的視窗稱為資訊視窗。使用資訊視窗的好處是可以使使用者在等待軟體主介面出現前的一段時間中得知軟體運行狀態。本例將示範如何來實現資訊視窗,當開啟程式時,資訊視窗先顯示,並在視窗上倒計時,直到“waiting 0”時,關閉該視窗,顯示程式的主視窗。

該功能的主要實現方法如下:

一般來說,大多數的資訊視窗是沒有標題列的,因此資訊視窗不能由繼承JFrame 類來實現,一種簡單的做法是通過繼承JWindow 來實現(當然繼承Window 類也可以,但一個原則是盡量使用swing 中的介面
類)。另外,本例用到java.awt 包中的MediaTracker 類。使用該類的好處是可以更好地管理程式中要使用的圖片,同時還可以保證圖片和介面同時顯示,避免了視窗顯示後很久才顯示圖片的缺點。

具體操作步驟如下:

1.建立一個Project,取名為JSpleshWindowDemo,其他設定按預設值。

2.建立一個Application ,取名為JSpleshWindowDemo,主視窗取名為MainFrame,主視窗標題取名為JSpleshWindowDemo。

3.先來編寫資訊視窗的代碼。建立一個新類SpleshWindow.java,繼承java.swing.JWindow類。在SpleshWindow 類中,定義新的屬性,代碼如下:

private String statusStr=null; //資訊視窗中要顯示的資訊private Image logoImg=null; //資訊視窗中的顯示圖片

4.向構造方法中添加代碼,載入圖片並初始化表單,實現代碼如下:

public SpleshWindow(JFrame owner) { //以JFrame 對象為參數,可以是資訊視窗和主視窗互動super( owner );// 載入圖片logoImg=getToolkit().getImage( ClassLoader.getSystemResource("images/splesh.jpg") );// 等待圖片載入完成java.awt.MediaTracker tracker=new java.awt.MediaTracker( this ); //建立一個MediaTracker 對象tracker.addImage( logoImg , 0 ); //將圖片放入MediaTracker 對象中,序號為0try{ //等待直到圖片載入完成tracker.waitForAll();}catch ( InterruptedException e ) {e.printStackTrace();}// 設定資訊表單在螢幕上的顯示位置setLocation( getToolkit().getScreenSize().width/2 - logoImg.getWidth(this)/2 , getToolkit().getScreenSize().height/2 -logoImg.getHeight(this)/2 );setSize( logoImg.getWidth(this) , logoImg.getHeight(this) ); // 設定視窗大小}

5.編寫設定顯示資訊的方法,代碼如下:

public void setStatus( String status ){statusStr=status;paint( getGraphics() ); // 重畫視窗來更新資訊視窗中的顯示資訊}

6.重設paint()方法來繪製圖片和顯示資訊的方法,代碼如下:

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 類,實現java.lang.Runnable 介面,並定義新的屬性,設定如下:

private SpleshWindow spleshWindow=null;

8.向MainFrame 類的初始化方法中,添加運行資訊視窗的代碼,實現代碼如下:

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");//建立新的線程,運行資訊視窗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 windowspleshWindow.setStatus( "Waiting "+(35-(long)(System.currentTimeMillis()/1000)+(long)(x/1000)) );}//關閉資訊表單if ( spleshWindow!=null ) {spleshWindow.dispose();spleshWindow=null;}}

9.編寫MainFrame 類的run()方法,如下所示:

public void run() {//建立一個資訊表單並顯示spleshWindow=new SpleshWindow( this );spleshWindow.show();// throw new java.lang.UnsupportedOperationException("Method run() not yet implemented.");}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.