Java AWT組件開發和Swing界面編程

1、AWT組件開發java

一、AWTdocker

       AWT是抽象窗口工具箱的縮寫,它爲編寫圖形用戶界面提供了用戶接口,經過這個接口就能夠繼承不少方法,省去了不少工做。AWT還能使應用程序更好地同用戶進行交互。編程

       AWT中的容器是一種特殊的組件,他能夠包含其餘組件,便可以把組件方法容器中。Container類是用來存放其餘組件的Component類的子類,Frame類又是Component的子類。Frame類用於建立具備標題欄和邊界的窗口。這裏經過繼承Frame類來創建本身的界面。瀏覽器

 

[java]  view plain  copy
 
 print?
  1. public class test extendsFrame{  
  2.     //建立構造器  
  3.     public test() throws HeadlessException {  
  4.         this.setTitle("第一個窗口程序");  
  5.         //x , y是距離顯示器左上角的距離;w , h是窗口的長寬  
  6.         this.setBounds(100, 100, 250, 250);  
  7.         //或者使用如下方式代替上面的一句代碼  
  8. //        this.setLocation(100, 100);  
  9. //        this.setSize(250 , 250);  
  10.         this.setVisible(true);  
  11.     }  
  12.     public static void main(String[] str) {  
  13.         new test();  
  14.     }  
  15. }  

 

 

       上面是窗口中一些必不可少的東西,下面是一些窗口的基礎應用:網絡

       >setTitle(「窗口」):定義窗口名稱框架

       >add(button):把按鈕添加到窗口中less

       >setBackground(Color):設置窗口的背景顏色jsp

       >setResizable(boolean):設置窗口大小是否能夠改變ide

       >setAlwaysOnTop(boolean):設置窗口是否總在最上面函數

       >setBounds(x, y, w, h):設置窗口起始位置和大小

       >setVisible(boolean):設置窗口可見

       若是想建立多個窗口,只須要在主方法main()中建立多個對象就好了。

二、佈局管理器

       Java中的圖形界面在佈局管理上採用容器和佈局管理相分離的方案,也就是說容器只是把組件放進來,但它無論怎樣放置。至於如何放置須要用到佈局管理器。Java中有幾種佈局管理器,分別是:FlowLayout , BorderLayout, GridLayout和GardLayout。

       1)、FlowLayout

       FlowLayout佈局管理器是默認的佈局管理器,它將組件按照從左到右、從上到下的順序來安排,並在默認狀況下使組件儘可能居中放置。下面的代碼要添加到test()類中:

        this.setLayout(new FlowLayout());

        //將按鈕組件放入容器中

        this.add(new Button("肯定"));

        this.add(new Button("取消"));

       你們能夠經過建立許多按鈕來觀察FlowLayout對組件的擺放規律,下面是使用一個按鈕監聽事件來實現按鈕的添加:

 

[java]  view plain  copy
 
 print?
  1. public class test extendsFrame implements ActionListener{  
  2.     int i;  
  3.     Button b = new Button("Add");  
  4.     public test() throws HeadlessException {  
  5.         this.setTitle("第一個窗口程序");  
  6.         this.setLayout(new FlowLayout());  
  7.         this.add(b);  
  8.         b.addActionListener(this);  
  9.         this.setBounds(100, 100, 250, 250);  
  10.         this.setVisible(true);  
  11.     }  
  12.     @Override  
  13.     public void actionPerformed(ActionEvent e){  
  14.         i++;  
  15.         Button bi = newButton("Button" + i);  
  16.         this.add(bi);  
  17.         this.setVisible(true);  
  18.     }  
  19.     public static void main(String[] str) {  
  20.         new test();  
  21.     }  
  22. }  

 

 

       在本程序中,因爲按鈕的大小不一樣,其總體看起來不太整齊,但這更說明了FlowLayout佈局管理器的規則。

       2)、BorderLayout

       BorderLayout佈局管理器只容許在容器內放置5個組件,這5個組件的位置是由BorderLayout類中的North、South、East、West和Center5個常量來肯定的,他們對應着容器中的上下左右中,用法以下:

       this.add(new Button(「按鈕」) ,BorderLayout.NORTH);

       this.add(new Button(「按鈕」) ,BorderLayout.CENTER);

       組件在BorderLayout中的大小都是能夠改變的。通常狀況下可讓中間區域大一些,並且能夠只用其中幾個區域。

       3)、GridLayout

       GridLayout佈局管理器是矩形網格,在網格中放置組件,每一個網格的高度和寬度都相等,組件的排列順序與FlowLayout相同。組件隨着網格的大小而在水平和垂直方向上拉伸,網格的大小是由容器的大小和建立網格的多少來肯定的。其用法以下:

       this.setLayout(newGridLayout(2 , 3)); //建立一個2行3列的網格

       this.add(new Button(「按鈕」));

       當組件數目大於網格數時,GridLayout保持行數不變而自動增長列數。

       4)、CardLayout

       CardLayout運行在一個組件中每次只顯示一組組件中的某一個,用戶能夠根據須要來選擇使用哪一個組件。CardLayout類提供了以下選擇組件的方法。

       >First(Container p):選擇容器中的第一個組件

       >last(Container p):選擇容器中的最後一個組件

       >next(Container p):選擇容器中當前組件的下一個組件

       >prebvious(Container p):選擇容器中當前組件的上一個組件

       >show(Container p ,String name):選擇容器中指定的組件

       前面幾種很容易理解,而show()方法來選擇容器中指定的組件。它的第一個參數是管理組件的容器,第二個參數是標識要顯示組件字符串,該字符串與將組件添加到容器時使用的字符串相同。其用法以下:

 

