GUI全称Graphical User Interfaces,意为图形用户户界面,又称为图形用户接口.,GUI指的就是采用图形方式显示的计算机操作用户界面,
GUI核心技术:Swing、AWT
为什么不用:
为什么我们要学习?
import java.awt.*;
public class TestFrame {
public static void main(String[] args){
//Frame
Frame frame = new Frame("我的第一个java图形界面窗口");
//设置可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色 Color
frame.setBackground(Color.yellow);
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
窗口无法关闭;停止java程序运行
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.cyan);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200,Color.green);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200,Color.red);
MyFrame myFrame4 = new MyFrame(300, 300, 200, 200,Color.yellow);
}
}
class MyFrame extends Frame {
static int id = 0;//可能存在多个窗口,我们需要要给计数器。
public MyFrame(int x,int y,int w,int h,Color color){
super("demo+"+(++id)); //id自增
setVisible(true);
setBounds(x,y,w,h);
setBackground(color);
}
}
Panel 可以看成是一个空间,但是不能单独存在。
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanle {
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(Color.lightGray);
//设置坐标,先对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(15, 193, 101));
//frame.add(panel);添加panel
frame.add(panel);
frame.setVisible(true);
//监听实践,监听窗口关闭 System,exit(0);
//适配器模式
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
//结束程序
System.exit(0);
}
});
}
}
监听实践,监听窗口关闭 System,exit(0);
//监听实践,监听窗口关闭 System,exit(0);
//适配器模式
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
//结束程序
System.exit(0);
}
});
frame.setLayout()
import java.awt.*;
public class TestFlowLayoout {
public static void main(String[] args) {
Frame frame = new Frame();
//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置为流式布局
frame.setLayout(new FlowLayout()); //默认居中
// frame.setLayout(new FlowLayout(FlowLayout.CENTER));// 居中
// frame.setLayout(new FlowLayout(FlowLayout.LEFT));// 左
// frame.setLayout(new FlowLayout(FlowLayout.RIGHT));// 右
frame.setSize(200, 200);
//添加按钮
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
import java.awt.*;
public class TestLayoout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
Button button1 = new Button("East");
Button button2 = new Button("west");
Button button3 = new Button("south");
Button button4 = new Button("north");
Button button5 = new Button("center");
//添加东西南北中效果
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
frame.add(button3,BorderLayout.SOUTH);
frame.add(button4,BorderLayout.NORTH);
frame.add(button5,BorderLayout.CENTER);
frame.setSize(500,500);
frame.setBackground(Color.MAGENTA);
frame.setVisible(true);
}
}
package awt;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanle {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5= new Button("btn5");
Button btn6= new Button("btn6");
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();//java函数 自动布局
frame.setVisible(true);
}
}
package awt;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanle {
public static void main(String[] args) {
//总窗
Frame frame = new Frame();
frame.setLayout(new GridLayout(2,1));
frame.setBounds(400,300,300,400);
frame.setBackground(Color.green);
frame.setVisible(true);
//四个面板
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);
for (int i = 0; i < 4; i++) {
p4.add(new Button("p4-btn-"+i));
}
p3.add(p4,BorderLayout.CENTER);
//放入面板
frame.add(p1);
frame.add(p3);
//设置监听
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}
}