学习资料
b站狂神说:https://www.bilibili.com/video/BV1DJ411B75F
第一个窗口Farme类
package com.zy7y.gui;
import java.awt.*;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui
* @Author: zy7y
* @Date: 2020/8/15 下午5:35
* @Description: 创建第一个窗口
*/
public class TestFrame {
public static void main(String[] args) {
// 实例化窗口对象
Frame frame = new Frame("第一个Java图像界面窗口");
// 设置窗口可见性
frame.setVisible(true);
// 设置窗口大小
frame.setSize(200,300);
// 设置背景色
frame.setBackground(new Color(0x9797DC));
// 初始位置
frame.setLocation(200,200);
// 设置窗口大小固定:
frame.setResizable(false);
}
}
封装创建窗口的方法
package com.zy7y.gui;
import java.awt.*;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui
* @Author: zy7y
* @Date: 2020/8/15 下午5:44
* @Description:
*/
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame(100,200,100,100,Color.CYAN);
MyFrame myFrame1 = new MyFrame(300,200,200,200,Color.cyan);
MyFrame myFrame2 = new MyFrame(200,200,300,300,Color.yellow);
MyFrame myFrame3 = new MyFrame(400,200,400,400,Color.BLACK);
}
}
class MyFrame extends Frame{
static int id = 0;
public MyFrame(int x, int y, int w, int h, Color color){
// 调用父类构造方法初始化一个Frame对象
super("第" + ++id + "个窗口");
// 窗口标题
// 窗口尺寸
setBounds(x,y,w,h);
// 窗口调节
setResizable(false);
// 窗口可见性
setVisible(true);
// 窗口弹出位置
setLocation(200,200);
// 窗口颜色
setBackground(color);
}
}
面板Plan类
面板可以嵌套
package com.zy7y.gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui
* @Author: zy7y
* @Date: 2020/8/15 下午5:54
* @Description: 面板 Plan类
*/
public class TestPlan {
public static void main(String[] args) {
// 创建窗口
Frame frame = new Frame();
// 创建面板
Panel panel = new Panel();
// 设置布局
frame.setLayout(null);
// 坐标
frame.setBounds(300,300,300,300);
frame.setBackground(Color.cyan);
// 设置面板
panel.setBackground(Color.red);
panel.setBounds(100,100,100,100);
// 将面板加入到frame窗口中
frame.add(panel);
frame.setVisible(true);
// 监听,适配器模式;
frame.addWindowListener(new WindowAdapter() {
// 窗口点击关闭的时候需要做的事情
@Override
public void windowClosing(WindowEvent e) {
// super.windowClosing(e);
// 强制退出程序
System.exit(0);
}
});
}
}
布局管理器
流式布局
package com.zy7y.gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui
* @Author: zy7y
* @Date: 2020/8/15 下午6:08
* @Description:
*/
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame("流式布局");
// 组件-按钮
Button button1 = new Button("按钮1");
Button button2 = new Button("按钮2");
Button button3 = new Button("按钮3");
// 设置为流式布局,并靠窗口Frame左侧
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
frame.setBounds(200,200,100,100);
// 将组件加入窗口中
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
// 监听,关闭窗口
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
东南西北中
package com.zy7y.gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui
* @Author: zy7y
* @Date: 2020/8/15 下午6:16
* @Description: 东西南北中
*/
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("东西南北中");
Button button1 = new Button("东");
Button button2 = new Button("西");
Button button3 = new Button("南");
Button button4 = new Button("北");
Button button5 = new Button("中");
// 添加到东,西,南,北,中的位置
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.setBounds(500,500,900,600);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
表格布局
package com.zy7y.gui;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui
* @Author: zy7y
* @Date: 2020/8/15 下午6:28
* @Description: 网格布局
*/
public class TestGridLayout {
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");
Button button4 = new Button("button4");
Button button5 = new Button("button5");
Button button6 = new Button("button6");
// 设置成3行2列的表格布局
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.add(button5);
frame.add(button6);
frame.setBounds(500,500,900,600);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
布局作业
package com.zy7y.gui;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui
* @Author: zy7y
* @Date: 2020/8/15 下午6:36
* @Description: 布局作业, 面板可以嵌套
*/
public class TestLayoutTopic {
public static void main(String[] args) {
Frame frame = new Frame("布局作业");
frame.setBounds(500,500,900,600);
Panel panel1 = new Panel(new BorderLayout());
panel1.setBackground(Color.black);
Panel panel2 = new Panel(new GridLayout(2,1));
Panel panel3 = new Panel(new BorderLayout());
panel3.setBackground(Color.red);
Panel panel4 = new Panel(new GridLayout(2,2));
// 内上
panel1.add(new Button("第一行-东"), BorderLayout.EAST);
panel1.add(new Button("第一行-西"), BorderLayout.WEST);
panel2.add(new Button("第一行-第一行"));
panel2.add(new Button("第一行-第二行"));
// 将面板panel2 添加到 面板panle1中间的位置
panel1.add(panel2,BorderLayout.CENTER);
// 内下
panel3.add(new Button("第二行-东"), BorderLayout.EAST);
panel3.add(new Button("第二行-西"), BorderLayout.WEST);
panel4.add(new Button("第二行-中-左上"));
panel4.add(new Button("第二行-中-右上"));
panel4.add(new Button("第二行-中-左下"));
panel4.add(new Button("第二行-中-右下"));
panel3.add(panel4, BorderLayout.CENTER);
frame.add(panel1);
frame.add(panel3);
frame.setLayout(new GridLayout(2,1));
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
事件监听
package com.zy7y.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui
* @Author: zy7y
* @Date: 2020/8/15 下午7:27
* @Description: 事件监听
*/
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button();
// 按钮事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被按下");
}
});
frame.add(button);
frame.pack();
frame.setVisible(true);
// 窗口关闭事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
package com.zy7y.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @ProjectName: JavaSE
* @PackageName: com.zy7y.gui
* @Author: zy7y
* @Date: 2020/8/15 下午8:10
* @Description: 两个按钮实现同一个监听
*/
public class TestActionEvent2 {
public static void main(String[] args) {
Frame frame = new Frame();
Button start = new Button("start");
Button stop = new Button("stop");
Button control = new Button("开始");
control.setBackground(Color.green);
MyMonitor myMonitor = new MyMonitor();
start.addActionListener(myMonitor);
stop.addActionListener(myMonitor);
control.addActionListener(myMonitor);
frame.add(start);
frame.add(stop);
frame.add(control);
frame.setLayout(new GridBagLayout());
frame.pack();
closeWindows(frame);
frame.setVisible(true);
}
// 关闭窗口
public static void closeWindows(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("按钮被点击:" + e.getActionCommand());
// 获得资源对象,强转成button,实现一个按钮切换的操作
Button button = (Button)e.getSource();
if (button.getLabel().equals("开始")) {
System.out.println("你点下了开始按钮");
button.setLabel("结束");
button.setBackground(Color.red);
}else if (button.getLabel().equals("结束")) {
System.out.println("你点下了结束按钮");
button.setLabel("开始");
button.setBackground(Color.green);
}
}
}