大家指教一下
刚学编程个多月,写了个非常简单的连连看(估计算不上是),请大家指教一下。package shiyan;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class shiyan2 extends JFrame implements ActionListener{
private JPanel p1,p2;
private JButton jbt1,jbt2,jbt3;
JButton bt[][]=new JButton[6][6];
private JLabel jlb1;
int[] num=new int[2];
String[] obj=new String[2];
int n1=1,n;
int i,jifen=0;
boolean bool=true;
shiyan2(){
}
void Mainshow(){
p1=new JPanel();
p2=new JPanel();
jbt1=new JButton("开始");
jbt2=new JButton("关闭");
jbt3=new JButton("设置");
jlb1=new JLabel();
jlb1.setText("看看再说!");
Container cp=getContentPane();
cp.setLayout(new BorderLayout());
p2.setLayout(new GridLayout(6,6));
//shezhibutton();
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
n=(int)((Math.random()*1000)%2);
bt[i][j]=new JButton();
bt[i][j].setText(Integer.toString(n+1));
bt[i][j].addActionListener(this);
p2.add(bt[i][j]);
}
}
cp.add(p2,BorderLayout.CENTER);
cp.add(p1,BorderLayout.NORTH);
p1.add(jlb1);
p1.add(jbt1);
jbt1.addActionListener(this);
p1.add(jbt2);
jbt2.addActionListener(this);
p1.add(jbt3);
jbt3.addActionListener(this);
this.setTitle("shiyan2");
this.setSize(300,300);
this.setVisible(true);
}
/**按钮单击事件*/
public void actionPerformed(ActionEvent e){
if(e.getSource()==jbt2)
System.exit(0);
if(e.getSource()==jbt1){
/*for(int n=0;n<6;n++){
for(int m=0;m<6;m++){
bt[n][m].setEnabled(true);
}
}*/
this.dispose();
shiyan2 shiyan=new shiyan2();
shiyan.Mainshow();
}
if(e.getSource()==jbt3){
for(int n=0;n<6;n++){
for(int m=0;m<6;m++){
bt[n][m].setEnabled(false);
}
}
jifen=0;
}
if(e.getSource()==getaction(e.getActionCommand())){
System.out.println("面板值="+getaction(e.getActionCommand()).getLabel());
while(i<num.length){
System.out.println("num["+i+"]="+num[i]);
if(num[i]==0){
System.out.println("数组长度="+num.length);
num[i]=getnumber(getaction(e.getActionCommand()).getLabel());
obj[i]=e.getActionCommand();
System.out.println("num["+i+"]="+num[i]);
}
if(num[i]!=0){
System.out.println("11111");
i++;
break;
}
}
if(i==num.length){
System.out.println("zhixing");
zhixing(num[0],num[1],obj[0],obj[1]);
clear();
i=0;
}
}
}
public static void main(String[] args){
shiyan2 shiyan=new shiyan2();
shiyan.Mainshow();
}
/**获取按钮int值*/
int getnumber(String str){
int n=0;
n=Integer.parseInt(str);
return n;
}
/**确认按钮*/
private JButton getaction(String button){
int a1=0,a2=0;
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
if(bt[i][j].getActionCommand()==button){
a1=i;
a2=j;
}
}
}
return bt[a1][a2];
}
/**清空记录按钮值的数组*/
private void clear(){
for(int i=0;i<num.length;i++){
num[i]=0;
}
}
/**判断两个按钮的值是否相同,并清除相同按钮*/
private void zhixing(int n,int m,String str1,String str2){
if(n==m&&str1!=str2){
for(int n1=0;n1<6;n1++){
for(int m1=0;m1<6;m1++){
if(bt[n1][m1].getActionCommand()==str1){
bt[n1][m1].setEnabled(false);
}
else if(bt[n1][m1].getActionCommand()==str2){
bt[n1][m1].setEnabled(false);
}
}
}
jifen=jifen+100; //记分
jlb1.setText(Integer.toString(jifen));
}
else if(n!=m){
System.out.println("错误!");
}
}
/**初始化按钮*/
void shezhibutton(){
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
n=(int)((Math.random()*1000)%2);
bt[i][j]=new JButton();
bt[i][j].setText(Integer.toString(n+1));
bt[i][j].addActionListener(this);
p2.add(bt[i][j]);
}
}
}
}