126.Java_5.5 GUI

GUI

GUI的各類元素(如:窗口,按鈕,文本框等)由Java類來實現java

 

1.AWT

使用AWT所涉及的類通常在java.awt包及其子包中編程

AWT(Abstract Window Toolkit)包括了不少類和接口,windows

用於Java Application 的GUI(Graphics User Interface 圖形用戶界面)編程佈局

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

 

1.1 Frame

       一、Frame是Windows的子類,由Frame或其子類建立的對象爲一個窗體。

  二、Frame的經常使用構造方法:

      1)Frame()

      2)Frame(String s)建立標題欄爲字符串s的窗口。this

setBounds(int x,int y,int width,int height)
//設置窗體位置和大小,x,y是左上角座標,
//width和height是寬度和高度

setSize(int width,int height)
//設置窗體的位置,x,y是左上角座標

setLocation(int x,int y)
//設置窗體的大小,width和height分別是寬度和高度。

setBackground(Color c)
//設置背景顏色,參數爲Color對象。

setVisible(boolean b)
//設置是否可見。

setTitle(String name) String getTitle()
setResizable(boolean b)
//設置是否能夠調整大小。

1.2 Panel

  一、Panel對象能夠當作能夠容納Compoent的空間

  二、Panel對象能夠擁有本身的佈局管理器

  三、Panel類擁有從其父類繼承來的spa

setBounds(int x,int y,int width,int height)
setSize(int width,int height)
setLocation(int x,int y)
setBackground(Color c)
setLayout(LayoutManager mgr)等方法。

  四、Panel的構造方法爲:3d

Panel()使用默認的FlowLayout類佈局管理器初始化。
Panel(LayoutManager layout)使用指定的佈局管理器初始化。

 



 

2. 佈局管理器

2.1綜述

Java語言中,提供了佈局管理器類的對象能夠管理code

  1)管理Component在Container中的佈局,沒必要直接設置Component位置和大小。orm

  2)每一個Container都有一個佈局管理器對象,當容器須要對某個組件進行定位或判斷其大小尺寸時,

        就會調用其對應的佈局管理器,調用Container的setLayout方法改變其佈局管理器對象。

Awt提供了5中佈局管理器類:

  FlowLayout    BorderLayout    GridLayout     CardLayout    GridBagLayout

2.2 介紹

2.2.1 BorderLayout佈局管理器

1)BorderLayout是Frame類的默認佈局管理器

2)BorderLayout將整個容器的佈局劃分紅

東(EAST)西(WEST)南(SOUTH)北(NORTH)中(CENTER)五個區域,

組件只能被添加到指定的區域。

3)如不指定組件的加入部位,則默認加入到CENTER區。

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

2.3 總結

  一、Frame是一個頂級窗口,Frame的缺省佈局管理器爲BorderLayout
  二、Panel沒法單獨顯示,必須添加到某個容器中。
  三、Panel的缺省佈局管理器爲FlowLayout。
  四、當把Panel做爲一個組件添加到某個容器中後,該Panel仍然能夠有本身的佈局管理器。
  五、使用佈局管理器時,佈局管理器負責各個組件的大小和位置,所以用戶沒法在這種狀況下設置組件大小和位置屬性,若是試圖使用Java語言提供的setLocation(),setSize(),setBounds()等方法,則都會被佈局管理器覆蓋。
  六、若是用戶確實須要親自設置組件大小或位置,則應取消該容器的佈局管理器,方法爲:setLayout(null)

 



 

 

3.事件

3.1 事件監聽

 

 3.2 源碼

/*  範例名稱:Java事件處理舉例
 *    源文件名稱:TestActionEvent.java
 * 要  點:
 *   1. Java事件處理機制
 *     2. 事件源、事件監聽器概念及做用
 *     3. 如何在一個現有組件上註冊監聽器
 */
 
import java.awt.*;
import java.awt.event.*;
 
public class TestActionEvent {
    public static void main(String args[]) {
            Frame f = new Frame("Test");
            Button b = new Button("Press Me!");
            Monitor bh = new Monitor();
            b.addActionListener(bh);
            f.add(b,BorderLayout.CENTER);
            f.pack();
            f.setVisible(true);
    }
}
 
class Monitor implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.out.println("a button has been pressed");    
    }
}
/*  範例名稱:Java事件處理舉例
 *    源文件名稱:TestActionEvent2.java
 * 要  點:
 *   1. 一個事件源組件上能夠同時註冊多個監聽器
 *     2. 一個監聽器對象能夠同時註冊到多個事件源組件上
 *     3. 事件源的信息能夠隨它所觸發的事件自動傳遞到全部註冊過的監聽器
 */
 