[java]  view plain  copy
 
 print?
  1. public class test extendsFrame implements ActionListener{  
  2.     Panel p = new Panel();  
  3.     Button bf = new Button("First");  
  4.     Button bl = new Button("Last");  
  5.     Button bn = new Button("next");  
  6.     Button bp = newButton("previous");  
  7.     Button bg = new Button("Go");  
  8.     TextField tf = new TextField();  
  9.     //設置CardLayout佈局管理器  
  10.     CardLayout cl = new CardLayout();  
  11.     public test() throws HeadlessException {  
  12.         this.setTitle("CardLayout佈局管理器");  
  13.         this.setLayout(null);  
  14.         this.add(p);  
  15.         //定義p面板爲CardLayout佈局管理器  
  16.         p.setLayout(cl);  
  17.         //爲CardLayout佈局管理器添加按鈕  
  18.         for (int i = 1; i <= 10; i++) {  
  19.             Button btemp = newButton("Button" + i);  
  20.             p.add(btemp, "" + i);  
  21.         }  
  22.         //爲每一個組件設置大小位置並添加到容器中  
  23.         p.setBounds(10, 40, 100, 100);  
  24.         this.add(bf);  
  25.         bf.addActionListener(this);  
  26.         bf.setBounds(120, 40, 60, 20);  
  27.         this.add(bl);  
  28.         bl.addActionListener(this);  
  29.         bl.setBounds(120, 70, 60, 20);  
  30.         this.add(bn);  
  31.         bn.addActionListener(this);  
  32.         bn.setBounds(120, 100, 60, 20);  
  33.         this.add(bp);  
  34.         bp.addActionListener(this);  
  35.         bp.setBounds(120, 130, 60, 20);  
  36.         this.add(bg);  
  37.         bg.addActionListener(this);  
  38.         bg.setBounds(60, 160, 40, 20);  
  39.         this.add(tf);  
  40.         tf.setBounds(20, 160, 40, 20);  
  41.         //設置窗口的位置和大小  
  42.         this.setBounds(200, 200, 210, 220);  
  43.         this.setVisible(true);  
  44.     }  
  45.     @Override  
  46.     public void actionPerformed(ActionEvent e){  
  47.         if (e.getSource() == bn) {  
  48.             cl.next(p);  
  49.         }  
  50.         if (e.getSource() == bp) {  
  51.             cl.previous(p);  
  52.         }  
  53.         if (e.getSource() == bf) {  
  54.             cl.first(p);  
  55.         }  
  56.         if (e.getSource() == bl) {  
  57.             cl.last(p);  
  58.         }  
  59.         if (e.getSource() == bg) {  
  60.             cl.show(p, tf.getText().trim());  
  61.             tf.setText("");  
  62.         }  
  63.     }  
  64.     public static void main(String[] args){  
  65.         new test();  
  66.     }  

 

 

三、組件和監聽接口

       組件和監聽接口是分不開的。組件和監聽接口在AWT中有不少,咱們只對複雜的、難以理解的進行講解,其餘的你們能夠經過API自行學習。

       1)、按鈕和ActionListener

       ActionListener是由處理ActionEvent事件的監聽器對象實現的。當單擊按鈕、在文本區域中按回車鍵、選擇菜單項、雙擊列表項都會觸發監聽器。該接口中的方法爲:

       public voidactoinPerformed(ActionEvent e)

       下面是一個實現ActionListener接口的實例:

 

