7-GUI编程入门到游戏实战【狂神】

Awt是底层自带的类-需要JRE,JRE包含自带的类,界面不美观
Swing封装了好看的类,可以不需要JRE
Awt
1,包含了很多类和接口!GUI
2、元素:窗口,按钮,文本框
3、java.awt-是java中自己的一些类,也就是JRE(JVM+一些自带的类)

组件和容器
Frame
package GUI;
import java.awt.*;
//GUI第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame,JDK,看源码
Frame frame = new Frame("我的第一个JAVA图像界面窗口");
//需要设置可见性
frame.setVisible(true);
//设置大小
frame.setSize(400, 400);
//设置背景颜色
frame.setBackground(new Color(75, 193, 193));
//弹出初始位置
frame.setLocation(200, 200);
//设置大小不可变
frame.setResizable(false);
}
}
创建多个窗口
package GUI;
import java.awt.*;
//GUI第一个界面
public class TestFrame {
public static void main(String[] args) {
//Frame,JDK,看源码
Frame frame = new Frame("我的第一个JAVA图像界面窗口");
//设置大小
frame.setSize(400, 400);
//弹出初始位置
frame.setLocation(200, 200);
//设置背景颜色
frame.setBackground(new Color(75, 193, 193));
//设置大小不可变
frame.setResizable(false);
//需要设置可见性
frame.setVisible(true);
}
}
package GUI;
import javax.swing.*;
import java.awt.*;
import static javax.swing.text.StyleConstants.setBackground;
//展示多个窗口
public class TestFrame2 {
public static void main(String[] args) {
MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.green);
MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.BLACK);
MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.yellow);
MyFrame myFrame4 = 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 GUI;
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(105, 193, 75));
//panel设置坐标,相对于frame
panel.setBounds(50, 50, 400, 400);
panel.setBackground(new Color(193, 89, 75));
//添加panel到frame
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件,addWindowListener 是 Frame 类的一个方法,用于为窗口添加一个窗口事件监听器
frame.addWindowListener(new WindowAdapter() {//这里使用了匿名内部类的方式创建了一个 WindowAdapter 类的实例。WindowAdapter 是一个抽象类,它实现了 WindowListener 接口,并为接口中的所有方法提供了空实现。通过创建 WindowAdapter 的匿名内部类实例,我们可以只重写我们关心的方法,而不必为所有方法提供实现。
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//Java 中用于终止当前 Java 虚拟机(JVM)的方法
}
});
}
}
3种布局管理器
流式布局
package GUI;
import java.awt.*;
public class TestFlowLayout {
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(FlowLayout.LEFT));
frame.setSize(300, 300);
//添加组件
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}

东西南北中
package GUI;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame();
//组件-按钮
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(300, 300);
frame.setVisible(true);
}
}

表格布局
package GUI;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame();
//组件-按钮
Button button1 = new Button("btn1");
Button button2 = new Button("btn2");
Button button3 = new Button("btn3");
Button button4 = new Button("btn4");
Button button5 = new Button("btn5");
Button button6 = new Button("btn6");
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.setSize(300, 300);
frame.setVisible(true);
}
}

事件监听
事件监听:当某个事情发生的时候,干什么?
package GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
public class TestActionEvent {
public static void main(String[] args) {
//按下按钮,触发事件
Frame frame = new Frame();
Button button = new Button();
//因为,addActionListener()方法的参数是一个ActionListener接口的实现类,所以,我们需要创建一个ActionListener的实现类,并重写其中的actionPerformed()方法。
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button, BorderLayout.CENTER);
frame.pack();
windowClose(frame);
frame.setVisible(true);
}
//关闭窗体的事件
private static void windowClose(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
多个按钮共享一个事件
输入框事件监听
package GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText01 {
public static void main(String[] args) {
//只管启动
new MyFrame();
}
}
class MyFrame extends Frame {
public MyFrame() {
TextField textField = new TextField();//建立一个文本框
add(textField);//也能写成this.add(textField),就是指这个MyFrame实例化后的对象
//监听这个文本框输入的文字
MyActionListener myActionListener2 = new MyActionListener();
//按下回车键也能触发输入框监听
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField textField = (TextField) e.getSource();//获得一些资源,返回一个对象
System.out.println(textField.getText()); //获得输入的文字
textField.setText(""); //清空输入框
}
}
简易计算器
组合大于继承
package GUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
//万物皆对象
class Calculator extends Frame {
public Calculator() {
//3个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1, num2, num3));
//1个标签
Label label = new Label("+");
//布局(流式布局)
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class MyCalculatorListener implements ActionListener {
//获取三个变量
private TextField num1;
private TextField num2;
private TextField num3;
public MyCalculatorListener(TextField num1, TextField num2, TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//获得加数和被加数
Integer num1Value = Integer.parseInt(num1.getText());
Integer num2Value = Integer.parseInt(num2.getText());
//将这个值+法运算后,放到第三个框
num3.setText(String.valueOf(num1Value + num2Value));
//清除前两个框
num1.setText("");
num2.setText("");
}
}
画笔
package GUI;
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, 500);
setVisible(true);
}
@Override
public void paint(Graphics g) {
//画笔,需要有颜色
g.setColor(Color.black);
g.drawOval(100, 100, 100, 100);
g.fillOval(100, 100, 100, 100);//实心
g.setColor(Color.red);
g.fillRect(150, 200, 200, 200);
}
}
鼠标监听事件
目的,实现鼠标画画
package GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
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.RED);
g.fillOval(point.x, point.y, 10, 10);
}
}
//添加一个点到界面上
public void addPoint(Point point) {
points.add(point);
}
private class MyMouseListener extends MouseAdapter {
//鼠标 按下 弹起,按住不放
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame =(MyFrame)e.getSource();
//我们点击的时候,就会在界面产生一个点
//这个点就是鼠标点击的位置
frame.addPoint(new Point(e.getX(), e.getY()));
//每次点击鼠标时,都需要重新画
frame.repaint();
}
}
}