import java.awt.*;
import java.awt.event.*;
public class TestActionEvent2 {
    public static void main(String args[]) {
            Frame f = new Frame("Test");
            Button b1 = new Button("Start");
            Button b2 = new Button("Stop");
            Monitor2 bh = new Monitor2();
            b1.addActionListener(bh);       
            b2.addActionListener(bh);
            b2.setActionCommand("game over");
            f.add(b1,"North");       
            f.add(b2,"Center");
            f.pack();              
            f.setVisible(true);
    }
}
 
class Monitor2 implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.out.println("a button has been pressed," + 
        "the relative info is:\n " + e.getActionCommand());    
    }

 



 

 

 

4. 類

4.1 TextField類

  一、java.awt.TextField類用來建立文本框對象。

  二、TextField有以下經常使用方法:

TextField()
TextField(int columns)
TextField(String text)
TextField(String text, int columns)
public void setText(String t)
public String gerText()
publicvoid setEchoChar(char c)設置回顯字符
public void setEditable(boolean b)
public boolean isEditable()
public void setBackground(Color C)
public void select(int selectionStart,int selectionEnd)
public void selectAll()
public void addActionListener(ActionListener l)
添加動做監聽器。
/*  範例名稱:Java事件處理舉例
 *    源文件名稱:TestActionEvent.java
 * 要  點:
 *   1. Java事件處理機制
 *     2. 事件源、事件監聽器概念及做用
 *     3. 如何在一個現有組件上註冊監聽器
 */
 
import java.awt.*;
import java.awt.event.*;
 
public class TestActionEvent {
    public static void main(String args[]) {
            Frame f = new Frame("Test");
            Button b = new Button("Press Me!");
            Monitor bh = new Monitor();
            b.addActionListener(bh);
            f.add(b,BorderLayout.CENTER);
            f.pack();
            f.setVisible(true);
    }
}
 
class Monitor implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.out.println("a button has been pressed");    
    }
}

 

4.2 Graphics類

每一個Component都有一個paint(Graphics g)用於實現繪圖目的,

每次重畫該Component時都自動調用paint方法。

Graphics類中提供了許多繪圖方法,Graphics類 Paint方法

import java.awt.*;
 
public class TestPaint {
    public static void main(String[] args) {
        new PaintFrame().launchFrame();
    }
}
 
class PaintFrame extends Frame {
     
    public void launchFrame() {
        setBounds(200,200,640,480);
        setVisible(true);
    }
     
    public void paint(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.red);
        g.fillOval(50, 50, 30, 30);
        g.setColor(Color.green);
        g.fillRect(80,80,40,40);
        g.setColor(c);
    }
     
}

 

4.3 鼠標事件適配器

抽象類java.awt.event.MouseAdapter實現了MouseListener,

可使用其子類做爲MouseEvent的監聽器,只要重寫其相應的方法便可。

  對於其餘的監聽器,也有對應的適配器。

  使用適配器能夠避免監聽器類定義沒有必要的空方法。

  GUI/MyMouseAdapter.java 鼠標適配器

  repaint - update() - paint();

 

 

4.4 Window事件

  一、Window事件所對應的事件類爲WindowsEvent,所對應的事件監聽接口爲WindowListener。

  二、WindowListener定義的方法有:

public void windowsOpened(WindowEvent e)
public void windowClosing(WindowsEvent e)
public void windowClosed(WindowEvent e)
public void windowIconified(WindowEvent e)
public void windowDeiconified(WindowEvent e)
public void windowActivated(WindowEvent e)
public void windowDeactivated(WindowEvent e)

  與WindowListener對應的適配器爲WindowAdapter。
      GUI/TestWindowClose.java
      GUI/TestAnonymous2.java

import java.awt.*;
import java.awt.event.*;
public class TestWindowClose {
  public static void main(String args[]) {
    new MyFrame55("MyFrame");
  }
}
class MyFrame55 extends Frame {
  MyFrame55(String s) {
    super(s);
    setLayout(null);
    setBounds(300, 300, 400, 300);
    this.setBackground(new Color(204, 204, 255));
    setVisible(true);
    //this.addWindowListener(new MyWindowMonitor());
     
    this.addWindowListener(
    new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        setVisible(false);
        System.exit(-1);
      }
    });
     
  }
  /*
  class MyWindowMonitor extends WindowAdapter {
      public void windowClosing(WindowEvent e) {
          setVisible(false);
          System.exit(0);
      }
  }
  */
}

 

 

 

相關文章
相關標籤/搜索