****************************************************************************************************************************************************************************
1、百闻不如一经历
package com.day;
import com.day.pojo.SonFrame;// 主函数入口
public class DayApplication {public static void main(String[] args) {new SonFrame();}
}
*******************************************************************
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class SonPanel1 extends JPanel {JButton click;public SonPanel1() {click = new JButton("点击");SonFrame.addClickListener(click);this.add(click);this.setLocation(500, 0);this.setPreferredSize(new Dimension(600, 300));this.setBackground(Color.red);this.setVisible(true);}
}
*******************************************************************
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class SonPanel2 extends JPanel {JButton touch;public SonPanel2() {touch = new JButton("触摸");SonFrame.addClickListener(touch);this.add(touch);this.setLocation(500, 250);this.setPreferredSize(new Dimension(600, 300));this.setBackground(Color.black);this.setVisible(true);}
}
*******************************************************************
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class SonFrame extends JFrame {static SonPanel1 sonPanel1 = new SonPanel1();static SonPanel2 sonPanel2 = new SonPanel2();public SonFrame() {// 流式布局this.setLayout(new FlowLayout());this.add(sonPanel1);this.add(sonPanel2);this.setLocation(500, 250);this.setSize(600, 600);this.setTitle("主体视图");this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void addClickListener(JButton jButton) {jButton.addActionListener(e -> { // lambda表达式String buttonInfo = e.getActionCommand();SonFrame temp = (SonFrame) jButton.getParent().getParent().getParent().getParent().getParent();switch (buttonInfo) {case "点击":sonPanel1.setVisible(false);sonPanel2.setVisible(true);temp.setTitle("展示的是面板2");break;case "触摸":sonPanel1.setVisible(true);sonPanel2.setVisible(false);temp.setTitle("展示的是面板1");break;}});}
}
************************************************
JOptionPane.showMessageDialog(this, "恭喜您全部答对!", "提示", JOptionPane.WARNING_MESSAGE);
************************************************
TextField比JTextField好用,JTextField会导致无法输入,程序卡不动。
****************************************************************************************************************************************************************************
1、GUI编程概述
【1】组件:窗口。弹窗。面板。文本框。列表框。按钮。图片。监听事件。鼠标事件。键盘事件。外挂。IDEA破解工具。
【2】SWING、AWT核心开发技术。因为界面丑所以不太广泛应用。主要是可以写小工具。重点了解监听。
****************************************************************************************************************************************************************************
2、AWT
【1】万物皆对象。包含了很多类与接口。GUI:图形用户界面编程。
【2】元素:窗口。按钮。文本框
【3】核心类:组件Component(按钮、文本、标签...)。Container容器(Windows Frame、Dialog、面板Panel Applet)
****************************************************************************************************************************************************************************
3、Frame操作Frame frame = new Frame("第一个图形窗口");frame.setSize(600, 600);frame.setVisible(true);frame.setBackground(Color.pink);frame.setLocation(20, 20);// 设置大小固定frame.setResizable(false);
****************************************************************************************************************************************************************************
4、面板PanelFrame frame = new Frame();Panel panel = new Panel();// 设置布局frame.setLayout(null);frame.setBounds(300, 300, 500, 500);frame.setBackground(Color.PINK);// panel设置panel.setBounds(50, 50, 400, 400);panel.setBackground(Color.CYAN);frame.add(panel);frame.setVisible(true);// 监听窗口关闭frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});
****************************************************************************************************************************************************************************
5、布局管理
【1】流式布局 FlowLayout。BorderLayout 东西南北中布局。表格布局GridLayout。
【2】先思考80%。代码20%。不要上来就写代码。
****************************************************************************************************************************************************************************
6、事件监听
【1】当某个事情发的时候,需要干什么。Frame frame = new Frame();Button button_1 = new Button();frame.add(button_1);frame.setSize(600, 600);frame.setVisible(true);// 按钮监听button_1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {button_1.setLabel("add label");}});// 关闭监听frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});
****************************************************************************************************************************************************************************
8、计算器
【1】组合(一个类在另一个类作为成员变量使用)大于继承
【2】内部类最大的好处,就是可以畅通无阻的访问外部类
package com.day.pojo;
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 Update {TextField textField_1, textField_2, textField_3;public void loadFrame() {// 窗体Frame frame = new Frame();frame.setLayout(new FlowLayout());// 3个文本框textField_1 = new TextField(10); // 字符数指定textField_2 = new TextField(10);textField_3 = new TextField(20);// 1个按钮Button button = new Button("=");button.addActionListener(new MyActionListener());// 1个标签Label label = new Label("+");// 布局frame.add(textField_1);frame.add(label);frame.add(textField_2);frame.add(button);frame.add(textField_3);// 设置可见frame.setSize(600, 600);frame.setVisible(true);// 设置点击可关闭frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}private class MyActionListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {// 1. 获得操作的数 2.将值加法运算 3.放到3个框,清空12框int num_1 = Integer.parseInt(textField_1.getText());int num_2 = Integer.parseInt(textField_2.getText());int num_3 = num_1 + num_2;textField_1.setText("");textField_2.setText("");textField_3.setText(num_3 + "");}}
}
【3】我的思路
package com.day.pojo;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 Calculator {public Calculator() {// 窗体Frame frame = new Frame();frame.setLayout(new FlowLayout());// 3个文本框TextField textField_1 = new TextField(10); // 字符数指定TextField textField_2 = new TextField(10);TextField textField_3 = new TextField(20);// 1个按钮Button button = new Button("=");button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 1. 获得操作的数 2.将值加法运算 3.放到3个框,清空12框int num_1 = Integer.parseInt(textField_1.getText());int num_2 = Integer.parseInt(textField_2.getText());int num_3 = num_1 + num_2;textField_1.setText("");textField_2.setText("");textField_3.setText(num_3 + "");}});// 1个标签Label label = new Label("+");// 布局frame.add(textField_1);frame.add(label);frame.add(textField_2);frame.add(button);frame.add(textField_3);// 设置可见frame.setSize(600, 600);frame.setVisible(true);// 设置点击可关闭frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}
}
****************************************************************************************************************************************************************************
9、画笔
【1】实际代码
package com.day.pojo;import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class MyPaint {public void doWork() {Frame frame = new Frame() {@Overridepublic void paint(Graphics g) {// super.paint(graphics);g.setColor(Color.red);g.drawOval(100, 100, 100, 100);g.fill3DRect(200, 200, 100, 100, false);// 养成习惯,画笔用完,把他还原成最初的颜色}};frame.setSize(600, 600);frame.setVisible(true);frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}
}
****************************************************************************************************************************************************************************
10、鼠标监听事件
【1】另一种思路
package com.day.pojo;import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;public class MyFrame {private ArrayList myPointList = new ArrayList<MyPoint>();public void doWork() {Frame frame = new Frame() {@Overridepublic void paint(Graphics g) {Iterator iterator = myPointList.iterator();while (iterator.hasNext()) {MyPoint myPoint = (MyPoint) iterator.next();g.setColor(Color.BLACK);g.fillOval(myPoint.getX(), myPoint.getY(), 10, 10);}this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}};// 因为frame为空无法repaint,所以要单独再添加frame.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {System.out.println(e.getX());myPointList.add(new MyPoint(e.getX(), e.getY()));frame.repaint();}});frame.setSize(600, 600);frame.setTitle("画图");frame.setVisible(true);}
}
****************************************************************************************************************************************************************************
11、窗口监听事件
【1】我已经无师自通,使用了匿名内部类
【2】主要也是关闭事件,别的用的很少Frame frame = new Frame() {@Overridepublic void paint(Graphics g) {Iterator iterator = myPointList.iterator();while (iterator.hasNext()) {MyPoint myPoint = (MyPoint) iterator.next();g.setColor(Color.BLACK);g.fillOval(myPoint.getX(), myPoint.getY(), 10, 10);}this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}};
****************************************************************************************************************************************************************************
13、键盘监听事件
【1】开发游戏会用到,和键盘窗口差不多
package com.day.pojo;import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;public class MyFrame {private ArrayList myPointList = new ArrayList<MyPoint>();public void doWork() {Frame frame = new Frame() {@Overridepublic void paint(Graphics g) {this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}};// 因为frame为空无法repaint,所以要单独再添加frame.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {//System.out.println(e.getKeyCode()); // 不用记录数组,用KeyEvent.XXX即可if (e.getKeyCode() == KeyEvent.VK_ENTER) {System.out.println("按下了Enter键盘");}}});frame.setSize(600, 600);frame.setTitle("画图");frame.setVisible(true);}
}
****************************************************************************************************************************************************************************
14、Swing之JFrame窗体
【1】对我来说太简单了
package com.day.pojo;import javax.swing.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");JLabel jLabel = new JLabel("HIT GUI开发");jLabel.setHorizontalAlignment(SwingConstants.CENTER);jFrame.add(jLabel);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
****************************************************************************************************************************************************************************
15、JDialog弹窗
【1】看实现代码
package com.day.pojo;import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);// 按钮JButton jButton = new JButton("点击弹出一个对话框");jButton.setBounds(30, 30, 200, 50);// 监听事件jButton.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {// 弹窗MyDialog myDialog = new MyDialog();}});container.add(jButton);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}class MyDialog {public MyDialog() {// 弹窗JDialog jDialog = new JDialog();jDialog.setVisible(true);jDialog.setBounds(100, 100, 500, 500);Container container = jDialog.getContentPane();container.add(new Label("HIT Window"));}
}
****************************************************************************************************************************************************************************
16、Icon、ImageIcon标签
【1】标签:Label。
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);Icon icon = new Icon() {@Overridepublic void paintIcon(Component c, Graphics g, int x, int y) {g.fillOval(x, 23, 15, 15);}@Overridepublic int getIconWidth() {return 0;}@Overridepublic int getIconHeight() {return 0;}};JLabel jLabel = new JLabel(" 标签", icon, SwingConstants.CENTER);jLabel.setBounds(0, 0, 60, 60);jLabel.setBackground(Color.RED);container.add(jLabel);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
【2】图片
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);ImageIcon imageIcon = new ImageIcon("src/main/resources/1.jpg");JLabel jLabel = new JLabel(" 标签", imageIcon, SwingConstants.CENTER);jLabel.setBounds(100, 100, 300, 300);container.add(jLabel);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
****************************************************************************************************************************************************************************
17、文本域JScroll面板
【1】JPanel
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);// 面板JPanel jPanel = new JPanel();jPanel.setBounds(0, 0, 100, 100);jPanel.add(new JButton("HIT按钮_1"));container.add(jPanel);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
【2】动态面板
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);// 文本域JTextArea jTextArea = new JTextArea(50, 50);jTextArea.setText("欢迎学习JAVA GUI");// Scroll面板JScrollPane jScrollPane = new JScrollPane(jTextArea);jScrollPane.setBounds(200, 200, 200, 200);container.add(jScrollPane);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
****************************************************************************************************************************************************************************
18、图片按钮、单选框、多选框
【1】图片按钮
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);// 图片按钮ImageIcon imageIcon = new ImageIcon("src/main/resources/1.jpg");JButton jButton = new JButton(imageIcon);jButton.setBounds(0, 0, 300, 300);container.add(jButton);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
【2】单选框
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);// 选框JRadioButton jRadioButton_1 = new JRadioButton("单选按钮_1");jRadioButton_1.setBounds(0, 0, 100, 50);JRadioButton jRadioButton_2 = new JRadioButton("单选按钮_2");jRadioButton_2.setBounds(150, 0, 100, 50);JRadioButton jRadioButton_3 = new JRadioButton("单选按钮_3");jRadioButton_3.setBounds(300, 0, 100, 50);ButtonGroup buttonGroup = new ButtonGroup();// 一个组智能选择一个buttonGroup.add(jRadioButton_1);buttonGroup.add(jRadioButton_2);buttonGroup.add(jRadioButton_3);container.add(jRadioButton_1);container.add(jRadioButton_2);container.add(jRadioButton_3);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
【3】多选框
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);// 选框JCheckBox jCheckBox_1 = new JCheckBox("多选_1");jCheckBox_1.setBounds(0, 0, 100, 50);JCheckBox jCheckBox_2 = new JCheckBox("多选_2");jCheckBox_2.setBounds(100, 0, 100, 50);container.add(jCheckBox_1);container.add(jCheckBox_2);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
****************************************************************************************************************************************************************************
19、下拉框、列表框
【1】下拉框:选择一些单个选项
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);// 下拉框JComboBox jComboBox = new JComboBox();jComboBox.setBounds(100, 100, 200, 30);jComboBox.addItem(null);jComboBox.addItem("正在上映");jComboBox.addItem("即将上映");container.add(jComboBox);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
【2】列表框:展示一些信息,动态扩容
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);// 下拉框String[] myArray = {"1", "2", "3"};JList jList = new JList(myArray);jList.setBounds(100, 100, 100, 100);container.add(jList);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
****************************************************************************************************************************************************************************
20、文本框、密码框、文本域
【1】文本框
package com.day.pojo;
import javax.swing.*;
import java.awt.*;
public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);// 文本框JTextField jTextField = new JTextField("Hello");jTextField.setBounds(100, 100, 100, 30);container.add(jTextField);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
【2】密码框
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);// 文本框JPasswordField jPasswordField = new JPasswordField();jPasswordField.setBounds(100, 100, 100, 30);jPasswordField.setEchoChar('*');container.add(jPasswordField);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
【3】文本域
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 获取当前容器,放内容Container container = jFrame.getContentPane();// 绝对布局container.setLayout(null);// 文本框JTextArea jTextArea = new JTextArea("文本区域");jTextArea.setBounds(100, 100, 100, 30);// 面板JScrollPane jScrollPane = new JScrollPane(jTextArea);jScrollPane.setBounds(100, 100, 100, 30);container.add(jScrollPane);jFrame.setSize(600, 600);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
****************************************************************************************************************************************************************************
21、贪吃蛇界面绘制
【1】帧:如果时间足够小,就是动画,一秒30帧 60帧。连起来是动画,拆开就是静态的图片。
package com.day.pojo;import javax.swing.*;
import java.awt.*;public class MyFrame {public void doWork() {JFrame jFrame = new JFrame("这是JFrame窗口");// 游戏面板JPanel jPanel = new JPanel() {// 游戏里的所有东西都使用这个画笔@Overrideprotected void paintComponent(Graphics g) {super.paintComponent(g); // 清屏用的this.setBackground(Color.WHITE);//Data.headImageIcon.paintIcon(this, g, 25, 11); // 广告的位置g.fillRect(25, 75, 850, 600); // 默认游戏界面}};JLabel jLabel = new JLabel("", Data.headImageIcon, SwingConstants.CENTER); // 这么用很快jPanel.add(jLabel);jFrame.add(jPanel);jFrame.setSize(900, 720);jFrame.setResizable(false);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}
----------------------------------
package com.day.pojo;import javax.swing.*;public class Data {public static ImageIcon headImageIcon = new ImageIcon("src/main/resources/res/head.jpg");public static ImageIcon upImageIcon = new ImageIcon("src/main/resources/res/up.png");public static ImageIcon downImageIcon = new ImageIcon("src/main/resources/res/down.png");public static ImageIcon leftImageIcon = new ImageIcon("src/main/resources/res/left.png");public static ImageIcon rightImageIcon = new ImageIcon("src/main/resources/res/right.png");public static ImageIcon bodyImageIcon = new ImageIcon("src/main/resources/res/body.png");public static ImageIcon foodImageIcon = new ImageIcon("src/main/resources/res/food.png");
}
****************************************************************************************************************************************************************************
22-26、贪吃蛇实战(自己解决了一个BUG)
package com.day.pojo;import javax.swing.*;public class Data {public static ImageIcon headImageIcon = new ImageIcon("src/main/resources/image/head.jpg");public static ImageIcon upImageIcon = new ImageIcon("src/main/resources/image/up.png");public static ImageIcon downImageIcon = new ImageIcon("src/main/resources/image/down.png");public static ImageIcon leftImageIcon = new ImageIcon("src/main/resources/image/left.png");public static ImageIcon rightImageIcon = new ImageIcon("src/main/resources/image/right.png");public static ImageIcon bodyImageIcon = new ImageIcon("src/main/resources/image/body.png");public static ImageIcon foodImageIcon = new ImageIcon("src/main/resources/image/food.png");
}
*********************************
package com.day.pojo;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;public class GamePanel extends JPanel implements KeyListener, ActionListener {// 构造函数的使用public GamePanel() {// new对象时就执行JLabel jLabel = new JLabel(new ImageIcon("src/main/resources/image/head.jpg"));this.add(jLabel);init();this.setFocusable(true); //获取焦点this.addKeyListener(this);timer.start(); // 开启定时器}// 定义蛇的数据结构int length; // 小蛇的长度int[] snakeX = new int[600]; // 小蛇的X坐标int[] snakeY = new int[500]; // 小蛇的Y坐标String direct;// 方向boolean startFlag = false;//食物的出现int foodX;int foodY;Random random = new Random();// 是否失败boolean failFlag = false;Integer score;String beforePress;// 初始化小蛇public void init() {length = 3;direct = "R";// 头snakeX[0] = 160;snakeY[0] = 80;// 身体1snakeX[1] = 80;snakeY[1] = 80;//身体2snakeX[2] = 0;snakeY[2] = 80;// 食物随机分布foodX = 80 + 80 * random.nextInt(9);foodY = 80 + 80 * random.nextInt(9);score = 0;}// 定时刷新Timer timer = new Timer(150, this);@Overrideprotected void paintComponent(Graphics g) {super.paintComponent(g); // 清屏用的this.setBackground(Color.WHITE);//Data.headImageIcon.paintIcon(this, g, 25, 11); // 广告的位置g.fillRect(25, 75, 880, 880); // 默认游戏界面// 画积分g.setColor(Color.white);g.setFont(new Font("微软雅黑", Font.BOLD, 40));g.drawString("长度 " + length, 750, 120);g.drawString("分数 " + score, 750, 180);// 画食物Data.foodImageIcon.paintIcon(this, g, foodX, foodY);// 画小蛇System.out.println(beforePress);switch (direct) {case "R":if (beforePress != null && !beforePress.equals("L")) {Data.rightImageIcon.paintIcon(this, g, snakeX[0], snakeY[0]);break;}case "L":if (beforePress != null && !beforePress.equals("R")) {Data.leftImageIcon.paintIcon(this, g, snakeX[0], snakeY[0]);break;}case "U":if (beforePress != null && !beforePress.equals("D")) {Data.upImageIcon.paintIcon(this, g, snakeX[0], snakeY[0]);break;}case "D":if (beforePress != null && !beforePress.equals("U")) {Data.downImageIcon.paintIcon(this, g, snakeX[0], snakeY[0]);break;}}beforePress = direct;for (int i = 1; i < length; i++) {Data.bodyImageIcon.paintIcon(this, g, snakeX[i], snakeY[i]);}// 游戏状态if (!startFlag) {g.setColor(Color.white);g.setFont(new Font("微软雅黑", Font.BOLD, 40));g.drawString("按下空格开始游戏", 300, 300);}// 判断是否失败if (failFlag) {g.setColor(Color.red);g.setFont(new Font("微软雅黑", Font.BOLD, 40));g.drawString("游戏失败,按下空格重新开始!!!", 300, 300);}}@Overridepublic void actionPerformed(ActionEvent e) {if (startFlag && !failFlag) {// System.out.println(snakeX[0] + "VS" + foodX);//System.out.println(snakeY[0] + "VS" + foodY);if (foodX == snakeX[0] && foodY == snakeY[0]) {length++;// 长度+1score = score + 10;// 重新生成食物foodX = 80 + 80 * random.nextInt(9);foodY = 80 + 80 * random.nextInt(9);}// 右移动for (int i = length; i > 0; i--) {snakeX[i] = snakeX[i - 1];snakeY[i] = snakeY[i - 1];}switch (direct) {case "R":snakeX[0] = snakeX[0] + 80;if (snakeX[0] > 880) {snakeX[0] = 30;}break;case "L":snakeX[0] = snakeX[0] - 80;if (snakeX[0] < 0) {snakeX[0] = 880;}break;case "U":snakeY[0] = snakeY[0] - 80;if (snakeY[0] < 0) {snakeY[0] = 880;}break;case "D":snakeY[0] = snakeY[0] + 80;if (snakeY[0] > 880) {snakeY[0] = 0;}break;}// 判断失败for (int i = 1; i < length; i++) {if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {failFlag = true;}}this.repaint(); //重画页面}}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {//System.out.println(e.getKeyCode());int keyCode = e.getKeyCode();if (keyCode == KeyEvent.VK_SPACE) { // 如果按下空格 就开始动if (failFlag) {// 重新开始failFlag = false;init();} else {startFlag = !startFlag; //取反}this.repaint();}// 小蛇移动switch (keyCode) {case KeyEvent.VK_UP:if (beforePress != null && !beforePress.equals("D")) {direct = "U";break;}case KeyEvent.VK_DOWN:if (beforePress != null && !beforePress.equals("U")) {direct = "D";break;}case KeyEvent.VK_LEFT:if (beforePress != null && !beforePress.equals("R")) {direct = "L";break;}case KeyEvent.VK_RIGHT:if (beforePress != null && !beforePress.equals("L")) {direct = "R";break;}}}@Overridepublic void keyReleased(KeyEvent e) {}
}
*************************
package com.day;import com.day.pojo.GamePanel;import javax.swing.*;/*** main function*/
public class DayApplication {public static void main(String[] args) {JFrame jFrame = new JFrame("这是JFrame窗口");// 游戏面板GamePanel gamePanel = new GamePanel();jFrame.add(gamePanel);jFrame.setSize(1000, 1000);jFrame.setResizable(false);jFrame.setVisible(true);jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}
}