[java]  view plain  copy
 
 print?
  1. public class mytest01extends Frame implements ActionListener{  
  2.     Button b1 = new Button("進入社區");  
  3.     Button b2 = new Button("退出");  
  4.     public mytest01() throws HeadlessException{  
  5.         this.setTitle("論壇");  
  6.         this.add(b1);  
  7.         this.add(b2 , BorderLayout.SOUTH);  
  8.         //爲按鈕添加監聽  
  9.         b1.addActionListener(this);  
  10.         b2.addActionListener(this);  
  11.         this.setBounds(100, 100, 200, 300);  
  12.         this.setVisible(true);  
  13.     }  
  14.     //實現ActionListener接口中的方法  
  15.     public void actionPerformed(ActionEvent e){  
  16.         if (e.getSource() == b1) {  
  17.             System.out.println("進入社區");  
  18.         }  
  19.         else{  
  20.             System.out.println("退出");  
  21.         }  
  22.     }  
  23.     public static void main(String[] args){  
  24.         new mytest01();  
  25.     }  
  26. }  

 

 

       2)、運用WindowListener

       WindowListener是處理WindowEvent事件的對象實現的。這個監聽器肯定了窗口設麼時候被打開、關閉、激活、不激活、最小化和最大化。該接口中的方法有:

       >public voidwindowActivated(WindowEvent e)

       >public voidwindowClosed(WindowEvent e)

       >public voidwindowClosing(WindowEvent e)

       >public voidwindowDeactivated(WindowEvent e)

       >public voidwindowDeiconfied(WindowEvent e)

       >public void windowIconified(WindowEvente)

       >public voidwindowOpened(WindowEvent e)

       這些接口很容易可是有點繁瑣,下面的實例程序很是清楚:

 

[java]  view plain  copy
 
 print?
  1. public class mytest01extends Frame implements WindowListener{  
  2.     public mytest01() throws HeadlessException{  
  3.        this.setTitle("WindowListener");  
  4.         this.addWindowListener(this);  
  5.         this.setBounds(100, 100, 200, 300);  
  6.         this.setVisible(true);  
  7.     }  
  8.     //實現接口中的方法  
  9.     public void windowOpened(WindowEvent e) {  
  10.         System.out.println("打開");  
  11.     }  
  12.     public void windowClosing(WindowEvent e) {  
  13.         System.out.println("菜單關閉");  
  14.         this.dispose();  
  15.     }  
  16.     public void windowClosed(WindowEvent e) {  
  17.         System.out.println("釋放");  
  18.     }  
  19.     public void windowIconified(WindowEvent e){  
  20.         System.out.println("最小化");  
  21.     }  
  22.     public void windowDeiconified(WindowEvente) {  
  23.         System.out.println("最大化");  
  24.     }  
  25.     public void windowActivated(WindowEvent e){  
  26.         System.out.println("激活");  
  27.     }  
  28.     public void windowDeactivated(WindowEvente) {  
  29.         System.out.println("失去焦點");  
  30.     }  
  31.     public static void main(String[] args){  
  32.         new mytest01();  
  33.     }  
  34. }  

 

 

       3)、文本組件和TextListener

       文本組件就像把窗口空白處當作記事本同樣,它是一個用來寫內容的組件。TextListener用來肯定什麼時候文本值改變。該接口還能夠用到不少地方,其接口方法以下:

       public voidtextValueChanged(TextEvent e)

       下面經過實例來了解TextListener監聽事件的使用

 

[java]  view plain  copy
 
 print?
  1. public class mytest01extends Frame implements TextListener{  
  2.     TextArea ta = newTextArea("saasdfgadsfg");  
  3.     public mytest01() throws HeadlessException{  
  4.         this.setTitle("textArea");  
  5.         this.add(ta);  
  6.         //設置文字樣式  
  7.         Font f = new Font("宋體", Font.ITALIC+ Font.BOLD, 50);  
  8.         ta.setFont(f);  
  9.         ta.addTextListener(this);  
  10.         ta.setForeground(Color.red);  
  11.         this.setBounds(100, 100, 300, 300);  
  12.         this.setVisible(true);  
  13.     }  
  14.     @Override  
  15.     public void textValueChanged(TextEvent e) {  
  16.         System.out.println(ta.getText());  
  17.     }  
  18.     public static void main(String[] args){  
  19.         new mytest01();  
  20.     }  
  21. }  

 

 

       上面的這段程序使用TextListener監聽文本的輸入、刪除等操做,就像記事本同樣能夠對文本進行修改、錄入。

2、Swing界面編程

       隨着Java的發展,AWT已經漸漸被淘汰,它已經不能適應發展的須要,不能知足開發功能強大的用戶界面的須要。這時Swing出現了,它是創建在AWT之上的組件集,在不一樣的平臺上都能保持組件的界面樣式,所以獲得了很是普遍的應用。

