一.创建新项目
二.插入图片
三.游戏的主界面
1.代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.border.BevelBorder;
public class GameJFrame extends JFrame implements KeyListener,ActionListener{
int[][] data = new int[4][4];
String path="images\\\\";
JMenuItem replayItem = new JMenuItem("重新游戏");
JMenuItem reLoginItem = new JMenuItem("重新登入");
JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("公众号");
this.setTitle("拼图单机版1.0");
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.addKeyListener(this);
JMenuBar jMenuBar = new JMenuBar();
JMenu functionJMenu = new JMenu("功能");
JMenu aboutJMenu = new JMenu("关于我们");
functionJMenu.add(replayItem);
functionJMenu.add(reLoginItem);
functionJMenu.add(closeItem);
aboutJMenu.add(accountItem);
replayItem.addActionListener(this);
reLoginItem.addActionListener(this);
closeItem.addActionListener(this);
accountItem.addActionListener(this);
jMenuBar.add(functionJMenu);
jMenuBar.add(aboutJMenu);
this.setJMenuBar(jMenuBar);
private void initData() {
int[] tempArr = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
for(int i=0;i
int index = r.nextInt(tempArr.length);
tempArr[i] = tempArr[index];
for(int i=0;i
data[i /4][i%4] = tempArr[i];
private void initImage() {
this.getContentPane().removeAll();
JLabel winJLabel = new JLabel(new ImageIcon("C:\\Users\\徐梦\\Desktop\\images\\s.png"));
winJLabel.setBounds(60, 59, 500, 500);
this.getContentPane().add(winJLabel);
JLabel stepCount =new JLabel("步数: " +step);
stepCount.setBounds(50, 30, 100, 20);
this.getContentPane().add(stepCount);
JLabel jLabel = new JLabel(new ImageIcon(path+num+".gif"));
jLabel.setBounds(151 * j+10, 121 *i+110, 151, 121);
jLabel.setBorder(new BevelBorder(1));
this.getContentPane().add(jLabel);
this.getContentPane().repaint();
public void keyTyped(KeyEvent e) {
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
this.getContentPane().removeAll();
JLabel all = new JLabel(new ImageIcon("C:\\Users\\徐梦\\Desktop\\images\\all.png"));
all.setBounds(10, 110, 603, 485);
this.getContentPane().add(all);
this.getContentPane().repaint();
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
System.out.println("向左");
data[x][y] = data[x ][y-1];
System.out.println("向上");
data[x][y] = data[x + 1][y];
System.out.println("向右");
data[x][y] = data[x ][y-1];
System.out.println("向下");
data[x][y] = data[x - 1][y];
public boolean victory() {
for(int i=0;i
for(int j=0;j
if(data[i][j] != win[i][j]) {
public void actionPerformed(ActionEvent e) {
Object obj =e.getSource();
System.out.println("重新游戏");
}else if(obj == reLoginItem) {
System.out.println("重新登入");
}else if(obj == closeItem) {
System.out.println("关闭游戏");
}else if(obj == accountItem) {
System.out.println("公众号");
2.程序的启动入口
public static void main(String[] args) {
3.结果
-
相关阅读:
mongostat性能分析
感受 OpenDNS
Git本地实现服务器搭建
算法与数据结构(第二周)——排序基础:插入排序法
前端技能树,面试复习第 47 天—— Vue-Router 详解
ollama 模型国内加速下载,制作自定义Modelfile模型文件
Apache ShenYu网关初体验
蓝桥杯实战应用【算法代码篇】-次数差(附Java、Python和C++代码)
【unity资源加载与优化章】Profiler优化工具详解
杭州高职画室哪家好?如何选择高职画室?高职美术学习选哪家画室?
-
原文地址:https://blog.csdn.net/m0_74981829/article/details/134497177