用Java实现别踩白块游戏,白块、黑块适合用哪个组建来实现
1个回答
2015-02-12
展开全部
适合用JButton来实现,实例如下:
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MiGong extends JFrame
{
private static final long serialVersionUID = 1L;
private static final int ROW = 8;
private static final int COL = 9;
int[][] map = {
{2,0,1,0,0,0,1,0},
{0,0,1,0,0,0,1,0},
{0,0,1,0,1,1,0,0},
{0,1,1,1,0,0,0,0},
{0,0,0,1,0,0,0,0},
{0,1,0,0,0,1,0,1},
{0,1,1,1,1,0,0,1},
{1,1,0,0,0,1,0,1},
{1,1,0,0,0,0,0,3}
};
public static void main ( String args[] )
{
new MiGong ();
}
public MiGong ()
{
setTitle ("迷宫");
setLayout (new GridLayout (ROW, COL, 0, 0));
for ( int h = 0; h < COL; h++ )
{
for ( int g = 0; g < ROW; g++ )
{
switch (map[h][g])
{
case 0:
JButton p = new JButton ();
p.setBackground (Color.red);
add (p);
break;
case 1:
JButton q = new JButton ();
q.setBackground (Color.blue);
add (q);
break;
case 2:
JButton m = new JButton ("入口");
m.setBackground (Color.green);
add (m);
break;
case 3:
JButton n = new JButton ("出口");
n.setBackground (Color.green);
add (n);
break;
}
}
}
pack ();
setLocationRelativeTo (null);
setDefaultCloseOperation (EXIT_ON_CLOSE);
setVisible (true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询