首先聲明,代碼是自創的,如有雷同,不勝榮幸!
先談談思路:
1.設計界面。
2.素材的處理。
3.設計圖片加載區域的圖片加載處理類。
4.設計按鈕組中的按鈕初始化方法。
5.設計按鈕組中的隨機圖片加載方法。
6.設計重置按鈕方法。
7.設計按鈕監聽器。
8.設計判定勝利的條件的方法。
1.界面的設計:
a.使用了Windows Builder插件,安裝網頁:WindoswBuilder - http://download.eclipse.org/windowbuilder/WB/integration/4.6/
b.頁面的整體整體樣式:
一個JFram中添加三個帶標題的JPanel,將整個分割為游戲選項區域,圖片展示區域和游戲區域。每個區域都有各自的控件,其中需要注意的是圖片展示區域放置了一個JLable來加載展示圖片。
c.代碼:我將源碼放在了com.rookie.view包下,並且類名為GameView。
1 package com.rookie.view; 2 3 import java.awt.EventQueue; 4 5 import javax.swing.JFrame; 6 import javax.swing.JPanel; 7 import javax.swing.border.EmptyBorder; 8 import javax.swing.JMenuBar; 9 import javax.swing.JMenu; 10 import javax.swing.JMenuItem; 11 import javax.swing.JOptionPane; 12 13 14 import java.awt.event.ActionListener; 15 import java.util.Timer; 16 import java.util.TimerTask; 17 import java.awt.event.ActionEvent; 18 import javax.swing.GroupLayout; 19 import javax.swing.GroupLayout.Alignment; 20 import javax.swing.border.TitledBorder; 21 22 import com.rookie.dao.PicloadDao; 23 import com.rookie.dao.GameDao; 24 25 import javax.swing.UIManager; 26 import java.awt.Color; 27 import javax.swing.LayoutStyle.ComponentPlacement; 28 import javax.swing.JLabel; 29 import javax.swing.JRadioButton; 30 import javax.swing.ButtonGroup; 31 import javax.swing.JComboBox; 32 import javax.swing.DefaultComboBoxModel; 33 import javax.swing.JTextField; 34 import javax.swing.JButton; 35 import javax.swing.SwingConstants; 36 37 public class GamerView extends JFrame { 38 39 /** 40 * 41 */ 42 private static final long serialVersionUID = 1L; 43 private JPanel mainPanel; 44 private final ButtonGroup buttonGroup = new ButtonGroup(); 45 private static JTextField textField_time; 46 private static JButton bt_GameBegin = null; 47 private static JLabel jl_loadImage = null; 48 private static JComboBox comboBox_SelectPic = null; 49 private static JRadioButton rb_simple = null; 50 private static JRadioButton rb_difficulty = null; 51 private static JPanel panel_beginGame = null; 52 private static GameDao gameChoseDao; 53 private static int time = 0; 54 private static Timer timer; 55 /** 56 * Launch the application. 57 */ 58 public static void main(String[] args) { 59 EventQueue.invokeLater(new Runnable() { 60 public void run() { 61 try { 62 GamerView frame = new GamerView(); 63 frame.setVisible(true); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 } 68 }); 69 } 70 71 /** 72 * Create the frame. 73 */ 74 public GamerView() { 75 setResizable(false); 76 setTitle("\u62FC\u56FE\u6E38\u620F(\u6D4B\u8BD5\u7248)"); 77 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 78 setBounds(100, 100, 710, 550); 79 80 JMenuBar menuBar = new JMenuBar(); 81 setJMenuBar(menuBar); 82 83 JMenu m_About = new JMenu("\u5173\u4E8E"); 84 menuBar.add(m_About); 85 86 JMenuItem mI_aboutMe = new JMenuItem("\u56E2\u961F\u4ECB\u7ECD"); 87 mI_aboutMe.addActionListener(new ActionListener() { 88 public void actionPerformed(ActionEvent e) { 89 showAboutActionListener(e); 90 } 91 }); 92 m_About.add(mI_aboutMe); 93 mainPanel = new JPanel(); 94 mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 95 setContentPane(mainPanel); 96 97 JPanel panel_checkGame = new JPanel(); 98 panel_checkGame.setBorder(new TitledBorder(null, "\u6E38\u620F\u9009\u9879\uFF1A", TitledBorder.CENTER, TitledBorder.TOP, null, Color.RED)); 99 100 JPanel panel_loadPic = new JPanel(); 101 panel_loadPic.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u539F\u56FE\u52A0\u8F7D\uFF1A", TitledBorder.CENTER, TitledBorder.TOP, null, Color.RED)); 102 panel_loadPic.setToolTipText(""); 103 104 panel_beginGame = new JPanel(); 105 panel_beginGame.setBorder(new TitledBorder(null, "\u6E38\u620F\u5F00\u59CB\uFF1A", TitledBorder.CENTER, TitledBorder.TOP, null, Color.RED)); 106 GroupLayout gl_mainPanel = new GroupLayout(mainPanel); 107 gl_mainPanel.setHorizontalGroup( 108 gl_mainPanel.createParallelGroup(Alignment.TRAILING) 109 .addGroup(gl_mainPanel.createSequentialGroup() 110 .addContainerGap() 111 .addGroup(gl_mainPanel.createParallelGroup(Alignment.TRAILING) 112 .addComponent(panel_checkGame, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 674, Short.MAX_VALUE) 113 .addGroup(Alignment.LEADING, gl_mainPanel.createSequentialGroup() 114 .addComponent(panel_loadPic, GroupLayout.PREFERRED_SIZE, 336, GroupLayout.PREFERRED_SIZE) 115 .addPreferredGap(ComponentPlacement.UNRELATED) 116 .addComponent(panel_beginGame, GroupLayout.PREFERRED_SIZE, 332, GroupLayout.PREFERRED_SIZE))) 117 .addContainerGap()) 118 ); 119 gl_mainPanel.setVerticalGroup( 120 gl_mainPanel.createParallelGroup(Alignment.LEADING) 121 .addGroup(gl_mainPanel.createSequentialGroup() 122 .addContainerGap() 123 .addComponent(panel_checkGame, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE) 124 .addPreferredGap(ComponentPlacement.UNRELATED) 125 .addGroup(gl_mainPanel.createParallelGroup(Alignment.BASELINE) 126 .addComponent(panel_loadPic, GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE) 127 .addComponent(panel_beginGame, GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)) 128 .addContainerGap()) 129 ); 130 131 jl_loadImage = new JLabel(""); 132 jl_loadImage.setIcon(null); 133 panel_loadPic.add(jl_loadImage); 134 135 136 137 GroupLayout gl_panel_beginGame = new GroupLayout(panel_beginGame); 138 gl_panel_beginGame.setHorizontalGroup( 139 gl_panel_beginGame.createParallelGroup(Alignment.LEADING) 140 .addGap(0, 320, Short.MAX_VALUE) 141 ); 142 gl_panel_beginGame.setVerticalGroup( 143 gl_panel_beginGame.createParallelGroup(Alignment.LEADING) 144 .addGap(0, 322, Short.MAX_VALUE) 145 ); 146 panel_beginGame.setLayout(gl_panel_beginGame); 147 148 JLabel label = new JLabel("\u6E38\u620F\u96BE\u5EA6\uFF1A"); 149 label.setForeground(Color.BLUE); 150 151 rb_simple = new JRadioButton("\u7B80\u5355"); 152 buttonGroup.add(rb_simple); 153 154 rb_difficulty = new JRadioButton("\u56F0\u96BE"); 155 buttonGroup.add(rb_difficulty); 156 157 rb_simple.setSelected(true); 158 159 JLabel label_1 = new JLabel("\u56FE\u7247\u9009\u62E9\uFF1A"); 160 label_1.setForeground(Color.BLUE); 161 162 comboBox_SelectPic = new JComboBox(); 163 comboBox_SelectPic.setModel(new DefaultComboBoxModel(new String[] {"\u98CE\u666F", "\u7F8E\u5973", "\u8C6A\u8F66", "\u6E38\u620F"})); 164 comboBox_SelectPic.setMaximumRowCount(5); 165 166 JLabel label_2 = new JLabel("\u6E38\u620F\u65F6\u95F4\uFF1A"); 167 label_2.setForeground(Color.BLUE); 168 169 textField_time = new JTextField(); 170 textField_time.setHorizontalAlignment(SwingConstants.RIGHT); 171 textField_time.setText("0"); 172 textField_time.setEditable(false); 173 textField_time.setColumns(10); 174 175 JLabel lblNewLabel = new JLabel("\u79D2"); 176 lblNewLabel.setForeground(Color.BLUE); 177 178 bt_GameBegin = new JButton("\u5F00\u59CB\u6E38\u620F"); 179 bt_GameBegin.addActionListener(new ActionListener() { 180 public void actionPerformed(ActionEvent eve) { 181 buttonClickAction(eve); 182 } 183 }); 184 GroupLayout gl_panel_checkGame = new GroupLayout(panel_checkGame); 185 gl_panel_checkGame.setHorizontalGroup( 186 gl_panel_checkGame.createParallelGroup(Alignment.LEADING) 187 .addGroup(gl_panel_checkGame.createSequentialGroup() 188 .addContainerGap() 189 .addComponent(label) 190 .addGap(6) 191 .addGroup(gl_panel_checkGame.createParallelGroup(Alignment.TRAILING) 192 .addComponent(rb_difficulty) 193 .addGroup(gl_panel_checkGame.createSequentialGroup() 194 .addComponent(rb_simple) 195 .addPreferredGap(ComponentPlacement.RELATED))) 196 .addGap(18) 197 .addComponent(label_1) 198 .addPreferredGap(ComponentPlacement.UNRELATED) 199 .addComponent(comboBox_SelectPic, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE) 200 .addGap(32) 201 .addComponent(label_2) 202 .addPreferredGap(ComponentPlacement.UNRELATED) 203 .addComponent(textField_time, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE) 204 .addPreferredGap(ComponentPlacement.RELATED) 205 .addComponent(lblNewLabel) 206 .addGap(52) 207 .addComponent(bt_GameBegin, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE) 208 .addContainerGap(76, Short.MAX_VALUE)) 209 ); 210 gl_panel_checkGame.setVerticalGroup( 211 gl_panel_checkGame.createParallelGroup(Alignment.LEADING) 212 .addGroup(gl_panel_checkGame.createSequentialGroup() 213 .addGroup(gl_panel_checkGame.createParallelGroup(Alignment.LEADING) 214 .addGroup(gl_panel_checkGame.createSequentialGroup() 215 .addComponent(rb_simple) 216 .addPreferredGap(ComponentPlacement.RELATED, 7, Short.MAX_VALUE) 217 .addComponent(rb_difficulty)) 218 .addGroup(gl_panel_checkGame.createSequentialGroup() 219 .addContainerGap() 220 .addGroup(gl_panel_checkGame.createParallelGroup(Alignment.BASELINE) 221 .addComponent(label_1) 222 .addComponent(comboBox_SelectPic, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 223 .addComponent(textField_time, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 224 .addComponent(lblNewLabel) 225 .addComponent(label_2) 226 .addComponent(bt_GameBegin, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE))) 227 .addGroup(gl_panel_checkGame.createSequentialGroup() 228 .addGap(22) 229 .addComponent(label))) 230 .addContainerGap()) 231 ); 232 panel_checkGame.setLayout(gl_panel_checkGame); 233 mainPanel.setLayout(gl_mainPanel); 234 } 235 236 private void showAboutActionListener(ActionEvent e) { 237 // TODO Auto-generated method stub 238 JOptionPane.showMessageDialog(null, "QQ:523980553"); 239 } 240 241 private void buttonClickAction(ActionEvent eve) { 242 // TODO Auto-generated method stub 243 //設置按鈕標題 244 if( bt_GameBegin.getText().equals("開始游戲") ) { 245 beginGame(); 246 timer = new Timer(); 247 timer.scheduleAtFixedRate(new MyTase(), 0, 900); 248 } 249 else { 250 resetGame(); 251 252 } 253 254 } 255 256 public static void beginGame() { 257 bt_GameBegin.setText("重置游戲"); 258 //獲取ComBox選項 259 int picId = comboBox_SelectPic.getSelectedIndex(); 260 //加載圖片 261 PicloadDao pic = new PicloadDao(); 262 pic.loadPic(picId, jl_loadImage); 263 //獲取難易度 264 if(rb_simple.isSelected()) { 265 gameChoseDao = new GameDao(); 266 gameChoseDao.initButton(panel_beginGame); 267 gameChoseDao.randomLoadPic(picId); 268 269 }else if(rb_difficulty.isSelected()) { 270 271 } 272 } 273 274 public static void resetGame() { 275 bt_GameBegin.setText("開始游戲"); 276 rb_simple.setSelected(true); 277 comboBox_SelectPic.setSelectedIndex(0); 278 textField_time.setText(""+0); 279 jl_loadImage.setIcon(null); 280 gameChoseDao.resetButton(); 281 time = 0; 282 timer.cancel(); 283 } 284 285 class MyTase extends TimerTask{ 286 @Override 287 public void run() { 288 // TODO Auto-generated method stub 289 time ++; 290 textField_time.setText("" + time); 291 if(time == 60) { 292 JOptionPane.showMessageDialog(null, "挑戰失敗!!!"); 293 resetGame(); 294 } 295 } 296 } 297 }
代碼被分成了幾部分:一部分是界面的初始化,這些都可以通過Windows builder來完成,但是從按下“開始游戲”的按鈕后,之后的任務都需要手動寫代碼,例如事件的處理,開始游戲和重置游戲的方法書寫,計時器的書寫。都是需要獨立完成,技術比較菜,可能代碼不是很完善,請原諒。只是基礎,不在詳細去說。
2.素材的處理。
a.統一規范的素材命名。
b.利用PS將圖進行切片,可以完成圖片分成均勻的幾份。
需要注意的是:如果你使用的是Win8或者Win10,你需要使用管理員權限打開PS。
3.設計圖片加載區域的圖片加載處理類。
因為我們使用的是png后綴的素材圖片,所以需要稍微處理一下。
new ImageIcon(GameVime.class,getResource();
1 package com.rookie.dao; 2 3 import java.io.File; 4 5 import javax.swing.ImageIcon; 6 import javax.swing.JLabel; 7 import javax.swing.JOptionPane; 8 9 import com.rookie.view.GamerView; 10 11 12 public class PicloadDao { 13 14 public PicloadDao() { 15 super(); 16 } 17 18 public void loadPic(int id,JLabel lable) { 19 String imageStr = "/LoadImage"+File.separator+"image_" + id + ".png"; 20 if(lable != null) { 21 try { 22 lable.setIcon(new ImageIcon(GamerView.class.getResource(imageStr))); 23 }catch(NullPointerException e){ 24 JOptionPane.showMessageDialog(null, "圖片加載失敗!!!"); 25 } 26 } 27 } 28 }
加入了異常處理機制,再出現問題時候可以及時提醒。
4.設計按鈕組中的按鈕初始化方法。
剛開始的時候想法是給每一個按鈕一個標題,但是后來發現標題會影響到圖片的布局,所以取消了標題的設計,之后就是用記錄每一個按鈕初始化時的位置,之后判定勝利的時候更加方便。
1 public void initButton(JPanel panel) { 2 panel.setLayout(null); 3 int wNum = 0; 4 int hNum = 0; 5 for(int i=0;i< buttons.length;i++) { 6 buttons[i] = new JButton(); 7 buttons[i].setVisible(true); 8 p[i] = new Point( y+width*wNum++,x+height*hNum); 9 buttons[i].setBounds(p[i].x, p[i].y, width, height); 10 if((i+1)%3==0) { 11 hNum++; 12 wNum=0; 13 } 14 buttons[i].addActionListener(this); 15 panel.add(buttons[i]); 16 } 17 }
5.設計按鈕組中的隨機圖片加載方法。
這個方法核心在於隨機數的產生,while()循環能夠保證不出現重復的隨機數,不會出現兩個或者多個按鈕加載了同一張圖片。
public void randomLoadPic(int picId) { int rNum = 0; randomNum = new int[9]; for(int i=0;i < buttons.length;i++) { try { rNum = (int)(Math.random()*9); while(buttons[rNum].getIcon() != null) { rNum = (int)(Math.random()*9)%9; } randomNum[i] = rNum; StringBuffer str = new StringBuffer( "/LoadImage/image_" + picId + "/simple/image_" + picId + "_0" + (i+1) + ".png"); buttons[rNum].setIcon(new ImageIcon(GamerView.class.getResource(str.toString()))); buttons[rNum].setActionCommand(""+rNum); randomNum[i] = rNum; }catch(Exception e) { JOptionPane.showMessageDialog(null, "異常錯誤,即將退出"); System.exit(0); } } }
異常處理機制保證再出現找不到圖片或者其他錯誤是能正常退出游戲,本來用的是String類但是運行的時候一直再報NullPointException錯誤,所以改用了StringBuffer,完美解決錯誤。當然,也不排除我開始的時候素材沒有處理好。
6.設計重置按鈕方法。
1 public void resetButton() { 2 for(int i=0;i < buttons.length;i++) { 3 try { 4 buttons[i].setIcon(null); 5 buttons[i].setVisible(false); 6 buttons[i] = null; 7 }catch(NullPointerException e) { 8 JOptionPane.showMessageDialog(null, "發生錯誤!!"); 9 } 10 } 11 }
很簡單,就是讓圖片清空,不可見,能夠使得界面初始化。
7.設計按鈕監聽器。
到了最關鍵的地方:按鈕監聽時候需要分成兩個部分,一個是首次點擊,一個是第二次點擊,首次點擊的時候我們只需要記錄當前按下的按鈕信息。第二次點擊之后需要判定當前按鈕是否在之間按鈕的上下左右四個位置,如果是的話,就進行交換,如果不是就提示錯誤。思路就這么簡單。
1 public void actionPerformed(ActionEvent e) { 2 if(beFlag == true) { //第一次點擊 3 beButton = (JButton)e.getSource(); 4 beFlag = false; 5 }else { 6 JButton nowButton = (JButton)e.getSource(); 7 int nX = nowButton.getX(); 8 int nY = nowButton.getY(); 9 int bX = beButton.getX(); 10 int bY = beButton.getY(); 11 beFlag = false; 12 if((Math.abs(nX-bX)<width && Math.abs(nY-bY)<=height)||(Math.abs(nX-bX)<=width && Math.abs(nY-bY)<height)) { 13 beButton.setBounds(nX, nY, width, height); 14 nowButton.setBounds(bX,bY,width,height); 15 }else { 16 JOptionPane.showMessageDialog(null, "選取錯誤!!!"); 17 } 18 this.successGame(); 19 } 20 }
getSource()可以獲取源數據,建議單獨拉一個代碼試一試。
8.設計判定勝利的條件的方法。
終於到了最后的判定勝利,剛開始的思路就是一一對應,加載第一張圖片的放在位置一上,甚至我還用了getIcon,發現判定失敗,額,然后轉換了一下思路,用記錄的隨機數來解決。buttons[romdom[i]] = point[i]。生成的第一個隨機加載的圖片應該放在第一個點上。思路沒問題吧。
1 public void successGame() { 2 for(int i=0;i<9;i++) { 3 ps = buttons[randomNum[i]].getLocation(); 4 if( ps.x == p[i].x && ps.y == p[i].y) { 5 6 }else { 7 return ; 8 } 9 } 10 JOptionPane.showMessageDialog(null, "勝利!"); 11 GamerView.resetGame(); 12 }
解決了所有問題,但是代碼還有許多地方需要去修改,比如難度選定之后需要加載對應的素材,為了保證代碼的不會有很多地方重復,還需要修改,但是大致的框架已經完成,而且完了兩局沒有發現什么Bug。
最后附上所有代碼:

1 package com.rookie.view; 2 3 import java.awt.EventQueue; 4 5 import javax.swing.JFrame; 6 import javax.swing.JPanel; 7 import javax.swing.border.EmptyBorder; 8 import javax.swing.JMenuBar; 9 import javax.swing.JMenu; 10 import javax.swing.JMenuItem; 11 import javax.swing.JOptionPane; 12 13 14 import java.awt.event.ActionListener; 15 import java.util.Timer; 16 import java.util.TimerTask; 17 import java.awt.event.ActionEvent; 18 import javax.swing.GroupLayout; 19 import javax.swing.GroupLayout.Alignment; 20 import javax.swing.border.TitledBorder; 21 22 import com.rookie.dao.PicloadDao; 23 import com.rookie.dao.GameDao; 24 25 import javax.swing.UIManager; 26 import java.awt.Color; 27 import javax.swing.LayoutStyle.ComponentPlacement; 28 import javax.swing.JLabel; 29 import javax.swing.JRadioButton; 30 import javax.swing.ButtonGroup; 31 import javax.swing.JComboBox; 32 import javax.swing.DefaultComboBoxModel; 33 import javax.swing.JTextField; 34 import javax.swing.JButton; 35 import javax.swing.SwingConstants; 36 37 public class GamerView extends JFrame { 38 39 /** 40 * 41 */ 42 private static final long serialVersionUID = 1L; 43 private JPanel mainPanel; 44 private final ButtonGroup buttonGroup = new ButtonGroup(); 45 private static JTextField textField_time; 46 private static JButton bt_GameBegin = null; 47 private static JLabel jl_loadImage = null; 48 private static JComboBox comboBox_SelectPic = null; 49 private static JRadioButton rb_simple = null; 50 private static JRadioButton rb_difficulty = null; 51 private static JPanel panel_beginGame = null; 52 private static GameDao gameChoseDao; 53 private static int time = 0; 54 private static Timer timer; 55 /** 56 * Launch the application. 57 */ 58 public static void main(String[] args) { 59 EventQueue.invokeLater(new Runnable() { 60 public void run() { 61 try { 62 GamerView frame = new GamerView(); 63 frame.setVisible(true); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 } 68 }); 69 } 70 71 /** 72 * Create the frame. 73 */ 74 public GamerView() { 75 setResizable(false); 76 setTitle("\u62FC\u56FE\u6E38\u620F(\u6D4B\u8BD5\u7248)"); 77 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 78 setBounds(100, 100, 710, 550); 79 80 JMenuBar menuBar = new JMenuBar(); 81 setJMenuBar(menuBar); 82 83 JMenu m_About = new JMenu("\u5173\u4E8E"); 84 menuBar.add(m_About); 85 86 JMenuItem mI_aboutMe = new JMenuItem("\u56E2\u961F\u4ECB\u7ECD"); 87 mI_aboutMe.addActionListener(new ActionListener() { 88 public void actionPerformed(ActionEvent e) { 89 showAboutActionListener(e); 90 } 91 }); 92 m_About.add(mI_aboutMe); 93 mainPanel = new JPanel(); 94 mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); 95 setContentPane(mainPanel); 96 97 JPanel panel_checkGame = new JPanel(); 98 panel_checkGame.setBorder(new TitledBorder(null, "\u6E38\u620F\u9009\u9879\uFF1A", TitledBorder.CENTER, TitledBorder.TOP, null, Color.RED)); 99 100 JPanel panel_loadPic = new JPanel(); 101 panel_loadPic.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u539F\u56FE\u52A0\u8F7D\uFF1A", TitledBorder.CENTER, TitledBorder.TOP, null, Color.RED)); 102 panel_loadPic.setToolTipText(""); 103 104 panel_beginGame = new JPanel(); 105 panel_beginGame.setBorder(new TitledBorder(null, "\u6E38\u620F\u5F00\u59CB\uFF1A", TitledBorder.CENTER, TitledBorder.TOP, null, Color.RED)); 106 GroupLayout gl_mainPanel = new GroupLayout(mainPanel); 107 gl_mainPanel.setHorizontalGroup( 108 gl_mainPanel.createParallelGroup(Alignment.TRAILING) 109 .addGroup(gl_mainPanel.createSequentialGroup() 110 .addContainerGap() 111 .addGroup(gl_mainPanel.createParallelGroup(Alignment.TRAILING) 112 .addComponent(panel_checkGame, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 674, Short.MAX_VALUE) 113 .addGroup(Alignment.LEADING, gl_mainPanel.createSequentialGroup() 114 .addComponent(panel_loadPic, GroupLayout.PREFERRED_SIZE, 336, GroupLayout.PREFERRED_SIZE) 115 .addPreferredGap(ComponentPlacement.UNRELATED) 116 .addComponent(panel_beginGame, GroupLayout.PREFERRED_SIZE, 332, GroupLayout.PREFERRED_SIZE))) 117 .addContainerGap()) 118 ); 119 gl_mainPanel.setVerticalGroup( 120 gl_mainPanel.createParallelGroup(Alignment.LEADING) 121 .addGroup(gl_mainPanel.createSequentialGroup() 122 .addContainerGap() 123 .addComponent(panel_checkGame, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE) 124 .addPreferredGap(ComponentPlacement.UNRELATED) 125 .addGroup(gl_mainPanel.createParallelGroup(Alignment.BASELINE) 126 .addComponent(panel_loadPic, GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE) 127 .addComponent(panel_beginGame, GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)) 128 .addContainerGap()) 129 ); 130 131 jl_loadImage = new JLabel(""); 132 jl_loadImage.setIcon(null); 133 panel_loadPic.add(jl_loadImage); 134 135 136 137 GroupLayout gl_panel_beginGame = new GroupLayout(panel_beginGame); 138 gl_panel_beginGame.setHorizontalGroup( 139 gl_panel_beginGame.createParallelGroup(Alignment.LEADING) 140 .addGap(0, 320, Short.MAX_VALUE) 141 ); 142 gl_panel_beginGame.setVerticalGroup( 143 gl_panel_beginGame.createParallelGroup(Alignment.LEADING) 144 .addGap(0, 322, Short.MAX_VALUE) 145 ); 146 panel_beginGame.setLayout(gl_panel_beginGame); 147 148 JLabel label = new JLabel("\u6E38\u620F\u96BE\u5EA6\uFF1A"); 149 label.setForeground(Color.BLUE); 150 151 rb_simple = new JRadioButton("\u7B80\u5355"); 152 buttonGroup.add(rb_simple); 153 154 rb_difficulty = new JRadioButton("\u56F0\u96BE"); 155 buttonGroup.add(rb_difficulty); 156 157 rb_simple.setSelected(true); 158 159 JLabel label_1 = new JLabel("\u56FE\u7247\u9009\u62E9\uFF1A"); 160 label_1.setForeground(Color.BLUE); 161 162 comboBox_SelectPic = new JComboBox(); 163 comboBox_SelectPic.setModel(new DefaultComboBoxModel(new String[] {"\u98CE\u666F", "\u7F8E\u5973", "\u8C6A\u8F66", "\u6E38\u620F"})); 164 comboBox_SelectPic.setMaximumRowCount(5); 165 166 JLabel label_2 = new JLabel("\u6E38\u620F\u65F6\u95F4\uFF1A"); 167 label_2.setForeground(Color.BLUE); 168 169 textField_time = new JTextField(); 170 textField_time.setHorizontalAlignment(SwingConstants.RIGHT); 171 textField_time.setText("0"); 172 textField_time.setEditable(false); 173 textField_time.setColumns(10); 174 175 JLabel lblNewLabel = new JLabel("\u79D2"); 176 lblNewLabel.setForeground(Color.BLUE); 177 178 bt_GameBegin = new JButton("\u5F00\u59CB\u6E38\u620F"); 179 bt_GameBegin.addActionListener(new ActionListener() { 180 public void actionPerformed(ActionEvent eve) { 181 buttonClickAction(eve); 182 } 183 }); 184 GroupLayout gl_panel_checkGame = new GroupLayout(panel_checkGame); 185 gl_panel_checkGame.setHorizontalGroup( 186 gl_panel_checkGame.createParallelGroup(Alignment.LEADING) 187 .addGroup(gl_panel_checkGame.createSequentialGroup() 188 .addContainerGap() 189 .addComponent(label) 190 .addGap(6) 191 .addGroup(gl_panel_checkGame.createParallelGroup(Alignment.TRAILING) 192 .addComponent(rb_difficulty) 193 .addGroup(gl_panel_checkGame.createSequentialGroup() 194 .addComponent(rb_simple) 195 .addPreferredGap(ComponentPlacement.RELATED))) 196 .addGap(18) 197 .addComponent(label_1) 198 .addPreferredGap(ComponentPlacement.UNRELATED) 199 .addComponent(comboBox_SelectPic, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE) 200 .addGap(32) 201 .addComponent(label_2) 202 .addPreferredGap(ComponentPlacement.UNRELATED) 203 .addComponent(textField_time, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE) 204 .addPreferredGap(ComponentPlacement.RELATED) 205 .addComponent(lblNewLabel) 206 .addGap(52) 207 .addComponent(bt_GameBegin, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE) 208 .addContainerGap(76, Short.MAX_VALUE)) 209 ); 210 gl_panel_checkGame.setVerticalGroup( 211 gl_panel_checkGame.createParallelGroup(Alignment.LEADING) 212 .addGroup(gl_panel_checkGame.createSequentialGroup() 213 .addGroup(gl_panel_checkGame.createParallelGroup(Alignment.LEADING) 214 .addGroup(gl_panel_checkGame.createSequentialGroup() 215 .addComponent(rb_simple) 216 .addPreferredGap(ComponentPlacement.RELATED, 7, Short.MAX_VALUE) 217 .addComponent(rb_difficulty)) 218 .addGroup(gl_panel_checkGame.createSequentialGroup() 219 .addContainerGap() 220 .addGroup(gl_panel_checkGame.createParallelGroup(Alignment.BASELINE) 221 .addComponent(label_1) 222 .addComponent(comboBox_SelectPic, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 223 .addComponent(textField_time, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 224 .addComponent(lblNewLabel) 225 .addComponent(label_2) 226 .addComponent(bt_GameBegin, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE))) 227 .addGroup(gl_panel_checkGame.createSequentialGroup() 228 .addGap(22) 229 .addComponent(label))) 230 .addContainerGap()) 231 ); 232 panel_checkGame.setLayout(gl_panel_checkGame); 233 mainPanel.setLayout(gl_mainPanel); 234 } 235 236 private void showAboutActionListener(ActionEvent e) { 237 // TODO Auto-generated method stub 238 JOptionPane.showMessageDialog(null, "QQ:523980553"); 239 } 240 241 private void buttonClickAction(ActionEvent eve) { 242 // TODO Auto-generated method stub 243 //設置按鈕標題 244 if( bt_GameBegin.getText().equals("開始游戲") ) { 245 beginGame(); 246 timer = new Timer(); 247 timer.scheduleAtFixedRate(new MyTase(), 0, 900); 248 } 249 else { 250 resetGame(); 251 252 } 253 254 } 255 256 public static void beginGame() { 257 bt_GameBegin.setText("重置游戲"); 258 //獲取ComBox選項 259 int picId = comboBox_SelectPic.getSelectedIndex(); 260 //加載圖片 261 PicloadDao pic = new PicloadDao(); 262 pic.loadPic(picId, jl_loadImage); 263 //獲取難易度 264 if(rb_simple.isSelected()) { 265 gameChoseDao = new GameDao(); 266 gameChoseDao.initButton(panel_beginGame); 267 gameChoseDao.randomLoadPic(picId); 268 269 }else if(rb_difficulty.isSelected()) { 270 271 } 272 } 273 274 public static void resetGame() { 275 bt_GameBegin.setText("開始游戲"); 276 rb_simple.setSelected(true); 277 comboBox_SelectPic.setSelectedIndex(0); 278 textField_time.setText(""+0); 279 jl_loadImage.setIcon(null); 280 gameChoseDao.resetButton(); 281 time = 0; 282 timer.cancel(); 283 } 284 285 class MyTase extends TimerTask{ 286 @Override 287 public void run() { 288 // TODO Auto-generated method stub 289 time ++; 290 textField_time.setText("" + time); 291 if(time == 60) { 292 JOptionPane.showMessageDialog(null, "挑戰失敗!!!"); 293 resetGame(); 294 } 295 } 296 } 297 }

1 package com.rookie.dao; 2 3 import java.io.File; 4 5 import javax.swing.ImageIcon; 6 import javax.swing.JLabel; 7 import javax.swing.JOptionPane; 8 9 import com.rookie.view.GamerView; 10 11 12 public class PicloadDao { 13 14 public PicloadDao() { 15 super(); 16 } 17 18 public void loadPic(int id,JLabel lable) { 19 String imageStr = "/LoadImage"+File.separator+"image_" + id + ".png"; 20 if(lable != null) { 21 try { 22 lable.setIcon(new ImageIcon(GamerView.class.getResource(imageStr))); 23 }catch(NullPointerException e){ 24 JOptionPane.showMessageDialog(null, "圖片加載失敗!!!"); 25 } 26 } 27 } 28 }

1 package com.rookie.dao; 2 3 import java.awt.Point; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 import javax.swing.ImageIcon; 8 import javax.swing.JButton; 9 import javax.swing.JOptionPane; 10 import javax.swing.JPanel; 11 12 import com.rookie.view.GamerView; 13 14 public class GameDao implements ActionListener{ 15 //按鈕組 16 private JButton []buttons = new JButton[9]; 17 //按鈕初始化組表 18 private final int x = 30; 19 private final int y = 55; 20 //按鈕大小 21 private final int width = 73; 22 private final int height = 107; 23 private boolean beFlag = true; 24 private JButton beButton = null; 25 private int []randomNum = new int[9]; 26 private Point p[] = new Point[9]; 27 private Point ps; 28 public GameDao() { 29 super(); 30 // TODO Auto-generated constructor stub 31 } 32 33 public void initButton(JPanel panel) { 34 panel.setLayout(null); 35 int wNum = 0; 36 int hNum = 0; 37 for(int i=0;i< buttons.length;i++) { 38 buttons[i] = new JButton(); 39 buttons[i].setVisible(true); 40 p[i] = new Point( y+width*wNum++,x+height*hNum); 41 buttons[i].setBounds(p[i].x, p[i].y, width, height); 42 if((i+1)%3==0) { 43 hNum++; 44 wNum=0; 45 } 46 buttons[i].addActionListener(this); 47 panel.add(buttons[i]); 48 } 49 } 50 51 public void randomLoadPic(int picId) { 52 int rNum = 0; 53 randomNum = new int[9]; 54 for(int i=0;i < buttons.length;i++) { 55 try { 56 rNum = (int)(Math.random()*9); 57 while(buttons[rNum].getIcon() != null) { 58 rNum = (int)(Math.random()*9)%9; 59 } 60 randomNum[i] = rNum; 61 StringBuffer str = new StringBuffer( 62 "/LoadImage/image_" + picId + "/simple/image_" + picId + "_0" + (i+1) + ".png"); 63 buttons[rNum].setIcon(new ImageIcon(GamerView.class.getResource(str.toString()))); 64 buttons[rNum].setActionCommand(""+rNum); 65 randomNum[i] = rNum; 66 }catch(Exception e) { 67 JOptionPane.showMessageDialog(null, "異常錯誤,即將退出"); 68 System.exit(0); 69 } 70 71 } 72 } 73 74 public void resetButton() { 75 for(int i=0;i < buttons.length;i++) { 76 try { 77 buttons[i].setIcon(null); 78 buttons[i].setVisible(false); 79 buttons[i] = null; 80 }catch(NullPointerException e) { 81 JOptionPane.showMessageDialog(null, "發生錯誤!!"); 82 } 83 } 84 } 85 86 public void actionPerformed(ActionEvent e) { 87 if(beFlag == true) { //第一次點擊 88 beButton = (JButton)e.getSource(); 89 beFlag = false; 90 }else { 91 JButton nowButton = (JButton)e.getSource(); 92 int nX = nowButton.getX(); 93 int nY = nowButton.getY(); 94 int bX = beButton.getX(); 95 int bY = beButton.getY(); 96 97 if((Math.abs(nX-bX)<width && Math.abs(nY-bY)<=height)||(Math.abs(nX-bX)<=width && Math.abs(nY-bY)<height)) { 98 beButton.setBounds(nX, nY, width, height); 99 nowButton.setBounds(bX,bY,width,height); 100 }else { 101 JOptionPane.showMessageDialog(null, "選取錯誤!!!"); 102 } 103 this.successGame(); 104 } 105 } 106 107 public void successGame() { 108 for(int i=0;i<9;i++) { 109 ps = buttons[randomNum[i]].getLocation(); 110 if( ps.x == p[i].x && ps.y == p[i].y) { 111 112 }else { 113 return ; 114 } 115 } 116 JOptionPane.showMessageDialog(null, "勝利!"); 117 GamerView.resetGame(); 118 } 119 }
有很多地方寫的並不是很好,但是思路就是這樣,可能不是盡善盡美,但是新手上路,還希望多多包涵!!!!
代碼我會打包放在百度雲:
鏈接:http://pan.baidu.com/s/1gfCcblD 密碼:jffk
新手上路,請多照顧,此處是測試版,並沒有完全完成,如果想溝通,請聯系我。