窗口监听事件
package GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
setBackground(Color.black);
setBounds(100, 100, 500, 500);
setVisible(true);
//addWindowListener(new MyWindowListner());//两种写法,一种是匿名内部类,一种是内部类,
//如果这个逻辑只适用于 WindowFrame 类,那么在 WindowFrame 类中定义内部类是一个合理的选择。如果这个逻辑需要在多个类中复用,那么可以考虑将其提取为一个独立的类。
addWindowListener(//其实都是默认有this的,可以省略
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("你点了X");
}
}
);
}
class MyWindowListner extends WindowAdapter {//内部类
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口,通过按钮隐藏
System.exit(0);//正常退出
}
}
}
//内部类:
//好处:
//如果MyWindowListener需要在其他地方复用,或者需要实现多个接口,使用内部类是一个很好的选择。
//内部类可以有自己的成员变量和方法,可以更灵活地处理逻辑。
//代码的可读性较高,尤其是在实现多个接口或需要复杂逻辑时。
//适用场景: 当需要复用WindowListener或者实现多个接口时,内部类更为合适。
//匿名内部类:
//好处:
//代码更加简洁明了,尤其是在只需要简单实现一个接口的方法的情况下。
//无需额外的命名,直接在需要的地方实现接口。
//适用场景: 当实现非常简单,不需要复用,且只在一个地方使用时,匿名内部类是一个不错的选择。
键盘监听事件
package GUI;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame {
public KeyFrame() {
setBounds(100, 100, 450, 300);
setVisible(true);
addKeyListener(new KeyAdapter() {
//键盘按下事件
@Override
public void keyPressed(KeyEvent e) {
int keyCod=e.getKeyCode();
if (keyCod==KeyEvent.VK_UP) {
System.out.println("上");
}
}
});
}
}
Swing
窗口、面板
package GUI;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init();初始化
public void init() {
//顶级窗口
JFrame frame = new JFrame("这是一个JFrameDemo");
frame.setVisible(true);
frame.setBounds(100, 100, 450, 300);
frame.setBackground(Color.LIGHT_GRAY);
//设置文字
JLabel label = new JLabel("这是一个JFrameDemo", JLabel.CENTER);
frame.add(label);
//容器实例化
//关闭事件
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JFrameDemo().init();
}
}
package GUI;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJframe2().init();
}
}
class MyJframe2 extends JFrame {
public void init() {
this.setBounds(100, 100, 450, 300);
this.setVisible(true);
JLabel jLabel = new JLabel("Hello World!");
this.add(jLabel);
//让文本标签剧中
jLabel.setHorizontalAlignment(JLabel.CENTER);
//获得一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.YELLOW);
}
}
JDialog弹窗
package GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo() {
this.setVisible(true);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JFrame,放东西的窗口
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("弹出对话框");
button.setBounds(10, 10, 100, 30);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener() {//监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new Dialog();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class Dialog extends JDialog {
public Dialog() {
this.setVisible(true);
this.setSize(200, 100);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("我来了"));
}
}
标签
label
package GUI;
import javax.swing.*;
import java.awt.*;
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(int width, int height) {
this.width = width;
this.height = height;
}
public void init() {
IconDemo iconDemo = new IconDemo(15, 15);
//图标放在标签上,也能放在按钮上
JLabel label = new JLabel("icontest", JLabel.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo(15, 15).init();
}
@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;
}
}
面板
package GUI;
import javax.swing.*;
import java.awt.*;
public class JPanelDemo extends JFrame {
public JPanelDemo() {
Container container = this.getContentPane();//获取面板容器,专门放置JPanel面板
container.setLayout(new GridLayout(2, 1,10, 10));//后面的参数是间距
JPanel panel1 = new JPanel(new GridLayout(1, 3));
JPanel panel2 = new JPanel(new GridLayout(1, 2));
JPanel panel3 = new JPanel(new GridLayout(2, 2));
JPanel panel4 = new JPanel(new GridLayout(3, 2));
panel1.add(new JButton("Button1"));
panel1.add(new JButton("Button2"));
panel1.add(new JButton("Button3"));
panel2.add(new JButton("Button4"));
panel2.add(new JButton("Button5"));
panel3.add(new JButton("Button6"));
panel3.add(new JButton("Button7"));
panel3.add(new JButton("Button8"));
panel3.add(new JButton("Button9"));
panel4.add(new JButton("Button10"));
panel4.add(new JButton("Button11"));
panel4.add(new JButton("Button12"));
panel4.add(new JButton("Button13"));
panel4.add(new JButton("Button14"));
panel4.add(new JButton("Button15"));
panel4.add(new JButton("Button16"));
container.add(panel2);
container.add(panel3);
container.add(panel1);
container.add(panel4);
this.setVisible(true);
this.setSize(500, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JPanelDemo();
}
}

JScrollPanel(滚动条面板)
package GUI;
import javax.swing.;
import java.awt.;
public class JScrollDemo extends JFrame {
public JScrollDemo() {
Container container = this.getContentPane();
//文本哉
JTextArea textArea = new JTextArea(20, 50);
textArea.setText("JScrollDemo");
//Scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100, 100, 450, 300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JScrollDemo();}
}
图片按钮、单选框、多选框
package GUI;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//图片按钮
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
//将图片变成一个图标
URL resource = JButtonDemo01.class.getResource("OIP-C.jpg");
ImageIcon imageIcon = new ImageIcon(resource);
//把这个图标设置到按钮上
JButton button = new JButton();
button.setIcon(imageIcon);
button.setToolTipText("Click Me");
container.add(button);
this.setVisible(true);
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
package GUI;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//单选按键
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
//将图片变成一个图标
URL resource = JButtonDemo01.class.getResource("OIP-C.jpg");
ImageIcon imageIcon = new ImageIcon(resource);
//单选框
JRadioButton radioButton1 = new JRadioButton("单选框1");
JRadioButton radioButton2 = new JRadioButton("单选框2");
JRadioButton radioButton3 = new JRadioButton("单选框3");
//由于单选框只能选中一个,所以需要将其放到一个按钮组中
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioButton1);
buttonGroup.add(radioButton2);
buttonGroup.add(radioButton3);
container.add(radioButton1, BorderLayout.NORTH);
container.add(radioButton2, BorderLayout.SOUTH);
container.add(radioButton3, BorderLayout.CENTER);
this.setVisible(true);
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
package GUI;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
//多选按键
public class JButtonDemo01 extends JFrame {
public JButtonDemo01() {
Container container = this.getContentPane();
//将图片变成一个图标
URL resource = JButtonDemo01.class.getResource("OIP-C.jpg");
ImageIcon imageIcon = new ImageIcon(resource);
//多选框
JCheckBox checkBox = new JCheckBox("Checkbox01");
JCheckBox checkBox1 = new JCheckBox("Checkbox02");
JCheckBox checkBox2 = new JCheckBox("Checkbox03");
container.add(checkBox, BorderLayout.NORTH);
container.add(checkBox1, BorderLayout.CENTER);
container.add(checkBox2, BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JButtonDemo01();
}
}
下拉框,列表框
package GUI;
import javax.swing.;
import java.awt.;
//下拉框
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
Container container = this.getContentPane();
JComboBox status = new JComboBox<>();
status.addItem(null);
status.addItem("正在上映");
status.addItem("已下架");
status.addItem("即将上映");
container.add(status);
this.setVisible(true);
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
package GUI;
import javax.swing.*;
import java.awt.*;
//列表框
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
Container container = this.getContentPane();
//生成列表的内容
String[] items = {"Java", "Python", "C++", "C#", "JavaScript"};
JList jList = new JList(items);
container.add(jList);
this.setVisible(true);
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
应用场景
下拉框:选择地区,或者一些单个选项
列表:展示 信息,一般是动态扩容
文本框
package GUI;
import javax.swing.;
import java.awt.;
//文本框
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
Container container = this.getContentPane();
//container.setLayout(null);//绝对布局
JTextField jTextFields = new JTextField("hello");
container.add(jTextFields);
this.setVisible(true);
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
package GUI;
import javax.swing.*;
import java.awt.*;
//密码框
public class TestComboboxDemo01 extends JFrame {
public TestComboboxDemo01() {
Container container = this.getContentPane();
JPasswordField jPasswordField = new JPasswordField();
jPasswordField.setEchoChar('*');
container.add(jPasswordField);
this.setVisible(true);
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboboxDemo01();
}
}
贪吃蛇
帧数,如果时间片足够小,就是动画,一秒30帧 60帧,连起来就是动画,拆开就是静态的图片

浙公网安备 33010602011771号