一、概述
GUI是图形用户界面,在Java中,图形用户界面我们用GUI表示,而GUI的完整英文为: Graphical User Interface(图形用户接口), 所谓图形用户界面就是以图形的方式来显示你计算机的操作界面, 我们计算机中操作的界面就是 我们Java中常说的图形用户界面。
在Java中GUI提供了对象在 java.awt 和 javax.swing 两个包中
在早年中, Java.awt为抽象窗口工具包, 英文为 Abstract Window ToolKit, 需要调用本地的系统方法来实现功能需求,属于重量级的控件.
而在awt的基础上,创建了一套图形界面系统, 从而提供了更多的组件, 而且完全是由Java实现的, 增强了移植性, 属于轻量级的控件. 有自己的显示效果, 显示自己的形状, 平台要求低了, 轻量级和体统嵌入的比较浅.
-
AWT(Abstract Window Toolkit)包括了很多类和接口,用于java Application的GUI(Graphics User Interface)图形用户界面编程;
-
GUI的各种元素(如:窗口,按钮,文本框等)由java类来实现;
-
使用AWT所涉及的类一般在java,awt包及其子包中(AWT目前还没有做到完全跨平台);
-
Container和Component是AWT中的两个核心类;
-
Container包含Window和Panel,Window可容纳其他元素,也可独立显示出来作为一个应用程序的窗口,Panel也可容纳其他元素,但Panel一般看不见,但不能作为应用程序的独立窗口进行显示出来,Panel要想显示,可以把自己装在Window里面即可。
二、AWT组件及容器
-
java的图形用户界面的最基本组成部分是Component,Component类及其子类的对象用来描述以图形化的方式显示在屏幕上并能与用户进行交互的GUI元素,例如,一个按钮,一个标签等;
-
一般的Component对象不能独立地显示出来,必须将“放在”某一的Container对象中才可以显示出来;
-
Container是Component子类,Container子类可以“容纳”别的Component对象;
-
Container对象可使用方法add(````````)向其中添加其他Component对象;
-
Container是Component的子类,因此Container对象也可以被当做Component对象添加到其他Container对象中;
-
有两种常用的Container:
Window:其对象表示自由停泊的顶级窗口;
Panel:其对象可作为容纳其他Component对象,但不能独立存在,必须被添加到其他Contianer中(如:Window或Applet)
1、Frame
public class ExDemo {
public static void main(String[] args) {
Frame frame = new Frame("frame");
frame.setVisible(true);
frame.setSize(400,400);
frame.setBackground(new Color(85,150,68));
frame.setLocation(200,200);
frame.setResizable(false);
}
}
public class TestFrame2 {
public static void main(String[] args) {
//展示多个窗口
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.MAGENTA);
}
}
class MyFrame extends Frame{
static int id = 0;
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame +" +(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
2、Panel
public class PanelDemo {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
frame.setLayout(null);
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(40,161,35));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(193, 15, 60));
frame.add(panel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
3、布局
AWT提供了5种布局管理器:FlowLayout,BorderLayout,GridLayout,CardLayout,GridBagLayout
- FlowLayout布局管理器是默认的布局管理器,它将组件按照从左到右、从上到下的顺序来安排,并在默认情况下使组件尽量居中放置。
- BorderLayout布局管理器只允许在容器内放置5个组件,这5个组件的位置是由BorderLayout类中的North、South、East、West和Center5个常量来确定的,他们对应着容器中的上下左右中
- GridLayout布局管理器是矩形网格,在网格中放置组件,每个网格的高度和宽度都相等,组件的排列顺序与FlowLayout相同。组件随着网格的大小而在水平和垂直方向上拉伸,网格的大小是由容器的大小和创建网格的多少来确定的。
- CardLayout运行在一个组件中每次只显示一组组件中的某一个,用户可以根据需要来选择使用哪个组件。
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
frame.setLayout(new GridLayout(2,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.pack();
frame.setVisible(true);
}
}
public class ExDemo {
public static void main(String[] args) {
//总 Frame
Frame frame = new Frame();
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setBackground(Color.BLACK);
frame.setVisible(true);
frame.setLayout(new GridLayout(2,1));
//4个面板
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
//上面
p1.add(new Button("East-1"),BorderLayout.EAST);
p1.add(new Button("West-1"),BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
//下面
p3.add(new Button("East-2"),BorderLayout.EAST);
p3.add(new Button("West-2"),BorderLayout.WEST);
//中间4个
for (int i = 0; i < 4; i++) {
p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
}
}
效果图
三、事件监听
事件监听:当某个事情发生的时候,干什么?
- 鼠标监听
- 键盘监听
- 窗口监听
- 事件监听
ActionListener是由处理ActionEvent事件的监听器对象实现的。当单击按钮、在文本区域中按回车键、选择菜单项、双击列表项都会触发监听器。该接口中的方法为:
public voidactoinPerformed(ActionEvent e)
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button("button1");
button.addActionListener(new MyListener());
frame.add(button,BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
windowClose(frame);
}
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了!");
}
}
输入框TextFild监听
文本组件就像把窗口空白处看成记事本一样,它是一个用来写内容的组件。TextListener用来确定何时文本值改变。该接口还可以用到很多地方,其接口方法如下:
public voidtextValueChanged(TextEvent e)
public class TestTextFild {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame {
public MyFrame() {
TextField textField = new TextField();
add(textField);
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();
System.out.println(field.getText());
field.setText("");
}
});
}
}
简单计算器,使用匿名内部类的方式实现监听,简化书写。
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
class Calculator extends Frame {
private TextField num1, num2, num3;
public Calculator() {
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("+");
Label label = new Label("=");
setLayout(new FlowLayout());
add(num1);
add(button);
add(num2);
add(label);
add(num3);
pack();
setVisible(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText((n1 + n2) + "");
num1.setText("");
num2.setText("");
}
});
}
}
鼠标监听
- 利用鼠标监听实现简单画板功能
public class TestMouseListener {
public static void main(String[] args) {
new Mouse("画图");
}
}
class Mouse extends Frame{
ArrayList points;
public Mouse(String title){
super(title);
setBounds(200,200,400,300);
points = new ArrayList();
setVisible(true);
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Mouse mouse = (Mouse) e.getSource();
mouse.addPaint(new Point(e.getX(),e.getY()));
mouse.repaint();//刷新
}
});
}
@Override
public void paint(Graphics g){
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x,point.y,10,10);
}
}
public void addPaint(Point point){
points.add(point);
}
}
窗口监听
WindowListener是处理WindowEvent事件的对象实现的。这个监听器确定了窗口设么时候被打开、关闭、激活、不激活、最小化和最大化。该接口中的方法有:
- public voidwindowActivated(WindowEvent e)
- ublic voidwindowClosed(WindowEvent e)
- public voidwindowClosing(WindowEvent e)
- public voidwindowDeactivated(WindowEvent e)
- public voidwindowDeiconfied(WindowEvent e)
- public void windowIconified(WindowEvente)
- public voidwindowOpened(WindowEvent e)
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.blue);
setBounds(100,100,200,200);
setVisible(true);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");
}
}
);
}
}
键盘监听
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if (keyCode == KeyEvent.VK_UP) {
System.out.println("你按下了上键");
}
}
});
}
}