JAVA--GUI__JAVA

來源:互聯網
上載者:User


AWT(Abstract WindowToolkit抽象視窗工具組)包括了很多類和介面,用於java.Application的GUI(Graphics User Interface圖形化使用者介面)編程,GUI的各種元素(如:視窗,按鈕,文字框等)由java類來實現。使用AWT所涉及的類一般在java.awt包及其子包中

Swing是一個為Java設計的GUI工具包。Swing是JAVA基礎類的一部分。Swing包括了圖形化使用者介面(GUI)器件如:文字框,按鈕,分隔窗格和表。Swing提供許多比AWT更好的螢幕顯示元素。它們用純Java寫成,所以同Java本身一樣可以跨平台運行,這一點不像AWT。

 

簡單的說:

 

AWT 是抽象視窗組件工具包,是 java最早的用於編寫圖形節目應用程式的開發包。

Swing 是為瞭解決 AWT存在的問題而新開發的包,它以 AWT 為基礎的。

 

Container和Component是AWT中的兩個核心類


        


Component和Container


java的圖形化使用者介面的最基本組成部分是Component,Component類及其子類的對象用來描述以圖形化方式顯示在螢幕上並能與使用者進行互動的GUI元素,例如:一個按鈕,一個標籤等。

一般的Component對象不能獨立的顯示出來,必須將“放在”某一的Container對象中才可以顯示出來

 

關係:


Container是Component子類,Container子類對象可以“容納”別的Component對象,也可以被當做Component對象添加到其他Container對象中

Container對象可使用方法add()向其中添加其他Component對象

 

有兩種常用的Container:


1,Window:其對象表示自由停泊的最上層視窗

2,Panel:其對象可作為容納其它Component對象,但不能獨立存在,必須被添加到其他Container中(如Window或Applet)

 

 

Frame:

Frame是Window的子類,由Frame或其子類建立的對象為一個表單

Frame的常用構造方法:

1,Frame()

2,Frame(String s)建立標題列為字串s的視窗


樣本:

import java.awt.*;public class TestMultiFrame{public static void main(String args[]){MyFrame f1=new MyFrame(100,100,200,200,Color.BLUE);MyFrame f2=new MyFrame(300,100,200,200,Color.YELLOW);MyFrame f3=new MyFrame(100,300,200,200,Color.GREEN);MyFrame f4=new MyFrame(300,300,200,200,Color.MAGENTA);}}class MyFrame extends Frame{static int id=0;MyFrame(int x,int y,int w,int h,Color color){super("MyFrame"+(++id));setBackground(color);setLayout(null);setBounds(x,y,w,h);setVisible(true);}}

 


Panel:

Panel對象可以看成可以容納Component的空間

Panel對象可以擁有自己的布局管理器


樣本:

import java.awt.*;public class TestMultiPanel{public static void main(String args[]){new MyFrame2("MyFrameWithPanel",300,300,400,300);}}class MyFrame2 extends Frame{private Panel p1,p2,p3,p4;MyFrame2(String s,int x,int y,int w,int h){super(s);setLayout(null);p1=new Panel(null);p2=new Panel(null);p3=new Panel(null);p4=new Panel(null);p1.setBounds(0,0,w/2,h/2);p2.setBounds(0,h/2,w/2,h/2);p3.setBounds(w/2,0,w/2,h/2);p4.setBounds(w/2,h/2,w/2,h/2);p1.setBackground(Color.blue);p2.setBackground(Color.green);p3.setBackground(Color.yellow);p4.setBackground(Color.magenta);add(p1);add(p2);add(p3);add(p4);setBounds(x,y,w,h);setVisible(true);}}




布局管理器:

 

java語言中,提供了布局管理器類的對象可以管理

1,管理Component在Container中的布局,不必直接設定Component位置和大小

2,每個Container都有一個布局管理器對象,當容器需要對某個組件進行定位或判斷其大小尺寸時,就會調用相應的布局管理器,調用Container的setLayout方法改變其布局管理器對象。

AWT提供了5種布局管理器類:
1,FlowLayout

2,BorderLayout

3,GridLayout

4,CardLayout

5,GridBagLayout

 

 

FlowLayout布局管理器

1,是Panel類的預設布局管理器

2,此布局管理器對組件逐行定位,行內從左至右,一行排滿後換行

3,不改變組件的大小,按組件原有尺寸顯示組件,可設定不同的組件間距,行距以及對齊

4,預設的對齊是置中


樣本:

import java.awt.*;public class TestFlowLayout2{public static void main(String args[]){Frame f=new Frame("Java Frame");FlowLayout l=new FlowLayout(FlowLayout.CENTER,20,40);f.setLayout(l);f.setLocation(300,400);f.setSize(300,200);f.setBackground(new Color(204,204,255));for(int i=1;i<=7;i++){f.add(new Button("Button"));}f.setVisible(true);}}



BorderLayout布局管理器

1,是Frame類的預設布局管理器

2,BorderLayout將整個容器的布局劃分成,東(EAST),西(WEST),南(SOUTH),北(NORTH),中(CENTER)五個地區,組件只能被添加到指定的地區

3,如不指定組件的加入部分,則預設加入到CENTER區

4,每個地區只能加入一個組件,如加入多個,則先前加入的會被覆蓋

5,縮放原則:北南兩個地區在水平方向縮放,東西兩個地區在垂直方向縮放,中部可在兩個方向上縮放


樣本:

import java.awt.*;public class TestBorderLayout{public static void main(String args[]){Frame f;f=new Frame("Border Layout");Button bn=new Button("BN");Button bs=new Button("BS");Button bw=new Button("BW");Button be=new Button("BE");Button bc=new Button("BC");f.add(bn,BorderLayout.NORTH);f.add(bs,BorderLayout.SOUTH);f.add(bw,BorderLayout.WEST);f.add(be,BorderLayout.EAST);f.add(bc,BorderLayout.CENTER);f.setSize(200,200);f.setVisible(true);}}




GridLayout布局管理器

1,此管理器將控制項劃分成規定的矩形網格,每個儲存格範圍大小相等。組件被添加到每個儲存格中,先從左至右添滿一行後換行,在從上到下


樣本:

import java.awt.*;public class TestGridLayout{public static void main(String args[]){Frame f=new Frame("GridLayout Example");Button b1=new Button("b1");Button b2=new Button("b2");Button b3=new Button("b3");Button b4=new Button("b4");f.setLayout(new GridLayout(3,2));f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.pack();  //包起來f.setVisible(true);}}



布局管理器總結:


1,Frame是一個最上層視窗,Frame的預設布局管理器為BorderLayout

2,Panel無法單獨顯示,必須添加到某個容器中。Panel的預設布局管理器為FlowLayout

3,當把Panel作為一個組件添加到某個容器中後,該Panel仍然可以有自己的布局管理器

4,使用布局管理器時,布局管理器負責各個組件的大小和位置,因此使用者無法在這種情況下設定組件大小和位置屬性,如果視圖使用java語言提供的setLocation(),setSize(),setBounds()等方法,則都會被布局管理器覆蓋

5,如果使用者確實需要親自設定組件大小或位置,則應取消該容器的布局管理器,方法為:setLayout(null)

以上只是對GUI做了簡單的介紹,讓我們對它有個簡單的理解,隨著以後的學習不斷深入。




聯繫我們

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