组件
GUI的核心技术:Swing AWT,界面不美观,需要jre环境
我们为什么要学习呢?
1. 可以写出自己心中想要的一些小工具
2. 工作时候,也可能需要维护到swing界面,概率极小
3. 了解MVC架构,了解监听!
A抽象的 W窗口 T工具,包括了很多类和接口。
元素:窗口,按钮,文本框(java.awt包)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BOUZc5nG-1611901065553)(C:\Users\Administrator\Desktop\Markdown学习\QQ截图20210128172814.png)]
package com.lean.www;
import java.awt.*;
import java.beans.FeatureDescriptor;
public class Gui01 {
public static void main(String[] args) {
//Frame JDK 看源码
Frame frame = new Frame("fistDui");
//设置窗口可见性
frame.setVisible(true);
//设置窗口大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(40, 107, 200));
//弹出的初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
问题,发现窗口关闭不了,解决办法,终止运行。
package com.lean.www;
//回顾封装
import java.awt.*;
public class Gui02 {
public static void main(String[] args) {
Frame frame1 = new MyFrame(100,100,200,200,Color.blue);
Frame frame2 = new MyFrame(300,100,200,200,Color.red);
Frame frame3 = new MyFrame(100,300,200,200,Color.yellow);
Frame frame4 = new MyFrame(300,300,200,200,Color.blue);
}
}
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);
}
}
面板Panel:可以堪称一个空间,但是不能单独存在
解决了关闭实现
package com.lean.www;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Panel01 {
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(64, 203, 203));
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听窗口关闭事件 强制关闭System.exit(0)
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
//窗口关闭的时候要做的事情
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
布局管理器
流式布局
package com.lean.www;
import java.awt.*;
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
//组件按钮
Button button01=new Button("01");
Button button02 = new Button("02");
Button button03=new Button("03");
//设置为流式布局,默认居中
frame.setLayout(new FlowLayout());
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));左
frame.setSize(200,200);
//把按钮加上
frame.add(button01);
frame.add(button02);
frame.add(button03);
frame.setVisible(true);
}
}
东西南北中 Border
package com.lean.www;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
Button east = new Button("East");
Button west = new Button("West");
Button noth = new Button("Noth");
Button south = new Button("South");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(noth,BorderLayout.NORTH);
frame.add(south,BorderLayout.SOUTH);
frame.add(center,BorderLayout.CENTER);
frame.setVisible(true);
}
}
表格布局 Grid
package com.lean.www;
import java.awt.*;
public class TestGirdLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
Button east = new Button("East");
Button west = new Button("West");
Button noth = new Button("Noth");
Button south = new Button("South");
frame.setLayout(new GridLayout(2,2));
frame.add(east);
frame.add(west);
frame.add(noth);
frame.add(south);
frame.pack();//java函数:自动最优布局
frame.setVisible(true);
}
}
练习:
package com.lean.www;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Homework {
public static void main(String[] args) {
Frame frame = new Frame("练习");
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));
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");
Button btn7 = new Button("btn7");
Button btn8 = new Button("btn8");
Button btn9 = new Button("btn9");
Button btn10 = new Button("btn10");
frame.setSize(400,300);
frame.setBackground(new Color(0, 73, 255));
frame.setLocation(200,200);
p1.add(btn1,BorderLayout.EAST);
p1.add(btn2,BorderLayout.WEST);
p2.add(btn3);
p2.add(btn4);
p1.add(p2,BorderLayout.CENTER);
p3.add(btn5,BorderLayout.EAST);
p3.add(btn6,BorderLayout.WEST);
p4.add(btn7);
p4.add(btn8);
p4.add(btn9);
p4.add(btn10);
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.setVisible(true);
//监听器关闭
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
总结
Frame是一个顶级窗口
panel无法单独显示,必须添加到某个容器中
布局管理器
大小,定位,背景颜色,可见性,监听(listener)
多个按钮,共享一个监听
package com.lean.www;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLOutput;
public class TestAction2 {
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("start");
Button button2 = new Button("stop");
//可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
//可以多个按钮只写一个监听类
button2.setActionCommand("stop");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.NORTH);
frame.add(button2,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
windowClose(frame);
}
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()获取按钮的信息
System.out.println("按钮被点击了:msg"+e.getActionCommand());
if(e.getActionCommand().equals("start")){
System.out.println("开始");
}else if(e.getActionCommand().equals("stop")){
System.out.println("结束");
}
}
}
package com.lean.www;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestText01 {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
windowCloes(frame);
}
private static void windowCloes(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按下Enter就会触发输入框的事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');//输入的时候就显示*了,*是char类型,后台正常
setVisible(true);
pack();
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand(); //获得一些资源,返回一个对象
TextField field = (TextField)e.getSource();
System.out.println(field.getText());//获得输入框的文本
field.setText("");//回车后文本框就变成空了
}
}