JAVA 关于国际象棋棋盘的扩展

来源:互联网 发布:淘宝服务热线 编辑:程序博客网 时间:2024/05/01 09:43

 闲来无事,打了下关于黑白棋盘的变化 代码如下:

import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.*;import javax.swing.JOptionPane;public class Test extends JFrame {public static void main(String[] args) {int que;  //定义边长que=Integer.parseInt(JOptionPane.showInputDialog("输入方阵的大小"));m.x=que;Test hh=new Test();hh.setDefaultCloseOperation(hh.EXIT_ON_CLOSE);hh.setVisible(true);hh.setLocationRelativeTo(null);hh.setTitle("black&white");hh.pack();if(que<5){hh.setSize(400,300);}else hh.setSize(600,600);}public  Test(){setLayout(new GridLayout(m.x,1));for(int i=0;i<m.x;i++){JPanel p1=new m();add(p1);m.y++;m.flag=-m.flag;}}}class m extends JPanel{ public static int x;public static  int y=0;public static int flag=1;public m(){  setLayout(new GridLayout(1,x));for(int i=0;i<x;i++){   int num=i+1;    int numy=y+1;JButton j1=new JButton(""+numy+num);add(j1);if(flag==1)  //关于黑白的变化{if(i%2==0)j1.setBackground(Color.black);else     j1.setBackground(Color.white);}else{if(i%2==0)j1.setBackground(Color.white);else     j1.setBackground(Color.black);}}    }}


0 0
原创粉丝点击