Java使用者介面之視窗和架構

來源:互聯網
上載者:User

標籤:

視窗和架構都是能夠在使用者介面顯示,並且可以容納其他組件的容器,這些容器分別使用swing中的JWindow和JFrame類建立

視窗:一種簡單的容器,不像常規圖形化使用者介面那樣,在頂端有標題和按鈕;

架構:包含使用者運行軟體時希望看到的所有常見的視窗特性,如關閉按鈕、最大化和最小化按鈕等。

建立架構時,必須在框剪的建構函式中執行如下幾種操作:

  1.調用父類JFrame的建構函式;

    ---super();

  2.設定架構的標題;

    ---super("Title")或者setTitle("Title");

  3.設定架構的大小;

    ---setSize(350, 125);

  4.設定架構的外觀;

    ---調用UIManager.setLookAndFeel()方法

      --將Nimbus設定為外觀UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

  5.定義使用者關閉架構時應執行的操作

    ---setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);按鈕被單擊時退出程式

    ---setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);關閉架構並銷毀架構對象,但應用程式繼續運行

    ---setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);讓架構開啟並繼續運行

    ---setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);關閉架構並繼續運行

  6.顯示架構

    ---setVisible(true);

Demo:

 1 package com.swingdemo.demo; 2  3 import javax.swing.JFrame; 4 import javax.swing.UIManager; 5  6 public class SalutonFrame extends JFrame { 7  8     private static final long serialVersionUID = 1L; 9     10     public SalutonFrame() {11 12         super("Saluton mondo!");13         setLookAndFeel();14         setSize(350, 100);15         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);16         setVisible(true);17         18     }19 20     /**21      * 將Nimbus指定為架構的外觀22      * UIManager.setLookAndFeel()方法設定GUI的外觀23      */24     private void setLookAndFeel() {25 26         try{27             UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");28         }catch(Exception e){29             e.printStackTrace();30         }31         32     }33 34     public static void main(String[] args) {35         36         SalutonFrame sal = new SalutonFrame();37         38     }39     40 }
View Code

 

Java使用者介面之視窗和架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.