JAVA石头迷阵
石頭迷陣
有報錯,請大神看看,是不是接口錯誤
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.sql.JDBCType; import java.util.Random;public class MyJFrame extends JFrame implements KeyListener, ActionListener {//定義一個二維數(shù)組,方便管理數(shù)據(jù)int[][] datas = new int[4][4];//定義一個二維數(shù)組,元素是最終勝利的元素int [][] victoy = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};//定義兩個變量,用來記錄空白位置int x0 = 0;int y0 = 0;//統(tǒng)計走了多少步的變量int step = 0;//定義兩個條目JMenuItem jMenuItem1;JMenuItem jMenuItem2;//構(gòu)造方法public MyJFrame(){//初始化界面initFrame();//初始化菜單initMenu();//初始化數(shù)據(jù)initData();//初始化圖片initImage();//窗體顯示this.setVisible(true);}public void initFrame(){//窗體寬高設(shè)置this.setSize(514, 595);//窗體標題this.setTitle("石頭迷陣單機版 v1.0");//窗體關(guān)閉模式this.setDefaultCloseOperation(3);//讓窗體位于屏幕正中央this.setLocationRelativeTo(null);//窗體置頂this.setAlwaysOnTop(true);//讓界面添加鍵盤監(jiān)聽this.addKeyListener(this);//取消內(nèi)部居中放置方式this.setLayout(null);}public void initMenu(){//創(chuàng)建一個菜單對象JMenuBar jMenuBar = new JMenuBar();//設(shè)置菜單寬高jMenuBar.setSize(514, 20);//創(chuàng)建一個選項JMenu jMenu1 = new JMenu("功能");JMenu jMenu2 = new JMenu("關(guān)于");//創(chuàng)建一個條目jMenuItem1 = new JMenuItem("重新游戲");jMenuItem2 = new JMenuItem("聯(lián)系我們");//給重新開始游戲這個條目綁定一個事件jMenuItem1.addActionListener(this);jMenuItem2.addActionListener(this);//白條目添加到選項當中jMenu1.add(jMenuItem1);jMenu2.add(jMenuItem2);//把選項添加到菜單中jMenuBar.add(jMenu1);jMenuBar.add(jMenu2);//把菜單添加到最外層的窗體中this.setJMenuBar(jMenuBar);}public void initData(){//創(chuàng)建數(shù)組int [] temp = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};//打亂數(shù)組中數(shù)據(jù)的順序// 1. 遍歷數(shù)組依次得到數(shù)組中的每一個數(shù)字for (int i = 0; i < temp.length; i++) {//temp[i]; --當前遍歷到數(shù)組中的數(shù)字// 2. 獲取數(shù)組中的一個隨機索引Random r = new Random();int index = r.nextInt(temp.length);// 3. 將當前遍歷到的數(shù)字,跟隨機索引上的數(shù)字兩者進行交換int number = temp[i];temp[i] = temp[index];temp[index] = number;}//把打亂后的數(shù)據(jù)添加到二維數(shù)組中//把arr中的16個元素添加到二維數(shù)組中for (int i = 0; i < temp.length; i++) {datas[i / 4][i % 4] = temp[i];if (temp[i] == 0){x0 = i / 4;y0 = i % 4;}}}public void initImage(){/*//添加圖片ImageIcon imageIcon1 = new ImageIcon("image\\1.png");//創(chuàng)建一個JLabel對象, 利用JLabel對象來設(shè)置圖片的位置和寬高JLabel jLabel1 = new JLabel(imageIcon1);//設(shè)置jLabel的位置和寬高//第一個參數(shù),第二個參數(shù)表示位置//第三個參數(shù),第四個參數(shù)表示寬高jLabel1.setBounds(0 + 50,0 + 90,100,100);//把JLabel添加到整個窗體中this.add(jLabel1);ImageIcon imageIcon2 = new ImageIcon("image\\2.png");JLabel jLabel2 = new JLabel(imageIcon2);jLabel2.setBounds(100 + 50,0 + 90,100,100);this.add(jLabel2);*//*for (int i = 1; i <= 4; i++){ImageIcon imageIcon = new ImageIcon("image\\" + i + ".png");JLabel jLabel = new JLabel(imageIcon);jLabel.setBounds((i-1) * 100 + 50,0 + 90, 100,100);this.add(jLabel);}*///將界面中原來所有的圖片全部刪除this.getContentPane().removeAll();JLabel label_step = new JLabel("步數(shù):" + step);label_step.setBounds(50,20,100,20);this.add(label_step);//判斷游戲是否勝利if (victory()){//如果該方法結(jié)果是true//表示游戲勝利ImageIcon imageIcon = new ImageIcon("image\\win.png");JLabel jLabel = new JLabel(imageIcon);jLabel.setBounds(514/2-266/2, 230,266,88);this.add(jLabel);}for (int i = 0; i < datas.length; i++){for (int j = 0; j < datas[i].length; j++) {int data = datas[i][j];if (data != 0){ImageIcon imageIcon = new ImageIcon("image\\" + data + ".png");JLabel jLabel = new JLabel(imageIcon);jLabel.setBounds(j * 100 + 50,i * 100 + 90,100,100);this.add(jLabel);}}}//游戲背景ImageIcon background = new ImageIcon("image\\background.png");JLabel backgroundjLabel = new JLabel(background);backgroundjLabel.setBounds(26,30,450,484);this.add(backgroundjLabel);//將整個界面重新繪制this.getContentPane().repaint();}public boolean victory(){//只要判斷兩個數(shù)組中的元素是否相同//datas 和 victoryfor (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if (datas[i][j] != victoy[i][j]){//return -- 可以寫在方法當中// 1. 停止方法// 2. 將return后面的數(shù)據(jù),返回給調(diào)用者return false;}}}return true;}@Overridepublic void keyTyped(KeyEvent e) {}//按下@Overridepublic void keyPressed(KeyEvent e) {}//松開@Overridepublic void keyReleased(KeyEvent e) {//獲取按鍵的數(shù)字int keyCode = e.getKeyCode();move(keyCode);//表示重新繪制界面initImage();}public void move(int keyCode){if (keyCode == 37){//左//表示空格右邊的圖片要向左移動datas[x0][y0] = datas[x0][y0 + 1];datas[x0][y0 + 1] = 0;y0++;step++;}else if (keyCode == 38){//上//表示把空格下面的圖片向上移動datas[x0][y0] = datas[x0 + 1][y0];datas[x0 + 1][y0] = 0;x0++;step++;}else if (keyCode == 39){//右//表示把空格左邊的圖片向右移動datas[x0][y0] = datas[x0][y0 - 1];datas[x0][y0 - 1] = 0;y0--;step++;}else if (keyCode == 40){//下datas[x0][y0] = datas[x0 - 1][y0];datas[x0 - 1][y0] = 0;x0--;step++;}else if (keyCode == 87){//W 表示作弊嗎datas = new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};}else {System.out.println("只能按上下左右鍵");}}@Overridepublic void actionPerformed(ActionEvent e) {//判斷//如果點擊的是重新開始,那么才執(zhí)行下面的代碼//如果點擊的是關(guān)于我們,彈出一個對話框if (e.getSource() == jMenuItem1){//重新數(shù)據(jù)打亂數(shù)據(jù)的順序initData();//計步器清零step = 0;//繪制整個界面initImage();}else if (e.getSource() == jMenuItem2){//點擊了聯(lián)系我們//創(chuàng)建了一個彈框?qū)ο驤Dialog jDialog = new JDialog();//創(chuàng)建了一個圖片對象ImageIcon imageIcon = new ImageIcon("image\\about.png");JLabel jLabel = new JLabel(imageIcon);jLabel.setBounds(0,0,344,344);//把圖片放到彈框當中jDialog.add(jLabel);//設(shè)置彈框大小jDialog.setSize(344,344);//要把彈框設(shè)置為頂層 -- 置頂效果jDialog.setAlwaysOnTop(true);// 要讓jDialog居中顯示jDialog.setLocationRelativeTo(null);//讓jDialog顯示出來jDialog.setVisible(true);}else {System.out.println("沒有這個按鍵");}//System.out.println("點擊了重新開始按鈕");} }總結(jié)
- 上一篇: 安卓通过链接打开淘宝客户端
- 下一篇: 新一代人工智能知识体系大全