资源描述:
《Java小游戏连连看.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclasslianliankanimplementsActionListener{JFramemainFrame;//主面板ContainerthisContainer;JPanelcenterPanel,southPanel,northPanel;//子面板JButtondiamondsButton[][]=newJButton[6][5];//游戏按钮数组JButtonexitButton,resetButto
2、n,newlyButton;//退出,重列,重新开始按钮JLabelfractionLable=newJLabel("0");//分数标签JButtonfirstButton,secondButton;//分别记录两次被选中的按钮intgrid[][]=newint[8][7];//储存游戏按钮位置staticbooleanpressInformation=false;//判断是否有按钮被选中intx0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV;//游戏按钮的位置坐标inti,j,k,n;
3、//消除方法控制publicvoidinit(){mainFrame=newJFrame("连连看");thisContainer=mainFrame.getContentPane();thisContainer.setLayout(newBorderLayout());centerPanel=newJPanel();southPanel=newJPanel();northPanel=newJPanel();thisContainer.add(centerPanel,"Center");thisContainer.add(southPanel,
4、"South");thisContainer.add(northPanel,"North");centerPanel.setLayout(newGridLayout(6,5));for(intcols=0;cols<6;cols++){for(introws=0;rows<5;rows++){diamondsButton[cols][rows]=newJButton(String.valueOf(grid[cols+1][rows+1]));diamondsButton[cols][rows].addActionListener(this);c
5、enterPanel.add(diamondsButton[cols][rows]);}}exitButton=newJButton("退出");exitButton.addActionListener(this);resetButton=newJButton("重列");resetButton.addActionListener(this);newlyButton=newJButton("再来一局");newlyButton.addActionListener(this);southPanel.add(exitButton);southPan
6、el.add(resetButton);southPanel.add(newlyButton);fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));northPanel.add(fractionLable);mainFrame.setBounds(280,100,500,450);mainFrame.setVisible(true);}publicvoidrandomBuild(){intrandoms,cols,rows;for(in
7、ttwins=1;twins<=15;twins++){randoms=(int)(Math.random()*25+1);for(intalike=1;alike<=2;alike++){cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);while(grid[cols][rows]!=0){cols=(int)(Math.random()*6+1);rows=(int)(Math.random()*5+1);}this.grid[cols][rows]=randoms;}}
8、}publicvoidfraction(){fractionLable.setText(String.valueOf(Integer.parseInt