//小心換成自己的圖片目錄。
導入javax . swing . *;
導入Java . awt . *;
導入Java . awt . event . *;
導入Java . io . file;
導入Java . io . filename filter;
公共類連連看實現ActionListener {
JFrame主機;//主面板
容器thisContainer
JPanel centerPanel,southPanel,northPanel//子面板
JButton diamonds button[][]= new JButton[6][5];//遊戲按鈕數組
JButton exitButton,resetButton,newlyButton//退出、重新排序和重新啟動按鈕
JLabel fractional able = new JLabel(" 0 ");//分數標簽
JButton firstButton,secondButton//分別記錄兩次選中的按鈕。
int grid[][]= new int[8][7];//存儲遊戲按鈕的位置
靜態布爾表達式信息=假;//判斷按鈕是否被選中。
int x0 = 0,y0 = 0,x = 0,y = 0,fristMsg = 0,secondMsg = 0,validateLV//遊戲按鈕的位置坐標
int i,j,k,n;//消除方法控制
//=====================================//
//= = = = = = = = = =通話畫面= = = = = = = = = = = = = = = = = = =/
private static Icon[]icons = new image Icon[6 * 5];
private static final String imgDir = " f:/img ";//在這裏填上圖片目錄的全稱就可以了。
靜態{
嘗試{
File dir =新文件(img dir);
file[]img files = dir . list files(new filename filter(){
public boolean accept(文件目錄,字符串名稱){
返回name.toLowerCase()。endsWith("。jpg”);
}
});
for(int I = 0;我& lt5*6;i++){
icons[I]=新圖像圖標(imgFiles[i])。getAbsolutePath());
}
} catch(Exception e){ e . printstacktrace();}
}
public void init() {
MainFrame = new JFrame(“JKJ連連看”);
mainframe . setdefaultcloseoperation(JFrame。EXIT _ ON _ CLOSE);
this container = mainframe . getcontentpane();
this container . set layout(new BorderLayout());
center panel = new JPanel();
south panel = new JPanel();
north panel = new JPanel();
thisContainer.add(centerPanel," Center ");
thisContainer.add(southPanel," South ");
thisContainer.add(northPanel," North ");
center panel . set layout(new GridLayout(6,5));
for(int cols = 0;cols & lt6;cols++) {
for(int rows = 0;行數& lt5;rows++) {
//diamonds button[cols][rows]= new JButton(String
// .value of(grid[cols+1][rows+1]);
//= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
diamonds button[cols][rows]= new JButton(icons[grid[cols+1][rows+1]-1]);
菱形按鈕[列][行]。addActionListener(this);
center panel . add(diamonds button[cols][rows]);
}
}
exit button = new JButton(" exit ");
exit button . addactionlistener(this);
reset button = new JButton(" regroup ");
reset button . addactionlistener(this);
NewlyButton = new JButton("再來壹局");
newly button . addactionlistener(this);
south panel . add(exit button);
south panel . add(reset button);
south panel . add(newly button);
fraction lable . settext(string . value of(integer . parse int(fraction lable
。getText()))));
north panel . add(fractional able);
mainFrame.setBounds(280,100,500,450);
mainframe . set visible(true);
}