GUI,全稱爲 Graphical User Interface,意爲 用戶圖形界面,是指採用圖形方式顯示的計算機操作用戶界面。
GUI編程的核心技術是AWT以及Swing。可以說Swing是AWT的升級版本。那麼就先來了解一下AWT。
AWT中包含了很多的類和接口,用於進行GUI編程。GUI編程中有很多元素:窗口、按鈕、文本框等。
在使用AWT編程中,我們主要使用到的包是java.awt包,在此包下有很多類和接口。
public abstract class Component
extends Object
implements ImageObserver, MenuContainer, Serializable
上述類的組件的抽象類。組件是具有可以在屏幕上顯示並且可以與用戶交互的圖形表示的對象。
該類的直接子類爲:
直接子類 |
---|
Button |
Canvas |
Checkbox |
Choice |
Container |
Label |
List |
Scrollbar |
TextComponent |
AWT中有組件和容器,一般是將組件添加到容器中。
Frame
Frame在java.awt包下,是一個標題和邊框的頂級窗口。
public class Frame
extends Window
implements MenuContainer
Frame的直接子類是JFrame。
構造方法
構造方法 | 具體含義 |
---|---|
Frame() | 構造的新實例 Frame初始時不可見 |
Frame(GraphicsConfiguration gc) | 構造一個新的,最初看不見的 Frame與指定的 GraphicsConfiguration |
Frame(String title) | 構造一個新的,最初不可見的 Frame對象,其中包含指定的標題 |
Frame(String title, GraphicsConfiguration gc) | 構造一個新的,最初不可見的 Frame對象,具有指定的標題和一個 GraphicsConfiguration |
成員方法
Frame中常用的成員方法有:
成員方法 | 具體含義 |
---|---|
public void setTitle(String title) | 將此框架的標題設置爲指定的字符串 |
public void setSize(int width,int height) | 調整此組件的大小,使其寬度爲width ,高度爲height |
public void setVisible(boolean b) | 設置窗口的可見性 |
public void setLocation(int x,int y) | 將此組件移動到新位置。 新位置的左上角由x和y參數確定 |
public void setBounds(int x,int y,int width,int height) | 移動並調整此組件的大小。 左上角的新位置由x和y確定 ,新尺寸由width和height確定 |
public void setResizable(boolean resizable) | 設置該框架是否可以由用戶調整大小 |
public void setBackground(Color bgColor) | 設置此窗口的背景顏色 |
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("Java GUI");
// 設置窗口的可見性
frame.setVisible(true);
// 設置窗口的大小
frame.setSize(500,500);
// 設置窗口彈出的初始位置
frame.setLocation(200,200);
// 設置窗口的背景顏色
frame.setBackground(Color.cyan);
// 設置窗口的大小是否可以由用戶調整,即窗口是否可以縮放
frame.setResizable(false);
}
}
程序運行結果:
在運行此程序時,該窗口是無法通過點擊關閉的按鈕來進行關閉的,只能通過停止程序的運行來關閉窗口。爲了解決此問題,我們可以添加一個監聽事件,用於監聽關閉事件:
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("Java GUI");
// 設置窗口的可見性
frame.setVisible(true);
// 設置窗口的大小
frame.setSize(500,500);
// 設置窗口彈出的初始位置
frame.setLocation(200,200);
// 設置窗口的背景顏色
frame.setBackground(Color.GREEN);
// 設置窗口的大小是否可以由用戶調整,即窗口是否可以縮放
frame.setResizable(false);
// 添加窗口監聽事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
Panel
在添加組件的時候,最好是將組件添加到容器裏如Panel即面板上,再將容器(Panel)添加到Frame中。
public class Panel
extends Container
implements Accessible
Panel 是最簡單的容器類,可以將其他組件放在面板提供的空間內,這些組件包括按鈕、文本框或者其他面板。
面板的默認佈局管理器是 FlowLayout 佈局管理器。
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame("TestPanel");
Panel panel = new Panel();
TextField textField = new TextField();
textField.setText("歡迎學習JAVA");
frame.setBounds(100,100,400,300);
frame.setResizable(false);
frame.setVisible(true);
panel.setBackground(Color.YELLOW);
panel.add(textField);
frame.add(panel);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
程序結果:
佈局管理器
在添加組件的時候,我們會對組件進行一定佈局,以達到我們想要的效果。
最常見的佈局有三種:
- FlowLayOut:流式佈局
- BorderLayOut:邊框佈局
- GridLayOut:表格佈局
FlowLayOut
流式佈局用於安排有向流中的組件,這非常類似於段落中的文本行。流的方向取決於容器的 componentOrientation 屬性,它可能是以下兩個值中的一個:
- ComponentOrientation.LEFT_TO_RIGHT
- ComponentOrientation.RIGHT_TO_LEFT
流式佈局一般用來安排面板中的按鈕。它使得按鈕呈水平放置,直到同一條線上再也沒有適合的按鈕。
public class TestFlowLayOut {
public static void main(String[] args) {
Frame frame = new Frame("TestFlowLayOut");
Panel panel = new Panel();
panel.setBackground(new Color(100,50,90));
for (int i = 0; i < 3; i++) {
Button button = new Button("button" + i);
panel.add(button);
}
frame.setBounds(100,100,300,300);
frame.setVisible(true);
frame.add(panel);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
程序結果:
BorderLayOut
BorderLayOut是一個用於佈置容器的邊框佈局,它可以對容器組件進行安排,並調整其大小,使其符合下列五個區域:北、南、東、西、中。它可以對容器組件進行安排,並調整其大小,使其符合下列五個區域:北、南、東、西、中。
每個區域最多隻能包含一個組件,並通過相應的常量進行標識:NORTH、SOUTH、EAST、WEST、CENTER。當使用邊框佈局將一個組件添加到容器中時,要使用這五個常量之一。
BorderLayout 將缺少字符串說明的情況解釋爲常量 CENTER。
public class TestBorderLayOut {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayOut");
Panel panel = new Panel();
panel.setLayout(new BorderLayout());
panel.setBackground(new Color(110, 50, 30));
Button button1 = new Button("button1" );
panel.add(button1,BorderLayout.NORTH);
Button button2 = new Button("button2" );
panel.add(button2,BorderLayout.SOUTH);
Button button3 = new Button("button3" );
panel.add(button3,BorderLayout.EAST);
Button button4 = new Button("button4" );
panel.add(button4,BorderLayout.WEST);
Button button5 = new Button("button5" );
panel.add(button5,BorderLayout.CENTER);
frame.add(panel);
frame.setBounds(200, 200, 300, 300);
frame.setResizable(false);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
程序結果:
GridLayOut
GridLayout 是一個佈局處理器,它以矩形網格形式對容器的組件進行佈置。容器被分成大小相等的矩形,一個矩形中放置一個組件。
可以通過構造方法或者setRows 和 setColumns 方法設置行數和列數。
構造方法
構造方法 | 具體含義 |
---|---|
GridLayout() | 創建具有默認值的網格佈局,即每個組件佔據一行一列 |
GridLayout(int rows, int cols) | 創建具有指定行數和列數的網格佈局 |
GridLayout(int rows, int cols, int hgap, int vgap) | 創建具有指定行數和列數的網格佈局 |
public class TestGridLayOut {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayOut");
Panel panel = new Panel();
panel.setBackground(new Color(68, 105, 120));
panel.setLayout(new GridLayout(2,2));
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
frame.add(panel);
frame.setBounds(200,200,300,350);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
程序結果:
事件監聽
當發生某件事情的時候,一般來說我們都要採取措施。事件監聽是用於監聽事件的發生,以及應該對此做些什麼。
public interface ActionListener
extends EventListener
ActionListener是用於接收操作事件的監聽器接口。對處理操作事件感興趣的類可以實現此接口,而使用該類創建的對象可使用組件的 addActionListener 方法向該組件註冊。在發生操作事件時,調用該對象的 actionPerformed 方法。
ActionListener中只有一個方法:
actionPerformed(ActionEvent e)
該方法是在發生操作時進行調用的。
public class TestActionListener {
public static void main(String[] args) {
Frame frame = new Frame("TestActionListener");
Panel panel = new Panel();
Button button = new Button("button");
panel.setBackground(new Color(20,70,50));
frame.setVisible(true);
frame.setSize(100,100);
// 按下按鈕,觸發事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("歡迎學習Java");
}
});
panel.add(button);
frame.add(panel);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
TextField事件監聽
public class TestTextFieldListener {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
Panel panel = new Panel();
panel.setBackground(Color.WHITE);
TextField textField = new TextField(10);
MyListener myListener = new MyListener();
textField.addActionListener(myListener);
panel.add(textField);
this.add(panel);
this.setBounds(100,100,300,300);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField source = (TextField) e.getSource();
String text = source.getText();
System.out.println(text);
source.setText("");
}
}
鼠標監聽
鼠標監聽顧名思義就是用於監聽鼠標的操作。
public interface MouseListener
extends EventListener
MouseListener是用於接收組件上“感興趣”的鼠標事件(按下、釋放、單擊、進入或離開)的監聽器接口。
方法 | 具體含義 |
---|---|
mouseClicked(MouseEvent e) | 鼠標按鍵在組件上單擊(按下並釋放)時調用 |
mouseEntered(MouseEvent e) | 鼠標進入到組件上時調用 |
mouseExited(MouseEvent e) | 鼠標離開組件時調用 |
mousePressed(MouseEvent e) | 鼠標按鍵在組件上按下時調用 |
mouseReleased(MouseEvent e) | 鼠標按鈕在組件上釋放時調用 |
旨在處理鼠標事件的類要麼實現MouseListener接口(及其包含的所有方法),要麼擴展抽象類 MouseAdapter(僅重寫所需的方法)。
public class TestMouseListener {
public static void main(String[] args) {
Frame frame = new Frame("TestMouseListener");
Panel panel = new Panel();
panel.setBackground(new Color(200,90,120));
frame.setVisible(true);
frame.setBounds(200,200,400,400);
frame.add(panel);
panel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("單擊鼠標了");
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
鍵盤監聽
鍵盤監聽與鼠標監聽類似,是用於進行鍵盤操作的監聽。
public interface KeyListener
extends EventListener
KeyListener是用於接收鍵盤事件(擊鍵)的監聽器接口。
方法 | 具體含義 |
---|---|
keyPressed(KeyEvent e) | 按下某個鍵時調用此方法 |
keyReleased(KeyEvent e) | 釋放某個鍵時調用此方法 |
keyTyped(KeyEvent e) | 鍵入某個鍵時調用此方法 |
旨在處理鍵盤事件的類要麼實現此接口(及其包含的所有方法),要麼擴展抽象 KeyAdapter 類(僅重寫有用的方法)。然後使用組件的 addKeyListener 方法將從該類所創建的偵聽器對象向該組件註冊。按下、釋放或鍵入鍵時生成鍵盤事件。然後調用偵聽器對象中的相關方法並將該 KeyEvent 傳遞給它。
public class TestKeyListener {
public static void main(String[] args) {
Frame frame = new Frame("TestKeyListener");
Panel panel = new Panel();
frame.setVisible(true);
frame.setBounds(100,100,200,200);
panel.setBackground(Color.BLUE);
panel.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("按下鍵盤了");
}
@Override
public void keyReleased(KeyEvent e) {
}
});
frame.add(panel);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
窗口監聽
窗口監聽是用於監聽窗口事件的。
public interface WindowListener
extends EventListener
WindowListener是用於接收窗口事件的監聽器接口。
方法 | 具體含義 |
---|---|
windowActivated(WindowEvent e) | 將 Window 設置爲活動 Window 時調用 |
windowClosed(WindowEvent e) | 因對窗口調用 dispose 而將其關閉時調用 |
windowClosing(WindowEvent e) | 用戶試圖從窗口的系統菜單中關閉窗口時調用 |
windowDeactivated(WindowEvent e) | 當 Window 不再是活動 Window 時調用 |
windowDeiconified(WindowEvent e) | 窗口從最小化狀態變爲正常狀態時調用 |
windowIconified(WindowEvent e) | 窗口從正常狀態變爲最小化狀態時調用 |
windowOpened(WindowEvent e) | 窗口首次變爲可見時調用 |
旨在處理窗口事件的類要麼實現此接口(及其包含的所有方法),要麼擴展抽象類 WindowAdapter(僅重寫所需的方法)。然後使用窗口的 addWindowListener 方法將從該類所創建的偵聽器對象向該 Window 註冊。當通過打開、關閉、激活或停用、圖標化或取消圖標化而改變了窗口狀態時,將調用該偵聽器對象中的相關方法,並將 WindowEvent 傳遞給該方法。
public class TestWindowListener {
public static void main(String[] args) {
Frame frame = new Frame("TestWindowListener");
Panel panel = new Panel();
panel.setBackground(Color.WHITE);
frame.add(panel);
frame.setVisible(true);
frame.setBounds(100,100,200,200);
frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("窗口打開");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("關閉窗口");
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("窗口已關閉");
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("窗口激活");
}
@Override
public void windowDeactivated(WindowEvent e) {
}
});
}
}
畫筆
public abstract class Graphics
extends Object
Graphics 類是所有圖形上下文的抽象基類,允許應用程序在組件(已經在各種設備上實現)以及閉屏圖像上進行繪製。
public class TestGraphics01 {
public static void main(String[] args) {
new MyFrame01();
}
}
class MyFrame01 extends Frame{
public MyFrame01(){
Panel panel = new MyPanel();
panel.setBackground(Color.WHITE);
this.setBounds(100,100,300,300);
this.setVisible(true);
this.add(panel);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyPanel extends Panel{
// 重寫paint方法
@Override
public void paint(Graphics g) {
//super.paint(g);
g.setColor(Color.RED);
g.drawRect(100,100,30,30);
g.fillOval(150,150,20,20);
g.fillRoundRect(180,180,20,20,10,10);
}
}
程序結果: