Day14 2021.3.29
GUI编程
概念
组件:
-
窗口
-
弹窗
-
面板
-
文本框
-
列表框
-
按钮
-
图片
-
监听事件
-
鼠标事件
-
键盘事件
核心技术:Swing AWT
AWT
-
包含了很多的类和接口
-
元素:窗口、按钮、文本框
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0U2jh1gX-1617008498104)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20210329123520103.png)]
Frame容器
package com.hong.Day011_Day020.Day014.Demo01;
import java.awt.*;
/*
java.awt.Frame 一个标题和边框的顶级窗口
构造方法:
Frame(String title) 构造一个新的,最初不可见的 Frame对象,其中包含指定的标题。
常用方法:
1.setBackground(Color bgColor)设置此窗口的背景颜色
2.setIconImage(Image image)将要显示的图像设置为此窗口的图标
3.setMenuBar(MenuBar mb)将此框架的菜单栏设置为指定的菜单栏
4.setOpacity(float opacity)设置窗口的不透明度
5.setResizable(boolean resizable)设置该框架是否可以由用户调整大小
6.setShape(Shape shape)设置窗口的形状
7.setTitle(String title)将此框架的标题设置为指定的字符串
8.setVisible(true)设置可见性
9.setLocation(x,y)窗口弹出的初始位置
*/
public class Demo01 {
public static void main(String[] args) {
//Frame
Frame frame = new Frame("第一个图像窗口");
//设置可见性
frame.setVisible(true);
//设置大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(Color.BLACK);
//弹出初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
//展示多个窗口
Myframe myframe1 = new Myframe(100,100,200,200,Color.blue);
Myframe myframe2 = new Myframe(100,300,200,200,Color.BLACK);
Myframe myframe3 = new Myframe(300,100,200,200,Color.GREEN);
Myframe myframe4 = new Myframe(300,300,200,200,Color.red);
}
}
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.hong.Day011_Day020.Day014.Demo01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/*
java.awt.Panel最简单的容器类
构造方法:
Panel(LayoutManager layout)使用指定的布局管理器创建一个新面板。
*/
public class Demo02 {
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.blue);
//panel设置坐标,
panel.setBounds(50,50,400,400);
panel.setBackground(Color.green);
//frame.add(panel)
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
布局管理器
- 流式布局 FlowLayout
- 东西南北中 BorderLayout
- 表格布局 GridLayout
package com.hong.Day011_Day020.Day014.Demo01;
import java.awt.*;
public class Demo03 {
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");
//流式布局 FlowLayout
frame.setLayout(new FlowLayout());
//frame.setLayout(new FlowLayout(FlowLayout.LEFT));
//frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
//东西南北中 BorderLayout
//frame.add(button1,BorderLayout.NORTH);
//表格布局 GridLayout
//frame.setLayout(new GridLayout(3,2));
frame.setSize(200,200);
frame.add(button1);
frame.add(button2);
frame.add(button3);
//frame.pack();自动最优填充
frame.setVisible(true);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9n6K0Pmt-1617008498105)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20210329132920577.png)]
事件监听
package com.hong.Day011_Day020.Day014.Demo01;
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 Demo04 {
public static void main(String[] args) {
//按下按钮,出发一些事件
Frame frame = new Frame();
Button button = new Button();
//构造一个ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button);
frame.pack();
frame.setVisible(true);
}
//关闭窗口事件
private static void WindowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
文本域
package com.hong.Day011_Day020.Day014.Demo01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo05 {
public static void main(String[] args) {
//启动
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
//TextArea textArea = new TextArea();//多行文本
TextField textField = new TextField();//单行文本
add(textField);
//监听文本框输入的文字 按下Enter就可以获得事件
textField.addActionListener(new Myaction());
//textField.setEchoChar('*');文本设置成*
pack();
setVisible(true);
}
}
class Myaction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();//获取一些资源
System.out.println(field.getText());
}
}
简易计算器
组合:
package com.hong.Day011_Day020.Day014.Demo02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class Demo01 {
public static void main(String[] args) {
Calulator calulator = new Calulator();
calulator.load();
}
}
//计算器类
class Calulator extends Frame{
TextField num1,num2,num3;
Button button;
Label label;
public void load(){
//3个文本框
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
//一个按钮
button = new Button("=");
button.addActionListener(new Myac(this));
//1个标签
label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
}
public Calulator(){
}
}
//监听器类
class Myac implements ActionListener{
//获取三个变量
//private TextField num1,num2,num3;
Calulator calulator = null;
public Myac(/*TextField num1, TextField num2, TextField num3*/Calulator calulator) {
// this.num1 = num1;
// this.num2 = num2;
// this.num3 = num3;
this.calulator = calulator;
}
@Override
public void actionPerformed(ActionEvent e) {
//在一个类中组合另外一个类
//1.获得加数和被加数
/* int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());*/
int n1 = Integer.parseInt(calulator.num1.getText());
int n2 = Integer.parseInt(calulator.num2.getText());
//2.相加
int n3 = n1 + n2;
calulator.num3.setText(""+(n3));
//3.清除两个加数
calulator.num1.setText("");
calulator.num2.setText("");
}
}
内部类:
package com.hong.Day011_Day020.Day014.Demo02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class Demo01 {
public static void main(String[] args) {
Calulator calulator = new Calulator();
calulator.load();
}
}
//计算器类
class Calulator extends Frame{
TextField num1,num2,num3;
Button button;
Label label;
public void load(){
//3个文本框
num1 = new TextField(10);//字符数
num2 = new TextField(10);//字符数
num3 = new TextField(20);//字符数
//一个按钮
button = new Button("=");
button.addActionListener(new Myac());
//1个标签
label = new Label("+");
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
setVisible(true);
}
public Calulator(){
}
//监听器类
private class Myac implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//在一个类中组合另外一个类
//1.获得加数和被加数
/* int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());*/
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.相加
int n3 = n1 + n2;
num3.setText(""+(n3));
//3.清除两个加数
num1.setText("");
num2.setText("");
}
}
}
画笔
package com.hong.Day011_Day020.Day014.Demo02;
import java.awt.*;
public class Demo02 {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//super.paint(g);
//画笔需要有颜色,可以画画
g.setColor(Color.green);
//g.drawOval(100,100,100,100);//空心圆
//g.fillOval(100,100,100,100);//实心圆
//g.fillRect(150,500,200,200);//矩形
}
}
鼠标监听
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-r0L9yxFe-1617008498107)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20210329144919151.png)]
package com.hong.Day011_Day020.Day014.Demo02;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
/*
实现鼠标画画,测试鼠标监听事件
*/
public class Demo03 {
public static void main(String[] args) {
new MyFrame("画图");
}
}
//自己的类
class MyFrame extends Frame{
ArrayList points;
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
public MyFrame(String string){
super(string);
setBounds(200,200,400,300);
//存鼠标点击的点
points = new ArrayList<>();
setVisible(true);
//鼠标监听器
this.addMouseListener(new MyMol());
}
@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);
}
//适配器模式
private class MyMol extends MouseAdapter {
//鼠标点击、按下、弹起、按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame myFrame = (MyFrame) e.getSource();
myFrame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都要重新画一次
myFrame.repaint();//刷新
}
}
}
窗口监听
package com.hong.Day011_Day020.Day014.Demo02;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/*
实现窗口监听
*/
public class Demo04 {
public static void main(String[] args) {
}
}
class WindowsFrame extends Frame {
public WindowsFrame(){
setBackground(Color.blue);
setBounds(100,100,200,200);
setVisible(true);
//addWindowListener(this);
this.addWindowListener(
//匿名内部类
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//正常退出0 非正常退出1
}
});
}
}
键盘监听
package com.hong.Day011_Day020.Day014.Demo02;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Demo05 {
public static void main(String[] args) {
new MyKeyFrame();
}
}
class MyKeyFrame extends Frame{
public MyKeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是哪一个
int key = e.getKeyCode();
System.out.println(key);
if (key == KeyEvent.VK_UP){
System.out.println("你按下了上键");
}
}
});
}
}
Swing
JFrame
标签居中:
label.setHorizontalAlignment(SwingConstants.CENTER);
package com.hong.Day011_Day020.Day014.Demo03;
import javax.swing.*;
import java.awt.*;
/*
JFrame
*/
public class Demo01 {
//init()初始化
public void init(){
//顶级窗口
JFrame frame = new JFrame("这是一个JFrame方法");
frame.setBounds(100,100,200,200);
frame.setVisible(true);
frame.setBackground(Color.blue);
//设置文字 JLabel
JLabel label = new JLabel("one");
frame.add(label);
//让文本标签居中 设置水平对齐
label.setHorizontalAlignment(SwingConstants.CENTER);
//容器实例化 获得一个容器
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.green);
contentPane.setBounds(10,10,200,200);
contentPane.setVisible(true);
//关闭事件
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo01().init();
}
}
JDialog弹窗
package com.hong.Day011_Day020.Day014.Demo03;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
JDialog弹窗
*/
//主窗口
public class Demo02 extends JFrame {
public Demo02(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialog();
}
});
container.add(button);
}
public static void main(String[] args) {
new Demo02();
}
}
//弹窗的窗口
class MyDialog extends JDialog{
public MyDialog() {
this.setVisible(true);
this.setBackground(Color.blue);
this.setBounds(100,100,500,500);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(new FlowLayout());
JLabel label = new JLabel("学JAVA");
label.setHorizontalAlignment(SwingConstants.CENTER);
container.add(label);
}
}
标签
标签Label
new Label("***")
图标Icon
package com.hong.Day011_Day020.Day014.Demo03;
import javax.swing.*;
import java.awt.*;
/*
Icon
*/
public class Iconcon extends JFrame implements Icon {
private int width;
private int heigth;
public Iconcon(){}
public Iconcon(int width, int heigth) {
this.width = width;
this.heigth = heigth;
}
public void init(){
Iconcon icon = new Iconcon(15,15);
//图标放在标签,也可以放在按钮上
JLabel icon1 = new JLabel("icon", icon, SwingConstants.CENTER);
Container container = getContentPane();
container.add(icon1);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,heigth);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.heigth;
}
public static void main(String[] args) {
new Iconcon().init();
}
}
图标ImageIcon
package com.hong.Day011_Day020.Day014.Demo03;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImafeIconDe extends JFrame{
public ImafeIconDe(){
//获取图片的地址
JLabel label = new JLabel("ImafeIcon");
URL url = ImageIcon.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setBounds(100,100,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ImafeIconDe();
}
}
面板
JPanel
package com.hong.Day011_Day020.Day014.Demo04;
import javax.swing.*;
import java.awt.*;
public class Jpanel1 extends JFrame {
public Jpanel1() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//上下的间距
JPanel jp = new JPanel(new GridLayout(1,3));
jp.add(new JButton("1"));
jp.add(new JButton("2"));
jp.add(new JButton("3"));
container.add(jp);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Jpanel1();
}
}
JScrollPane
package com.hong.Day011_Day020.Day014.Demo04;
import javax.swing.*;
import java.awt.*;
public class JScroll1 extends JFrame {
public JScroll1() {
Container container = this.getContentPane();
//文本域
JTextArea jta = new JTextArea(20, 50);
jta.setText("学习JAVA");
//JScrollPane面板
JScrollPane jScroll = new JScrollPane(jta);
container.add(jScroll);
this.setVisible(true);
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScroll1();
}
}
按钮
图片按钮
package com.hong.Day011_Day020.Day014.Demo05;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class JBu11 extends JFrame {
public JBu11() {
Container container = this.getContentPane();
//将一个图片变为图标
URL url = JBu11.class.getResource("tx.jpj");
Icon icon = new ImageIcon(url);
//把这个图标按在按钮上
JButton button = new JButton();
button.setIcon(icon);
button.setToolTipText("图片按钮");//按钮标签
container.add(button);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(500,300);
}
public static void main(String[] args) {
new JBu11();
}
}
单选按钮
JRadioButton button1 = new JRadioButton("button1");
JRadioButton button2 = new JRadioButton("button2");
JRadioButton button3 = new JRadioButton("button3");
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
container.add(button1,BorderLayout.NORTH);
container.add(button2,BorderLayout.EAST);
container.add(button3,BorderLayout.SOUTH);
多选按钮
JCheckBox checkBox1 = new JCheckBox("1");
JCheckBox checkBox1 = new JCheckBox("2");
container.add(checkBox1,BorderLayout.NORTH);
container.add(checkBox1,BorderLayout.EAST);
列表
列表框
//生成列表内容
String[] con = {"1","2","3"};
//列表中需要放入内容
JList list = new JList(con);
下拉框
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在上映");
status.addItem("已下架");
status.addItem("即将上映");
应用场景
- 下拉:选择地区,或者一些单个选项
- 列表:展示信息,一般是动态展示
文本框
文本框
JTextField field = new JTextField();//单行文本
密码框
JPasswordField jPasswordField = new JPasswordField();
文本域
TextArea textArea = new TextArea();//文本域
贪吃蛇游戏
rLayout.NORTH);
container.add(button2,BorderLayout.EAST);
container.add(button3,BorderLayout.SOUTH);
#### 多选按钮
```java
JCheckBox checkBox1 = new JCheckBox("1");
JCheckBox checkBox1 = new JCheckBox("2");
container.add(checkBox1,BorderLayout.NORTH);
container.add(checkBox1,BorderLayout.EAST);
列表
列表框
//生成列表内容
String[] con = {"1","2","3"};
//列表中需要放入内容
JList list = new JList(con);
下拉框
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("正在上映");
status.addItem("已下架");
status.addItem("即将上映");
应用场景
- 下拉:选择地区,或者一些单个选项
- 列表:展示信息,一般是动态展示
文本框
文本框
JTextField field = new JTextField();//单行文本
密码框
JPasswordField jPasswordField = new JPasswordField();
文本域
TextArea textArea = new TextArea();//文本域