一、Swing組件庫

       在Swing組件中有許多種組件,它們被封裝在JFC中,下面咱們會對每一種組件進行詳細介紹。Swing包不少,但日常用到的只有javax.swing.*和javax.swing.event.*這兩個包,其餘的不多用到。

       1)、JFC結構

       JFC是Java的基礎類,是Java Foundation Classes的縮寫形式,封裝了一組用於構建圖形用戶界面的組件和特性。JFC包含了圖形用戶界面構建中須要用到的頂級容器(Applet、Dialog、Frame)、普通容器(面板、滾動面板、拆分窗格組件、選項卡插U能給個和工具條等)、特殊容器(InternalFrame、Layeredpane、root pane)、基本組件(button , combo box , list , menu , slider , spinner和textfild)等。

       2)、與AWT的區別

       最大的區別在於Swing組件的實現與本地實現無關。Swing組件比AWT組件具備更多的功能。例如在Swing中添加了按鈕組件和標籤組件,經過繼承來更改Swing組件的行爲和外觀,訪問技術等。

二、Jfram窗口容器

       它定義了一個UI程序的框架,是圖形程序不可缺乏的一部分。它與java.awt.Frame類很相似,它是RootPaneContainer的一種,是頂層的Swing容器。經過繼承jframe,所構建的容器就有一些最基本的組件。例如和關閉按鈕相對應的容器有一個setDefaultCloseOperation(int operation)方法,這是每一個Swing程序都有的,它有四個參數:

       >DO_NOTHING_ON_CLOSE(單擊後無論用)

       >HIDE_ON_CLOSE(單擊後窗口隱藏)

       >DISPOSE_ON_CLOSE(單擊後窗口釋放)

       >EXIT_ON_CLOSE(單擊後退出)

       Jframe是最重要的頂層容器,其顯示效果是一個窗口,帶有標題和尺寸重置角標。能夠在其中添加按鈕、標籤等組件。下面是一個應用JFrame類的基本程序:

 

[java]  view plain  copy
 
 print?
  1. public class test extendsJFrame{  
  2.     public test() throws HeadlessException {  
  3.         this.setLayout(new GridLayout(2, 2));  
  4.         this.setBounds(10, 10, 600, 400);  
  5.         this.setVisible(true);  
  6.         //設置當單機窗口的關閉按鈕時退出  
  7.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  8.     }  
  9.     public static void main(String[] args){  
  10.         new test();  
  11.     }  
  12. }  

 

 

三、經過Icon接口進行圖像操做

       Icon接口用於顯示圖像。在Icon接口中定義了許多方法,這些方法都是圖像應用方面必不可少的。

       在Swing組件中支持圖像顯示。ImageIcon類用來描述圖像。建立Icon對象的方法有3種:

       >new ImageIcon(Image i)

       >new ImageIcon(Stringfilename)(參數爲圖像文件的名稱)

       >new ImageIcon(URL u)(參數爲網絡地址)

       實現Icon接口的方法也有3種:

       >PaintIcon(Graphics)

       >getIconWidth()(設置圖像寬度)

       >getIconHeight()(設置圖像長度)

       JLable是一種既能夠包含文本又能夠包含圖像的控件,該控件不能響應用戶的動做。建立一個JLable的方法以下:

       Jlabel j1 = new JLable(「a」);

       this.add(j1);

       Icon組件能夠實現帶有圖標的按鈕或標籤。Icon是一個固定大小的圖片,一般用於裝飾組件。下面是一個Icon的實例應用:

 

[java]  view plain  copy
 
 print?
    1. public class test extendsJFrame{  
    2.     JLabel j1 = new JLabel("a");  
    3.     JLabel j2 = new JLabel("b");  
    4.     public test() throws HeadlessException {  
    5.         this.setLayout(new GridLayout(2, 2));  
    6.         this.add(j1);  
    7.         this.add(j2);  
    8.         //引入原有圖片  
    9.         ImageIcon i1 = new ImageIcon("D:\\桌面\\桌面\\安卓開發工具\\素材\\圖標\\a1.png");  
    10.         j1.setIcon(i1);  
    11.         //引入自作圖片  
    12.         Icon i2 = new MyIconlmp();  
    13.         j2.setIcon(i2);  
    14.         this.setBounds(10, 10, 600, 400);  
    15.         this.setVisible(true);  
    16.         //設置當單機窗口的關閉按鈕時退出  
    17.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    18.     }  
    19.     //定義一個揮之圖片的類  
    20.     class MyIconlmp implements Icon{  
    21.         public void paintIcon(Component c,Graphics g, int x, int y) {  
    22.             for (int i = 0; i < 3; i++) {  
    23.                 g.setColor(new Color(30*i,50*i, 60*i));  
    24.                 g.fillOval(10, 10 + 100*i, 120,80);  
    25.             }  
    26.         }  
    27.         //設置圖片寬度  
    28.         public int getIconWidth() {  
    29.             return 200;  
    30.         }  
    31.         //設置圖片高度  
    32.         public int getIconHeight() {  
    33.             return 200;  
    34.         }  
    35.     }  
    36.     public static void main(String[] args){  
    37.         new test();  
    38.     }  
    39. }  

