资源描述:
《Java连连看游戏》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、Java连连看游戏今天我们要制作的是连连看游戏,其代码如下:package连连看;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclasslianliankanimplementsActionListener{JFramemainFrame;ContainerthisContainer;JPanelcenterPanel,southPanel,northPanel;JButtondiamondsButton[][]=newJButt
2、on[6][5];JButtonexitButton,resetButton,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;publicvoidinit()
3、{mainFrame=newJFrame("连连看");thisContainer=mainFrame.getContentPane();thisContainer.setLayout(newBorderLayout());centerPanel=newJPanel();southPanel=newJPanel();northPanel=newJPanel();thisContainer.add(centerPanel,"Center");thisContainer.add(southPanel,"South");t
4、hisContainer.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(thi
5、s);centerPanel.add(diamondsButton[cols][rows]);}}exitButton=newJButton("退出");exitButton.addActionListener(this);resetButton=newJButton("重列");resetButton.addActionListener(this);newlyButton=newJButton("新游戏");newlyButton.addActionListener(this);southPanel.add(exi
6、tButton);southPanel.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);}publicvoidrandomBuil
7、d(){intrandoms,cols,rows;for(inttwins=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.ra
8、ndom()*5+1);}this.grid[cols][rows]=randoms;}}}publicvoidfraction(){fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));}publicvoidreload(){i