在一个 Grid 布局的 面板上 放 5*5 个 Button. 这些Button 都是 蓝色的. 按其中任意一个 Button 其颜色变 黄色. 且周围4个Button(如有4个的话) 也变黄色. 再按任意一个Button时, 同理. 但是 一但一个Button如已经变过色 (例:前面一次变为黄色了)那么,会再变成 蓝色.如再被列入变色范围内,再变成 黄色,^^^^^^蓝色^^^黄色^^^^^^^^^^; 以此类推: 直到 5*5 都变成黄色 输出对话框:You are win!注: 周围变色 是指 东 西 南 北 4个. 斜对面的不算. 如果在角上的 那么周围只有2个 这个CSDN的兄第不会想不通吧. :)恶梦啊 恶梦.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class GridButton extends JFrame{
private Container container;
private JButton jButton1 = new JButton("jButton1");
private JButton jButton2 = new JButton("jButton2");
private JButton jButton3 = new JButton("jButton3");
private JButton jButton4 = new JButton("jButton4");
private JButton jButton5 = new JButton("jButton5");
private JButton jButton6 = new JButton("jButton6");
private JButton jButton7 = new JButton("jButton7");
private JButton jButton8 = new JButton("jButton8");
private JButton jButton9 = new JButton("jButton9");
private JButton jButton10 = new JButton("jButton10");
private JButton jButton11 = new JButton("jButton11");
private JButton jButton12 = new JButton("jButton12");
private JButton jButton13 = new JButton("jButton13");
private JButton jButton14 = new JButton("jButton14");
private JButton jButton15 = new JButton("jButton15");
private JButton jButton16 = new JButton("jButton16");
private JButton jButton17 = new JButton("jButton17");
private JButton jButton18 = new JButton("jButton18");
private JButton jButton19 = new JButton("jButton19");
private JButton jButton20 = new JButton("jButton20");
private JButton jButton21 = new JButton("jButton21");
private JButton jButton22 = new JButton("jButton22");
private JButton jButton23 = new JButton("jButton23");
private JButton jButton24 = new JButton("jButton24");
private JButton jButton25 = new JButton("jButton25");
private GridLayout gridLayout = new GridLayout();
public GridButton(){
super("GridButton");
gridLayout.setRows(5);
gridLayout.setVgap(0);
gridLayout.setColumns(5);
gridLayout.setHgap(0);
container = getContentPane();
container.setLayout(gridLayout);
container.add(jButton1,null);
container.add(jButton2,null);
container.add(jButton3,null);
container.add(jButton4,null);
container.add(jButton5,null);
container.add(jButton6,null);
container.add(jButton7,null);
container.add(jButton8,null);
container.add(jButton9,null);
container.add(jButton10,null);
container.add(jButton11,null);
container.add(jButton12,null);
container.add(jButton13,null);
container.add(jButton14,null);
container.add(jButton15,null);
container.add(jButton16,null);
container.add(jButton17,null);
container.add(jButton18,null);
container.add(jButton19,null);
container.add(jButton20,null);
container.add(jButton21,null);
container.add(jButton22,null);
container.add(jButton23,null);
container.add(jButton24,null);
container.add(jButton25,null);
jButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(jButton1.getBackground()==container.getBackground())
jButton1.setBackground(Color.BLUE);
else
jButton1.setBackground(container.getBackground());
if(jButton2.getBackground()==container.getBackground())
jButton2.setBackground(Color.BLUE);
else
jButton2.setBackground(container.getBackground());
if(jButton6.getBackground()==container.getBackground())
jButton6.setBackground(Color.BLUE);
else
jButton6.setBackground(container.getBackground());
}
});
jButton2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(jButton1.getBackground()==container.getBackground())
jButton1.setBackground(Color.BLUE);
else
jButton1.setBackground(container.getBackground());
if(jButton2.getBackground()==container.getBackground())
jButton2.setBackground(Color.BLUE);
else
jButton2.setBackground(container.getBackground());
if(jButton3.getBackground()==container.getBackground())
jButton3.setBackground(Color.BLUE);
else
jButton3.setBackground(container.getBackground()); if(jButton7.getBackground()==container.getBackground())
jButton7.setBackground(Color.BLUE);
else
jButton7.setBackground(container.getBackground());
}
});
jButton3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(jButton2.getBackground()==container.getBackground())
jButton2.setBackground(Color.BLUE);
else
jButton2.setBackground(container.getBackground());
if(jButton3.getBackground()==container.getBackground())
jButton3.setBackground(Color.BLUE);
else
jButton3.setBackground(container.getBackground());
if(jButton4.getBackground()==container.getBackground())
jButton4.setBackground(Color.BLUE);
else
jButton4.setBackground(container.getBackground()); if(jButton8.getBackground()==container.getBackground())
jButton8.setBackground(Color.BLUE);
else
jButton8.setBackground(container.getBackground());
}
});
jButton4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(jButton3.getBackground()==container.getBackground())
jButton3.setBackground(Color.BLUE);
else
jButton3.setBackground(container.getBackground());
if(jButton4.getBackground()==container.getBackground())
jButton4.setBackground(Color.BLUE);
else
jButton4.setBackground(container.getBackground());
if(jButton5.getBackground()==container.getBackground())
jButton5.setBackground(Color.BLUE);
else
jButton5.setBackground(container.getBackground()); if(jButton9.getBackground()==container.getBackground())
jButton9.setBackground(Color.BLUE);
else
jButton9.setBackground(container.getBackground());
}
});
jButton5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(jButton4.getBackground()==container.getBackground())
jButton4.setBackground(Color.BLUE);
else
jButton4.setBackground(container.getBackground());
if(jButton5.getBackground()==container.getBackground())
jButton5.setBackground(Color.BLUE);
else
jButton5.setBackground(container.getBackground());
if(jButton10.getBackground()==container.getBackground())
jButton10.setBackground(Color.BLUE);
else
jButton10.setBackground(container.getBackground());
}
});
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
setLocation(screenWidth/4,screenHeight/4);
setSize(400, 400);
setVisible(true);
}
public static void main(String args[]){
System.out.println("Start up Grid...");
GridButton gridButton = new GridButton();
gridButton.setDefaultCloseOperation(GridButton.EXIT_ON_CLOSE);
}
}
只是写了jButton1到jButton5的ActionListener,楼主可以参考写下去。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;public class T {
public static void main(String[] args) {
final int N = 5;
final JPanel btnPanel = new JPanel(new GridLayout(N, N, 2, 2));
btnPanel.setPreferredSize(new Dimension(N * 40, N * 40));
final JButton[][] buttons = new JButton[N][];
final int[] dx = {0, 1, -1, 0, 0};
final int[] dy = {0, 0, 0, 1, -1};
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
Point pos = (Point) btn.getClientProperty("Position");
for (int i = 0; i < 5; i++) {
int x = pos.x + dx[i];
int y = pos.y + dy[i];
if (x < 0 || y < 0 || x >= N || y >= N) {
continue;
}
JButton b = buttons[x][y];
b.setBackground(b.getBackground() == Color.blue ? Color.yellow : Color.blue);
}
boolean win = true;
for (int i = 0; i < N; i++) {
for (int k = 0; k < N; k++) {
if (buttons[i][k].getBackground() != Color.yellow) {
win = false;
break;
}
}
if (!win) {
break;
}
}
if (win) {
JOptionPane.showMessageDialog(btnPanel, "You win!");
System.exit(0);
}
}
}; for (int i = 0; i < N; i++) {
buttons[i] = new JButton[N];
for (int k = 0; k < N; k++) {
JButton btn = buttons[i][k] = new JButton();
btn.setBackground(Color.blue);
btn.addActionListener(al);
btn.putClientProperty("Position", new Point(i, k));
btnPanel.add(btn);
}
}
JFrame f = new JFrame();
f.getContentPane().add(btnPanel, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
关键就是变色的算法..楼上的为何不用 for来创建JButton 数组? 可省事很多.
楼上的代码可能是用工具生成的,手写代码不会这样写.
import java.awt.event.*;import javax.swing.*;public class Buttons extends JFrame{
public Buttons()
{
ButtonsPane bp = new ButtonsPane();
Container c = this.getContentPane();
c.add(bp);
this.pack();
this.setSize(600,600);
this.setLocation(300,200);
this.show();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bp.repaint();
} public static void main(String[] args)
{
new Buttons();
}
}class ButtonsPane extends JPanel implements ActionListener
{
JButton[][] jb = new JButton[5][5];
boolean[][] jbTest = new boolean[5][5];
public ButtonsPane()
{
this.setLayout(new GridLayout(5,5,5,5));
addButton(this,jb);
}
public void addButton(JPanel jp,JButton[][]jb)
{
for(int i = 0;i < 5;i++)
for(int j = 0;j < 5;j++)
{
jb[i][j] = new JButton("Press");
jb[i][j].setBackground(Color.BLUE);
jb[i][j].addActionListener(this);
jp.add(jb[i][j]);
}
}
public void actionPerformed(ActionEvent e)
{
for(int i = 0;i < 5;i++)
for(int j = 0;j < 5;j++)
{
if(e.getSource() == jb[i][j])
{
testWhere(i,j);
repaint();
}
testWin();
}
}
public void testWin()
{
int ok = 0 ;
for(int i = 0;i < 5;i++)
for(int j = 0;j < 5;j++)
{
if(jbTest[i][j] == true)ok++;
}
if(ok == 25)
JOptionPane.showMessageDialog(new JFrame("恭喜"),"you win!");
}
public void paint(Graphics g)
{
for(int i = 0;i < 5;i++)
for(int j = 0;j < 5;j++)
{
if(jbTest[i][j])
jb[i][j].setBackground(Color.YELLOW);
else
jb[i][j].setBackground(Color.BLUE);
jb[i][j].repaint();
}
}
public void testWhere(int i,int j)
{
if(i == 0 && j == 0)
{
jbTest[i][j] = !jbTest[i][j];
jbTest[i][j+1] = !jbTest[i][j+1];
jbTest[i+1][j] = !jbTest[i+1][j];
}
else if(i == 0 && j == 4)
{
jbTest[i][j] = !jbTest[i][j];
jbTest[i][j-1] = !jbTest[i][j-1];
jbTest[i+1][j] = !jbTest[i+1][j];
}
else if(i == 4 && j == 0)
{
jbTest[i][j] = !jbTest[i][j];
jbTest[i][j+1] = !jbTest[i][j+1];
jbTest[i-1][j] = !jbTest[i-1][j];
}
else if(i == 4 && j == 4)
{
jbTest[i][j] = !jbTest[i][j];
jbTest[i][j-1] = !jbTest[i][j-1];
jbTest[i-1][j] = !jbTest[i-1][j];
}
else if(i == 0)
{
jbTest[i][j] = !jbTest[i][j];
jbTest[i][j+1] = !jbTest[i][j+1];
jbTest[i+1][j] = !jbTest[i+1][j];
jbTest[i][j-1] = !jbTest[i][j-1];
}
else if(j == 0)
{
jbTest[i][j] = !jbTest[i][j];
jbTest[i][j+1] = !jbTest[i][j+1];
jbTest[i+1][j] = !jbTest[i+1][j];
jbTest[i-1][j] = !jbTest[i-1][j];
}
else if(i == 4)
{
jbTest[i][j] = !jbTest[i][j];
jbTest[i][j-1] = !jbTest[i][j-1];
jbTest[i-1][j] = !jbTest[i-1][j];
jbTest[i][j+1] = !jbTest[i][j+1];
}
else if(j == 4)
{
jbTest[i][j] = !jbTest[i][j];
jbTest[i-1][j] = !jbTest[i-1][j];
jbTest[i+1][j] = !jbTest[i+1][j];
jbTest[i][j-1] = !jbTest[i][j-1];
}
else
{
jbTest[i][j] = !jbTest[i][j];
jbTest[i][j+1] = !jbTest[i][j+1];
jbTest[i][j-1] = !jbTest[i][j-1];
jbTest[i-1][j] = !jbTest[i-1][j];
jbTest[i+1][j] = !jbTest[i+1][j];
}
}
}
编译通过.