四、按鈕

       JButton類用來定義按鈕。JButton類的經常使用方法以下:

       >addActionListener():註冊點擊事件監聽器;

       >setText():設置按鈕文字;

       >setIcon():設置按鈕圖標。

       經過組建的setMnemonic()方法能夠設置組件Mnemonic助記符。經過組件的setTipText能夠設置組建的ToolTip提示信息。

       在Swing中,JButton是有AbstractButton類派生的。AbstractButton類派生了兩個組件:JButton和JtoggleButton組件。JButton是Swing的按鈕,而ToggleButton組件是單選按鈕和複選框的基類。下面是一個按鈕的應用程序。

 

[java]  view plain  copy
 
 print?
  1. public class test extendsJFrame implements ActionListener{  
  2.     JButton jb = new JButton("Clickme!");  
  3.     public test() throws HeadlessException {  
  4.         this.setLayout(new GridLayout(2, 2));  
  5.         this.add(jb);  
  6.         jb.addActionListener(this);  
  7.         jb.setMnemonic('b');//給按鈕設置組件助記符  
  8.         jb.setToolTipText("I am abutton");  
  9.         this.setBounds(10, 10, 600, 400);  
  10.         this.setVisible(true);  
  11.         //設置當單機窗口的關閉按鈕時退出  
  12.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  13.     }  
  14.     public static void main(String[] args){  
  15.         new test();  
  16.     }  
  17.     @Override  
  18.     public void actionPerformed(ActionEvent e){  
  19.         this.jb.setText("Clicked");  
  20.     }  
  21. }  

 

 

五、複選框

       在Swing中,用JcheckBox類來定義複選框。複選框和單選按鈕有點相似,可是一組複選框中能夠有任何數量的複選框被選中。複選框能夠爲每一次的單擊操做添加一個事件。但日常只會監聽事件,由於它們讓客戶來肯定該單擊操做時選中仍是取消選中複選框。

       複選框又被稱爲檢測盒。JcheckBox提供選中/未選中兩種狀態,當用戶單擊複選框時改變複選框原來設置的狀態。

六、彈出式菜單

       JPopupMenu是一種Menu的組件,所以在使用JPopupMenu時都須要一個Container來放置JPopupMenu。

       經過JpopupMenu類能夠定義彈出式菜單,其重要方法有:

       >add(JmenuItem e):(往菜單中增長菜單項)

       >show():(顯示菜單)

       經過JMenuItem類來定義菜單項,經過addActionListener()爲菜單項增長事件處理。

       JpopupMenu是一個可彈出並顯示一系列選項的小窗口,可用於用戶在菜單欄上選擇選項時顯示菜單,還能夠用於當用戶選擇菜單項並激活時顯示「右拉式(pull-right)「菜單。經過下面的程序進一步瞭解相關的知識:

 

[java]  view plain  copy
 
 print?
  1. public class test extendsJFrame {  
  2.     //定義一個彈出式菜單  
  3.     JPopupMenu jpm = new JPopupMenu();  
  4.     public test() throws HeadlessException {  
  5.         this.setLayout(new GridLayout(2, 2));  
  6.         this.setBounds(10, 10, 600, 400);  
  7.         this.setVisible(true);  
  8.         //設置當單機窗口的關閉按鈕時退出  
  9.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  10.         //引入jiaPopMenu()方法  
  11.         this.jiaPopMenu();  
  12.     }  
  13.     public void jiaPopMenu(){  
  14.         JMenuItem item = newJMenuItem("A");  
  15.         //爲A添加監聽事件  
  16.         item.addActionListener(newActionListener() {  
  17.             public voidactionPerformed(ActionEvent e) {  
  18.                 System.out.println("AClicked!!!");  
  19.             }  
  20.         });  
  21.         jpm.add(item);//將彈出項添加到菜單  
  22.         //定義菜單項  
  23.         item = new JMenuItem("B");  
  24.         item.addActionListener(newActionListener() {  
  25.             public voidactionPerformed(ActionEvent e) {  
  26.                 System.out.println("BClicked");  
  27.             }  
  28.         });  
  29.         jpm.add(item);  
  30.         //編寫右鍵彈出單擊事件  
  31.         this.addMouseListener(newMouseAdapter() {  
  32.             public voidmouseReleased(MouseEvent e) {  
  33.                 if (e.isPopupTrigger()) {  
  34.                     jpm.show(e.getComponent(),e.getX(), e.getY());  
  35.                 }  
  36.             }  
  37.         });  
  38.     }  
  39.     public static void main(String[] args){  
  40.         new test();  
  41.     }  
  42. }  

 

 

       使用JPopupMenu組件實現右鍵快捷菜單功能,並使用ActionListener的對象來將菜單激活,當右擊時便可彈出菜單。

