資源簡介
在界面上設計4*4的按鈕,即16個按鈕排列成4*4的網格形狀。在16個按鈕中,只有15個按鈕上有1-15的數字,而且這些數字在按鈕上不重復出現,有一個按鈕上沒有數字。當程序啟動時,這15個數字是隨機排列的。當單擊某個按鈕時,若該按鈕上有數字而且該按鈕與沒有數字的按鈕相鄰,則將該按鈕上的數字給沒有數字的按鈕,同時該按鈕變成沒有數字的按鈕。當著15個數字在4*4的網格中成順序或逆序排列,則顯示消息框表明排列成功,并重新隨機排列這15個數字在按鈕網格上的位置。
代碼片段和文件信息
import?java.awt.Container;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.util.Vector;
import?javax.swing.JButton;
import?javax.swing.Jframe;
import?javax.swing.JOptionPane;
import?javax.swing.JPanel;
public?class?SortGame?extends?Jframe?implements?ActionListener?{
private?JPanel?mainPanel?=?new?JPanel();
private?JButton?button[]?=?new?JButton[16];
private?int?butNum[]?=?new?int[16];
private?Vector?optionalNum?=?new?Vector();
public?SortGame(){
super(“16方格排序游戲“);
mainPanel.setLayout(new?GridLayout(4433));
getContentPane().add(mainPanel);
for(int?i=0;?i<16;?i++)?{
button[i]?=?new?JButton();
button[i].addActionListener(this);
mainPanel.add(button[i]);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1014??2009-04-29?17:27??SortGame\.classpath
?????文件????????384??2009-04-29?17:27??SortGame\.project
?????文件???????3768??2009-04-29?19:26??SortGame\bin\SortGame.class
?????目錄??????????0??2009-04-29?17:29??SortGame\bin
?????文件???????3147??2009-04-29?19:26??SortGame\src\SortGame.java
?????目錄??????????0??2009-04-29?17:28??SortGame\src
?????目錄??????????0??2009-04-29?17:27??SortGame
?????文件???????2592??2009-04-29?19:35??sortgame.jar
-----------?---------??----------?-----??----
????????????????10905????????????????????8
- 上一篇:Java 數字轉換成中文讀法
- 下一篇:簡易飛行棋
評論
共有 條評論