import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Main extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; private Container contentPane; private JButton btn1; private JButton btn2; private JLabel label1; private int row=10; private int col=10; private int [][]a; private JPanel p,p1,p2,p3; private int num=0; private JButton[][] btns; private int number[][]; public Main(String title){ super(title); contentPane = getContentPane(); setSize(297,377); this.setBounds(400,100,400,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); a=new int[row+2][col+2]; number=new int[row+2][col+2]; initGUI(); } public void initGUI(){ p1=new JPanel(); btn1=new JButton("开始"); btn1.addActionListener(this); btn2=new JButton("结束"); btn2.addActionListener(this); label1=new JLabel(""+num+""); btns=new JButton[row][col]; p1.add(btn1); p1.add(btn2); p3=new JPanel(); p3.add(label1); p=new JPanel(); p.setLayout(new BorderLayout()); contentPane.add(p); p.add(p1,BorderLayout.NORTH); p.add(p3,BorderLayout.CENTER); p2=new JPanel(); p2.setLayout(new GridLayout(row,col,0,0)); for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ btns[i][j]=new JButton("〇"); btns[i][j].setMargin(new Insets(0,0,0,0)); btns[i][j].setFont(null); btns[i][j].addActionListener(this); //btns[i].addMouseListener(new NormoreMouseEvent()); p2.add(btns[i][j]); } } contentPane.add(p,BorderLayout.NORTH); contentPane.add(p2,BorderLayout.CENTER); } public void go(){ setVisible(true); } public static void main(String []args){ new Main("生命游戏").go(); } public void actionPerformed(ActionEvent e) { for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ if(e.getSource()==btns[i][j]){ btns[i][j].setText("●"); a[i][j]=1; num++; } } label1.setText("目前共有"+num+"个活细胞"); } if(e.getSource()==btn1){ for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ number[i][j]=0;//number是周围活细胞的个数 for(int z=i-1;z<i+2;z++){ for(int y=j-1;y<j+2;y++){ if(z<0||z>=row||y<0||y>=col){continue;} if(a[z][y]==1) number[i][j]++; } } //如果自身是活细胞的话,number需要-1 if(a[i][j]==1) number[i][j]--; } } //周围活细胞的个数在上下限内,则变为活细胞,否则变成死细胞 for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { switch(number[i][j]) { case 1: a[i][j]=a[i][j]; break; case 2: a[i][j]=a[i][j]; break; case 3: a[i][j]=1; break; default: a[i][j]=0; } } } for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ if(a[i][j]==1){ btns[i][j].setText("●"); } if(a[i][j]==0){ btns[i][j].setText("〇"); } } } num=0; for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ if(a[i][j]==1){ num++; } } } label1.setText("目前共有"+num+"个活细胞"); } if(e.getSource()==btn2){ for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ btns[i][j].setText("〇"); a[i][j]=0; num=0; label1.setText("目前共有"+num+"个活细胞"); } } } } }
![](https://static.zuidaima.com/resource/img/heart_ico.png)
fenghuijun LV26
2023年3月27日
微信网友_6292282163859456 LV4
2023年1月5日
xiuxiukun LV1
2020年12月1日
mealy2 LV1
2020年11月10日
谢知非zhf LV3
2020年5月25日
w571018253 LV1
2020年5月14日
可多拉 LV1
2020年4月13日
candice123 LV7
2019年12月21日
Strawberry LV1
2019年12月3日
2331337364 LV1
2019年11月26日
![](https://static.zuidaima.com/resource/img/cattle_ico.png)
interface LV22
6月30日
求学的熊猫 LV8
2023年12月26日
yangxb2 LV10
2023年7月11日
caomin LV4
2023年5月24日
fenghuijun LV26
2023年3月27日
微信网友_6292282163859456 LV4
2023年1月5日
理工铝孩 LV1
2022年12月20日
微信网友_6191697646571520 LV6
2022年11月24日
微信网友_5992582549164032 LV6
2022年6月21日
huevnn LV5
2022年6月16日