七、單選按鈕

       單選按鈕的實質就是在一組按鈕中一次只能有一個按鈕被選中。單選按鈕的外觀相似複選框,可是複選框沒有對能夠選擇的選項數目進行限制。對於每一組的單選按鈕,必須建立一個ButtonGroup對象實例而且將每個單選按鈕添加到該ButtonGroup對象中。下面的實例講解的單選按鈕的基本用法:

 

[java]  view plain  copy
 
 print?
  1. public class test extendsJFrame {  
  2.     //定義兩個單選選項  
  3.     JRadioButton r1 = newJRadioButton("No.1");  
  4.     JRadioButton r2 = newJRadioButton("No.2");  
  5.     //定義一個按鈕組  
  6.     ButtonGroup bg = new ButtonGroup();  
  7.     public test() throws HeadlessException {  
  8.         this.setLayout(new GridLayout(3, 1));  
  9.         this.setBounds(10, 10, 600, 400);  
  10.         this.setVisible(true);  
  11.         //設置當單機窗口的關閉按鈕時退出  
  12.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  13.         //引入jiaPopMenu()方法  
  14.         this.Add();  
  15.     }  
  16.     public void Add(){  
  17.         //將單選按鈕加入按鈕組中  
  18.         bg.add(r1);  
  19.         bg.add(r2);  
  20.         //將單選按鈕加入佈局管理器  
  21.         this.add(r1);  
  22.         this.add(r2);  
  23.         //默認選中r2  
  24.         r2.setSelected(true);  
  25.     }  
  26.     public static void main(String[] args){  
  27.         new test();  
  28.     }  
  29. }  

 

 

八、下拉列表框

       下拉列表框可讓人感受到整個界面很簡潔,在大的網頁中都能感受到這一點。列表能夠有許多選項,因此它們一般被放置在一個滾動窗格中。組合框與下拉列表框類似,區別在於使用組合框時用戶能夠不從列表中選擇項目,還能夠選擇一個項目。在某些版本的組合框中,還能夠輸入本身的選擇,如瀏覽器的地址欄。

       JComboBox方法不少,它們主要用來管理列表中的數據:

       >addItem():添加一個項目到JComboBox

       >get/setSelectedIndex():獲取/設置JComboBox中選中項目的索引

       >get/setSelectedItem():獲取/設置選中的對象

       >removeAllItems():從JComboBox刪除全部對象

       >remoteItem():從JComboBox刪除特定對象

       下面是一個下拉別表框的實例,從中能夠更詳細的瞭解到下拉列表框的使用:

 

[java]  view plain  copy
 
 print?
  1. public class test extendsJFrame {  
  2.     //定義下啦菜單  
  3.     JComboBox jcb = new JComboBox();  
  4.     public test() throws HeadlessException {  
  5.         this.setLayout(new GridLayout(3, 1));  
  6.         this.setBounds(10, 10, 600, 400);  
  7.         //引入jiaPopMenu()方法  
  8.         this.Add();  
  9.         //設置當單機窗口的關閉按鈕時退出  
  10.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  11.         this.setVisible(true);  
  12.     }  
  13.     public void Add(){  
  14.         String[] str = new String[10];  
  15.         for (int i = 0; i < 10; i++) {  
  16.             //jcb.addItem("此方法直接將內容添加進列表中" +i);  
  17.             str[i] = "選項" + i;  
  18.         }  
  19.         //或者使用Vector來做爲item  
  20.         //Vector v = new Vector();  
  21.         //v.add("選項1");  
  22.         //v.add("選項2");  
  23.         //v.add("選項3");  
  24.         //jcb = new JComboBox(v);  
  25.         jcb = new JComboBox(str);  
  26.         this.add(jcb);  
  27.     }  
  28.     public static void main(String[] args){  
  29.         new test();  
  30.     }  
  31. }  

 

 

九、選項卡

       當今博客盛行的時代,選項卡常常被用到。在博客中,用戶常常會改變北京樣式,用不一樣的風格體現個性。而這些徹底能夠用選項卡來製做。

       使用JTabbedPane類能夠把幾個組件放在一個組件中,如面板。用戶能夠經過選擇對應於目標組件的選項卡來選擇要查看的組件。要建立一個選項卡窗格,只要實例化一個JTabbedPane對象,建立要顯示的租金啊,而後再將這些組件添加到選項卡窗格中便可。

       當建立一個要添加到選項卡窗格的組件時,不管當前可見的是哪一個子選項卡,每個子選項都將得到相同的顯示空間。只不過當前顯示窗格的高度會比其餘窗格稍高一點,以進行區分。下面是一個應用實例:

 

