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);
}
}
程序结果: