GUI编程入门到游戏实战

Source

组件

  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘事件
  • 破解工具

1. 简介

GUI的核心技术 :Swing,AWT

  • 界面不美观
  • 需要jar环境

有什么用?

  • 可以写一些小具
  • 工作时候可能需要维护swing界面,概率极小
  • 了解MVC架构,了解监听

2. AWT

2.1 AWT介绍

  • 包含了很多类和接口!GUI!
  • 元素:窗口,按钮,文本框
  • java.awt

在这里插入图片描述

2.2 组件和容器

2.2.1. Frame

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package 第一个Frame窗口;
import java.awt.*;

//GUI的第一个界面
public class TestFrame {
public static void main(String[] args) {
Frame frame=new Frame("我的第一个Java图像界面窗口"); //Frame ,JDK,看源码
frame.setVisible(true); //需要设置可见性 w,h
frame.setSize(500,500); //设置窗口大小
//frame.setBackground(Color.red); //设置背景颜色
frame.setBackground(new Color(69, 142, 239));
frame.setLocation(200,200); //弹出初始位置
frame.setResizable(false); //设置大小固定
}
}

运行效果图

在这里插入图片描述

问题:发现窗口关闭不掉,停止java程序即可

尝试回顾封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package 第一个Frame窗口;
import java.awt.*;

public class TestFrame01 {
public static void main(String[] args) {
MyFrame MyFrame01=new MyFrame(100,100,300,300,Color.red);
MyFrame MyFrame02=new MyFrame(400,100,300,300,Color.yellow);
MyFrame MyFram03=new MyFrame(100,400,300,300,Color.green);
MyFrame MyFrame04=new MyFrame(400,400,300,300,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);
setVisible(true);
//setSize(x,y);
//setLocation(w,h);
setBounds(x,y,w,h);
}
}

在这里插入图片描述

2.2.2. Panel面板

解决了关闭事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package Panel面板;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//Panel可以看成是一个空间,但是不能单独存在
public class TestPanel {
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(120, 239, 46, 255));
panel.setBounds(50,50,400,400); //panel设置坐标,相对于frame
panel.setBackground(new Color(239, 93, 93));
frame.add(panel);
frame.setVisible(true);//设置是否可见
//监听事件,监听窗口关闭事件,System.exit(0),强行关闭,正常关闭.System.exit(1),异常关闭
//适配器模式
frame.addWindowListener(new WindowAdapter() {
//窗口点击关闭时需要做的事情
@Override
public void windowClosing(WindowEvent e) {
System.exit(0); //结束程序
}
});
}
}

在这里插入图片描述

2.3 布局管理器

  • 流式布局
  • 东西南北中
  • 表格布局

流式布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package FlowLayOut;
import java.awt.*;

/**
* 3种布局管理器
* (1)流式布局
* (2)东西南北中
* (3)表格布局
*/
public class TestFlowLayOut {
public static void main(String[] args) {
Frame frame=new Frame();
// 流式布局
//组件,按钮
Button button = new Button("button");
Button button1 = new Button("button1");
Button button2 = new Button("button2");
//frame.setLayout(new FlowLayout());//FlowLayout.CENTER //设置为流式布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT));
//frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.setSize(200,200);
//把按钮添加上去
frame.add(button);
frame.add(button1);
frame.add(button2);
frame.setVisible(true);
}
}

在这里插入图片描述

东西南北中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package 布局管理器;
import java.awt.*;
import static java.awt.BorderLayout.*;

/* 东西南北中 */
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 south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");

//frame.add(east,EAST);
//frame.add(west,WEST);
//frame.add(south,SOUTH);
//frame.add(north,NORTH);
//frame.add(center,CENTER);
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);

frame.setSize(200,200);
frame.setVisible(true);
}
}

在这里插入图片描述

表格布局 Grid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package 布局管理器;
import java.awt.*;

/* 表格布局 */
public class TestGridLayout {
public static void main(String[] args) {
Frame frame=new Frame("TestGridLayout");

Button grid1 = new Button("grid1");
Button grid2 = new Button("grid2");
Button grid3 = new Button("grid3");
Button grid4 = new Button("grid4");
Button grid5 = new Button("grid5");
Button grid6 = new Button("grid6");
frame.setLayout(new GridLayout(3,2));
frame.add(grid1);
frame.add(grid2);
frame.add(grid3);
frame.add(grid4);
frame.add(grid5);
frame.add(grid6);
frame.pack();//Java函数
frame.setVisible(true);
}
}

在这里插入图片描述

思考

在这里插入图片描述

分析过程

在这里插入图片描述

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package 布局管理器;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Exercise {
public static void main(String[] args) {
Frame frame=new Frame("Exercise"); //总窗口
frame.setSize(400,300);
frame.setBackground(new Color(1,1,1));
frame.setLocation(500,300);
frame.setVisible(true);
//分
frame.setLayout(new GridLayout(2,1));
//上板面
Panel p1=new Panel(new BorderLayout());
Panel p2=new Panel(new GridLayout(2,1));
p1.add(new Button("east-1"),BorderLayout.EAST);
p1.add(new Button("west-1"),BorderLayout.WEST);
p2.add(new Button("north-1"));
p2.add(new Button("north-2"));
//下板面
Panel p3=new Panel(new BorderLayout());
Panel p4=new Panel(new GridLayout(2,2));
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("south-"+i));
}
//组合
p1.add(p2,BorderLayout.CENTER);
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
//监听关闭窗口
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

在这里插入图片描述

总结
  1. Frame是一个顶级窗口
  2. Panel无法单独显示,必须添加到某个容器中
  3. 布局管理器
    1. 流式布局
    2. 东西南北中
    3. 表格
  4. 大小,定位,背景颜色,可见性,监听!

2.4 事件监听

事件监听:当某个事件发生的时候,干什么?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package 事件监听;
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 TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发事件
Frame frame=new Frame("TestActionEvent");
Button button=new Button("button");
//因为addActionListener()需要一个ActionListener,所以我们需要构建一个ActionListener
MyActionListenner myActionListenner=new MyActionListenner();
button.addActionListener(myActionListenner);

frame.add(button,BorderLayout.CENTER);
frame.pack();

windowClose(frame);//关闭窗口
frame.setVisible(true);
}
//关闭窗口事件
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

//事件监听
class MyActionListenner implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}

多个按钮,共享一个事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package 事件监听;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/* 两个按钮,实现同一个监听 */
public class TestActionEvent02 {
public static void main(String[] args) {
//开始 停止
Frame frame=new Frame("开始-停止");
Button start = new Button("start");
Button stop = new Button("stop");
//可以定义触发所显示返回的信息,如果不定义,则会走默认值!
stop.setActionCommand("button-stop");
//可以多个按钮只写一个监听类,按钮可根据监听类,判断按钮实现的操作
MyMonitor myMonitor = new MyMonitor();
start.addActionListener(myMonitor);
stop.addActionListener(myMonitor);
frame.add(start,BorderLayout.NORTH);
frame.add(stop,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
}
//事件监听
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()获取按钮信息
System.out.println("按钮被点击了,msg:"+e.getActionCommand());
if (e.getActionCommand().equals("start")){

}
}
}

2.5 输入文本框TextField监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package 事件监听;
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) {
new MyFrame(); //启动
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField=new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener01 myActionListener01 = new MyActionListener01();
//按下回车就会触发这个输入框的事件
textField.addActionListener(myActionListener01);
//设置替换编码
textField.setEchoChar('*');//设置编码格式,一般用于隐藏密码
pack();
setVisible(true);
windowsClose( this);
}
public static void windowsClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener01 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field=(TextField) e.getSource();//获的一些资源
System.out.println(field.getText());//获得文本框的文本
field.setText("");
}
}

2.6 简易计算机,组合+内部类回顾复习!

oop(面向对象)原则:先组合,大于继承!

在这里插入图片描述

目前代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package 事件监听;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/* 简易计算器 */
public class TestCalculator {
public static void main(String[] args) {
//new Calculator();
TestActionEvent.windowClose(new Calculator());//直接调用之间创建的窗口关闭事件
}
}
//计算器类
class Calculator extends Frame{
public Calculator() {
//3个文本框
TextField textField1 = new TextField(10);//字符数
TextField textField2 = new TextField(10);
TextField textField3 = new TextField(20);
//一个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(textField1,textField2,textField3));
//一个标签
Label label = new Label("+");
//布局
setLayout(new FlowLayout());
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
}
}
class MyCalculatorListener implements ActionListener{
//获取三个变量
private TextField textField1,textField2,textField3;
public MyCalculatorListener(TextField textField1,TextField textField2,TextField textField3){
this.textField1=textField1;
this.textField2=textField2;
this.textField3=textField3;
}
@Override
public void actionPerformed(ActionEvent e) {
//获取加数和被加数
//parseInt将字符串参数作为有符号的十进制整数进行解析
int n1=Integer.parseInt(textField1.getText());
int n2=Integer.parseInt(textField2.getText());
//将这个值+法运算后,放到第三个框
textField3.setText(""+(n1+n2));
//清除前两个框
textField1.setText("");
textField2.setText("");
}
}

完全改造成面向对象写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package 事件监听;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCalculator02 {
public static void main(String[] args) {
new Calculator01().loadFrame();
}
}
//计算器类
class Calculator01 extends Frame {
TextField textField1,textField2,textField3; //属性
public void loadFrame(){ //方法
textField1=new TextField(10);
textField2=new TextField(10);
textField3=new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener01(this));
setLayout(new FlowLayout()); //布局
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
pack();
setVisible(true);
}
}

class MyCalculatorListener01 implements ActionListener {
//获取计算机这个对象,在一个类中组合另外一个类
Calculator01 calculator01=null;
public MyCalculatorListener01(Calculator01 calculator01){
this.calculator01=calculator01;
}
@Override
public void actionPerformed(ActionEvent e) {
//获取加数和被加数
//将这个值+法运算后,放到第三个框
//清除前两个框
int n1=Integer.parseInt(calculator01.textField1.getText());
int n2=Integer.parseInt(calculator01.textField2.getText());
calculator01.textField3.setText(""+(n1+n2));
calculator01.textField1.setText("");
calculator01.textField2.setText("");
}
}

内部类:

  • 更好的包装
  • 内部类最好的方法,就是可以畅通无阻的访问外部类的属性和方法!
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    package 事件监听;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class TestCalculator03 {
    public static void main(String[] args) {
    new Calculator02().loadFrame();
    }
    }
    class Calculator02 extends Frame { //计算器类
    TextField textField1,textField2,textField3; //属性
    public void loadFrame(){ //方法
    textField1=new TextField(10);
    textField2=new TextField(10);
    textField3=new TextField(20);
    Button button = new Button("=");
    Label label = new Label("+");
    button.addActionListener(new MyCalculatorListener02());
    setLayout(new FlowLayout()); //布局
    add(textField1);
    add(label);
    add(textField2);
    add(button);
    add(textField3);
    pack();
    setVisible(true);
    }
    private class MyCalculatorListener02 implements ActionListener { //监听器
    @Override
    public void actionPerformed(ActionEvent e) {
    //获取加数和被加数
    //将这个值+法运算后,放到第三个框
    //清除前两个框
    int n1=Integer.parseInt(textField1.getText());
    int n2=Integer.parseInt(textField2.getText());
    textField3.setText(""+(n1+n2));
    textField1.setText("");
    textField2.setText("");
    }
    }
    }
    在这里插入图片描述

2.7 画笔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package 画笔Paint;
import java.awt.*;

/* 画笔 */
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,400);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//画笔,需要有颜色,画笔可以画画
//g.setColor(new Color(157, 3, 253, 255));
g.drawOval(100,100,100,100);
g.fillOval(100,200,200,200);//实心的圆

//g.setColor(Color.RED);
g.fillRect(200,100,200,200);
//养成习惯,画笔用完,将他还原到最初的颜色
}
}

2.8 鼠标监听

目的:想要实现鼠标画画

在这里插入图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package 鼠标监听;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

/* 鼠标监听事件 */
public class TestMouseListener {
public static void main(String[] args) { new MyFrame("画图"); }
}
class MyFrame extends Frame {
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList points;
public MyFrame(String title) {
super(title);
setBounds(200,200,400,300);
points=new ArrayList(); //鼠标点击的点的集合
setVisible(true);
this.addMouseListener(new MyMouseListener()); //鼠标监听器,正对这个窗口
}

@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 MyMouseListener extends MouseAdapter{ //适配器模式
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame=(MyFrame)e.getSource();
//这个点击时,就会在界面上产生一个点!画
//这个点就是鼠标的点
frame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
frame.repaint();
}
}
}

2.9 窗口监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package 窗口监听;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindow {
public static void main(String[] args) { new WindowsFrame("窗口监听"); }
}
class WindowsFrame extends Frame{
public WindowsFrame(String title){
super(title);
setBackground(Color.yellow);
setBounds(100,100,400,400);
setVisible(true);
addWindowListener(new WindowAdapter() {//匿名内部类
@Override
public void windowClosing(WindowEvent e) { //关闭窗口
setVisible(false);//隐藏窗口
//System.exit(0);
System.out.println("windowClosing");
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowsFrame windowsFrame=(WindowsFrame) e.getSource();
windowsFrame.setTitle("被激活了");
System.out.println("windowActivated");
}
});
}
}

2.10 键盘监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package 键盘监听;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.security.UnresolvedPermission;

/* 键盘监听 */
public class TestKeyListener {
public static void main(String[] args) { new KeyFrame("键盘监听"); }
}
class KeyFrame extends Frame{
public KeyFrame(String title){
super(title);
setBackground(new Color(255, 251, 0));
setBounds(100,100,400,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("你按压了上键");
}
}
});
}
}

3. Swing

3.1 窗口、面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package Swing之JFrame窗体;
import javax.swing.*;
import java.awt.*;

public class TestJFrame {
public void init(){ //init();初始化
JFrame jFrame=new JFrame("这是一个JFrame窗口"); //顶级窗口
jFrame.setBounds(100,100,400,400);
jFrame.setVisible(true);
JLabel jLabel=new JLabel("这是我创建的第一个JFrame窗口"); //设置文字JLabel
jFrame.add(jLabel);
jLabel.setHorizontalAlignment(SwingConstants.CENTER); //让文本标签居中,设置水平对齐
//容器
jFrame.getContentPane().setBackground(Color.yellow);//背景颜色需要设置在容器中
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new TestJFrame().init(); //创建一个窗口
}
}

3.2 弹窗

JDialog,用来被弹出,默认就有关闭事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package JDialog弹窗;
import Swing之JFrame窗体.TestJFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestJDialog extends JFrame { //主窗口
public TestJDialog(String title){
super(title);
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame放东西,容器
Container container=this.getContentPane();//Container集装箱
container.setLayout(null); //绝对布局
JButton button = new JButton("点击我,弹出一个新弹窗"); //按钮
button.setBounds(50,50,200,60);
container.add(button);
//创建按钮监听事件,点击一个按钮时,弹出一个弹窗
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FirstDialog();
}
});

}

public static void main(String[] args) {
new TestJDialog("主窗口");
}
}
class FirstDialog extends JDialog{
public FirstDialog(){
this.setVisible(true);
this.setBounds(300,300,400,400);
Container container= this.getContentPane();//Container集装箱
//container.setBackground(Color.yellow);
container.add(new Label("我是弹窗"));
}
}

3.3 标签

label

1
new JLabel("xxx")

图标 ICON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package IconImageIcon标签;
import javax.swing.*;
import java.awt.*;

/* 图标,需要实现类,Frame继承 */
public class TestIcon extends JFrame implements Icon {
private int width;
private int height;
public TestIcon() { }

public TestIcon(int width, int height) {
this.width = width;
this.height = height;
}

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}

@Override
public int getIconWidth() { return this.width; }

@Override
public int getIconHeight() { return this.height; }

public static void main(String[] args) {
new TestIcon().init();
}
public void init(){
TestIcon testIcon =new TestIcon(15,15);
//图标可以放在标签上,也可以放在按钮上
JLabel jLabel=new JLabel("icontest",testIcon,SwingConstants.CENTER);
Container container= getContentPane();
container.add(jLabel);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

在这里插入图片描述

图片 ImageIcon

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package IconImageIcon标签;
import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class TestImageIcon extends JFrame {
public static void main(String[] args) {
new TestImageIcon();
}
public TestImageIcon (){
//获取图片地址
JLabel jLabel=new JLabel("ImageIcon");
URL url=TestImageIcon.class.getResource("下载.jpg");
ImageIcon imageIcon=new ImageIcon(url);
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container container=getContentPane();
container.add(jLabel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

在这里插入图片描述

3.4 面板

JPanel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package 文本域JScroll面板;
import javax.swing.*;
import java.awt.*;

public class TestJPanel extends JFrame {
public TestJPanel(String title){
super(title);
Container container=getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//后面两个参数的意思是面板的间距
JPanel jPanel1=new JPanel(new GridLayout(1,3));
JPanel jPanel2=new JPanel(new GridLayout(1,2));
JPanel jPanel3=new JPanel(new GridLayout(2,1));
JPanel jPanel4=new JPanel(new GridLayout(3,2));
jPanel1.add(new Button("1"));
jPanel1.add(new Button("1"));
jPanel1.add(new Button("1"));
jPanel2.add(new Button("2"));
jPanel2.add(new Button("2"));
jPanel3.add(new Button("3"));
jPanel3.add(new Button("3"));
jPanel4.add(new Button("4"));
jPanel4.add(new Button("4"));
jPanel4.add(new Button("4"));
jPanel4.add(new Button("4"));
jPanel4.add(new Button("4"));
jPanel4.add(new Button("4"));
container.add(jPanel1);
container.add(jPanel2);
container.add(jPanel3);
container.add(jPanel4);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new TestJPanel("JPanel面板");
}
}

在这里插入图片描述

JScrollPanel面板(可滚动面板)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package 文本域JScroll面板;
import javax.swing.*;
import java.awt.*;

/* 滚屏 */
public class TestJScroll extends JFrame {
public static void main(String[] args) {
new TestJScroll("JScrollPanel");
}
public TestJScroll(String title){
super(title);
Container container=getContentPane();
JTextArea textArea = new JTextArea(20,50); //文本域
textArea.setText("开启你的Java之旅");
JScrollPane jScrollPane = new JScrollPane(textArea); //Scroll面板
container.add(jScrollPane);
setVisible(true);
setBounds(100,100,400,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

在这里插入图片描述

3.5 按钮

图片按钮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package 图片按钮单选框多选框;
import javax.swing.*;
import java.awt.*;
import java.net.URL;

/* 图片按钮 */
public class TestJButton extends JFrame {
public static void main(String[] args) {
TestJButton testJButton=new TestJButton();
}
public TestJButton(){
Container container=this.getContentPane();
URL url=TestJButton.class.getResource("下载.jpg"); //将一个图片变成一个图标
ImageIcon imageIcon = new ImageIcon(url);
JButton jbutton = new JButton("按钮"); //将图片放在按钮上
jbutton.setIcon(imageIcon);
jbutton.setToolTipText("图片按钮");
container.add(jbutton);
this.setVisible(true);
this.setBounds(100,100,400,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

在这里插入图片描述

  • 单选按钮

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    package 图片按钮单选框多选框;
    import javax.swing.*;
    import java.awt.*;
    import java.net.URL;

    /* 单选框,单选按钮 */
    public class TestJButton02 extends JFrame {
    public static void main(String[] args) {
    new TestJButton02();
    }
    public TestJButton02(){
    Container container=this.getContentPane();
    //单选框
    JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
    JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
    JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");
    //由于单选框只能选一个,分组,一个分组中只能选一个
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(jRadioButton1);
    buttonGroup.add(jRadioButton2);
    buttonGroup.add(jRadioButton3);
    container.add(jRadioButton1,BorderLayout.NORTH);
    container.add(jRadioButton2,BorderLayout.CENTER);
    container.add(jRadioButton3,BorderLayout.SOUTH);
    this.setVisible(true);
    this.setBounds(100,100,400,300);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    }

    在这里插入图片描述

  • 复选按钮

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    package 图片按钮单选框多选框;
    import javax.swing.*;
    import java.awt.*;

    public class TestJButton03 extends JFrame {
    public static void main(String[] args) {
    new TestJButton03();
    }
    public TestJButton03(){
    Container container=this.getContentPane();
    //多选框
    JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
    JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
    container.add(jCheckBox1,BorderLayout.NORTH);
    container.add(jCheckBox2,BorderLayout.SOUTH);
    this.setVisible(true);
    this.setBounds(100,100,400,300);
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    }

    在这里插入图片描述

3.6 列表

  • 下拉框
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package 下拉框列表框;
    import javax.swing.*;
    import java.awt.*;

    /* 下拉框 */
    public class TestCombobox extends JFrame {
    public static void main(String[] args) {
    new TestCombobox();
    }
    public TestCombobox(){
    Container contentPane = getContentPane();
    JComboBox<Object> objectJComboBox = new JComboBox<>();
    objectJComboBox.addItem(null);
    objectJComboBox.addItem("语文");
    objectJComboBox.addItem("数学");
    objectJComboBox.addItem("英语");
    contentPane.add(objectJComboBox);
    pack();
    setVisible(true);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    }
              snakeX[i]=snakeX[i-1];
              snakeY[i]=snakeY[i-1];
          }
          //走向
          if (fx.equals("R")){
              snakeX[0]=snakeX[0]+25;
              //边界判断
              if (snakeX[0]>850){
                  snakeX[0]=25;
              }
          }
          if (fx.equals("L")){
              snakeX[0]=snakeX[0]-25;
              //边界判断
              if (snakeX[0]<25){
                  snakeX[0]=850;
              }
          }
          if (fx.equals("U")){
              snakeY[0]=snakeY[0]-25;
              //边界判断
              if (snakeY[0]<75){
                  snakeY[0]=650;
              }
          }
          if (fx.equals("D")){
              snakeY[0]=snakeY[0]+25;
              //边界判断
              if (snakeY[0]>650){
                  snakeY[0]=75;
              }
          }
          //判断失败,撞到自己就算失败
          for (int i=1;i<length;i++){
              if (snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                  isFail=true;
              }
          }
          repaint();//重画页面
      }
      timer.start();//定时器开启
    
    }
    }
    
    

在这里插入图片描述