[java]  view plain  copy
 
 print?
  1. public class test extendsJFrame {  
  2.     //定義選項卡  
  3.     JTabbedPane jtb = newJTabbedPane(JTabbedPane.BOTTOM);  
  4.     //定義選項  
  5.     JPanel jp1 = new JPanel();  
  6.     JPanel jp2 = new JPanel();  
  7.     JPanel jp3 = new JPanel();  
  8.     public test() throws HeadlessException {  
  9.         this.setLayout(new GridLayout(1, 1));  
  10.         this.setBounds(10, 10, 600, 400);  
  11.         //引入jiaPopMenu()方法  
  12.         this.Add();  
  13.         //設置當單機窗口的關閉按鈕時退出  
  14.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  15.         this.setVisible(true);  
  16.     }  
  17.     public void Add(){  
  18.         //設置選項事件  
  19.         jp1.setBackground(Color.green);  
  20.         jp2.setBackground(Color.red);  
  21.         jp3.setBackground(Color.BLUE);  
  22.         this.add(jtb);  
  23.         //將選項加入選項卡中  
  24.         jtb.add("綠色背景", jp1);  
  25.         jtb.add("紅色背景", jp2);  
  26.         jtb.add("藍色背景", jp3);  
  27.     }  
  28.     public static void main(String[] args){  
  29.         new test();  
  30.     }  
  31. }  

 

 

十、滑桿

       在應用程序中JSlider支持數值變化。它是一種迅速而簡單的方式,不只能讓用戶以可視形式得到他們當前選擇的反饋,還能獲得能夠接受的值的範圍。

       JSlider類定義了滑桿組件,它的重要方法有:

       >setMaxorTickSpacing():設置主刻度

       >setMinorTickSpacing():設置次刻度

       >setPaintTicks():設置是否繪製刻度

       >setPaintLabels():設置是否繪製標籤

       >addChangeListener():刻度變化事件處理

       >getValue():獲取當前滑塊位置值

       >setValue():設置滑塊初始位置

       下面是具體應用實例:

 

[java]  view plain  copy
 
 print?
  1. public class test extendsJFrame implements ChangeListener{  
  2.     JSlider js = new JSlider();  
  3.     JLabel jl = new JLabel("20");  
  4.     public test() throws HeadlessException {  
  5.         this.setLayout(new GridLayout(4, 1));  
  6.         this.setBounds(10, 10, 600, 400);  
  7.         //引入jiaPopMenu()方法  
  8.         this.Add();  
  9.         //設置當單機窗口的關閉按鈕時退出  
  10.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  11.         this.setVisible(true);  
  12.     }  
  13.     public void Add(){  
  14.         js.setMaximum(100);//最大刻度  
  15.         js.setMinimum(0);//最小刻度  
  16.         js.setOrientation(JSlider.HORIZONTAL);  
  17.         js.setValue(20);//設置滑桿初始位置  
  18.         js.setMajorTickSpacing(20);//設置主刻度  
  19.         js.setMinorTickSpacing(5);//次刻度  
  20.         js.setPaintTicks(true);//繪製刻度  
  21.         js.setPaintLabels(true);//繪製標籤  
  22.         this.add(js);  
  23.         this.add(jl);  
  24.         js.addChangeListener(this);  
  25.          
  26.     }  
  27.     public static void main(String[] args){  
  28.         new test();  
  29.     }  
  30.     public void stateChanged(ChangeEvent e) {  
  31.         jl.setText(js.getValue() +"");  
  32.     }  
  33. }  

 

 

十一、滾動條

       在Swing中,組件中的內容超出其區域時,就須要使用滾動條。它和網頁的滾動條類似。JscrollPane的方法以下:

       >getHorizontalScrollBar():返回水平的JscrollBar組件

       >getVerticalScrollBar():返回垂直的JscrollBar組件

       >get/setHorizontalScrollBarPolicy():Always、Never或As Needed

       >get/setVerticalScrollBarPolicy():與水平函數相同

       下面是對方法的演示:

 

[java]  view plain  copy
 
 print?
  1. public class test extendsJFrame {  
  2.     JLabel jl = new JLabel();  
  3.     JScrollPane jsp = new JScrollPane(jl);  
  4.     JScrollBar jsb =jsp.getVerticalScrollBar();  
  5.     public test() throws HeadlessException {  
  6.         this.setLayout(new GridLayout(1, 2));  
  7.         this.setBounds(10, 10, 600, 400);  
  8.         //引入jiaPopMenu()方法  
  9.         this.Add();  
  10.         //設置當單機窗口的關閉按鈕時退出  
  11.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  12.         this.setVisible(true);  
  13.     }  
  14.     public void Add(){  
  15.         this.add(jsp);  
  16.         jl.setIcon(new ImageIcon("D:\\桌面\\桌面\\image062_s.jpg"));  
  17.         //監聽滾動條的滾動  
  18.         jsb.addAdjustmentListener(newAdjustmentListener() {  
  19.             @Override  
  20.             public voidadjustmentValueChanged(AdjustmentEvent e) {  
  21.                System.out.println(jsb.getModel() + "");  
  22.             }  
  23.         });  
  24.         System.out.println("處理滾動條的四個基本屬性的數據模型:minimum、maximum、value 和extent。" + jsb.getModel());  
  25.     }  
  26.     public static void main(String[] args){  
  27.         new test();  
  28.     }  
  29. }  

 

 

十二、列表框

       列表框是一個很是有用的組件,也愈來愈受到重視。尤爲是它具備多選能力,在選擇選項時能夠按住Shift鍵進行多選。

       JList是一個有用的組件,其相似於一組複選框或一組單選按鈕。經過設置,容許對列表框中的項目進行多項選擇。JList的方法以下:

       >getSelectedIndex():獲取選中的第一個索引

       >getSelectionMode():設置單項選擇仍是多項選擇

       >getListData():設置在列表框中使用數據的模型

       >getSelectedValue():獲取選中的第一個值

       列表框常常與滾動條搭配,由於若是沒有滾動條,列表框中的內容可能沒法徹底顯示,下面是一個實例:

 

[java]  view plain  copy
 
 print?
  1. public class test extendsJFrame {  
  2.     //定義列表框  
  3.     JList jl = new JList(new String[]{"北京" , "天津" , "上海" , "大連" , "青島" , "武漢" , "西安"});  
  4.     JScrollPane jsp = new JScrollPane(jl);  
  5.     public test() throws HeadlessException {  
  6.         this.setLayout(new GridLayout(4, 2));  
  7.         this.setBounds(10, 10, 600, 400);  
  8.         //引入jiaPopMenu()方法  
  9.         this.Add();  
  10.         //設置當單機窗口的關閉按鈕時退出  
  11.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  12.         this.setVisible(true);  
  13.     }  
  14.     public void Add(){  
  15.         this.add(jsp);  
  16.     }  
  17.     public static void main(String[] args){  
  18.         new test();  
  19.     }  
  20. }  

 

 

1三、菜單

       JMenu、JMenuItem和JMenuBar組件是在JFrame中開發菜單系統的主要構造塊。任何菜單系統的基礎都是JMenuBar。它們就像Word中的文件、編輯同樣,下面還有新建、複製、粘貼等一系列內容。其方法以下:

       JMenuItem和JMenu:

       >get/setAccelerator():獲取/設置快捷鍵

       >get/setText():獲取/設置菜單的文本

       >get/setIcon():獲取/設置菜單使用的圖片

       JMenu專用:

       >add():將JMenu或JMenuItem添加到JMenuBar或JMenu中。

       JMenu組件使用來存放和整合JMenuItem的組件,也是構成一個菜單不可缺乏的組件之一。實現包含JMenuItem的彈出窗口,用戶選擇JMenuBar上的選項時會顯示該JMenuItem。下面是一個關於菜單的應用實例:

 

[java]  view plain  copy
 
 print?
  1. public class test extendsJFrame {  
  2.     //定義菜單條組件  
  3.     JMenuBar jmb = new JMenuBar();  
  4.     //定義3個菜單組件  
  5.     JMenu jm1 = new JMenu("文件");  
  6.     JMenu jm2 = new JMenu("編輯");  
  7.     JMenu jm3 = new JMenu("新建");  
  8.     //定義3個菜單項組件  
  9.     JMenuItem jmi1 = newJMenuItem("word");  
  10.     JMenuItem jmi2 = new JMenuItem("複製");  
  11.     JMenuItem jmi3 = new JMenuItem("粘貼");  
  12.     public test() throws HeadlessException {  
  13.         this.setLayout(new GridLayout(1, 1));  
  14.         this.setBounds(10, 10, 600, 400);  
  15.         //引入jiaPopMenu()方法  
  16.         this.Add();  
  17.         //設置當單機窗口的關閉按鈕時退出  
  18.        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  19.         this.setVisible(true);  
  20.     }  
  21.     public void Add(){  
  22.         //調整組件間的包含關係  
  23.         jmb.add(jm1);  
  24.         jmb.add(jm2);  
  25.         jm1.add(jm3);  
  26.         jm3.add(jmi1);  
  27.         jm2.add(jmi2);  
  28.         jm2.add(jmi3);  
  29.         this.setJMenuBar(jmb);  
  30.     }  
  31.     public static void main(String[] args){  
  32.         new test();  
  33.     }  
  34. }  

 

 

       包含關係爲:JmenuBar包含JMenu,JMenu能夠包含JMenu和JMenuItem

相關文章
相關標籤/搜索