本人菜鸡一只 想编一个很简单的游戏
5×5的checkbox阵
按下其中一个checkbox
这个和四周的checkbox都会改变形态
代码好像没有什么错误 但是eclipse不能运行诶
有大神能帮我看看嘛 我感觉是reverse和click那里错掉啦
但是不知道怎么改
import就省略啦
public class game{
JPanel panel;
JFrame frame;
ArrayList<JCheckBox> cb;
int[] numLabel={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
String[] nl={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
"16","17","18","19","20","21","22","23","24","25"};
public static void main(String[] args){
new game().go();
}
public void go(){
frame = new JFrame("lights out puzzle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
JPanel bg = new JPanel(layout);
bg.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
cb = new ArrayList<JCheckBox>();
Box box = new Box(BoxLayout.Y_AXIS);
JButton reset = new JButton("reset");
reset.addActionListener(new reset());
box.add(reset);
bg.add(BorderLayout.EAST,box);
frame.getContentPane().add(bg);
GridLayout g = new GridLayout(5,5);
g.setVgap(1);
g.setHgap(2);
panel = new JPanel(g);
bg.add(BorderLayout.CENTER,panel);
for(int i=0;i<25;i++){
JCheckBox c = new JCheckBox();
c.setSelected(false);
c.addActionListener(new click());
cb.add(c);
panel.add(c);
}
frame.setBounds(50,50,300,3000);
frame.pack();
frame.setVisible(true);
}
public class reset implements ActionListener {
public void actionPerformed(ActionEvent e) {
for(JCheckBox c:cb){
c.setSelected(false);
}
}
}
public class click implements ActionListener{
JCheckBox[] jc =(JCheckBox[]) cb.toArray();
public void actionPerformed(ActionEvent e) {
for(int i=0;i<25;i++){
if(jc[i].isSelected()){
reverse(jc[i-1]);
reverse(jc[i+1]);
reverse(jc[i-5]);
reverse(jc[i+5]);
}
}
}
public void reverse(JCheckBox jcb){
JCheckBox c = null;
if(c.isSelected()){
c.setSelected(false);
}
else{
c.setSelected(true);
}
}
}
}
5×5的checkbox阵
按下其中一个checkbox
这个和四周的checkbox都会改变形态
代码好像没有什么错误 但是eclipse不能运行诶
有大神能帮我看看嘛 我感觉是reverse和click那里错掉啦
但是不知道怎么改
import就省略啦
public class game{
JPanel panel;
JFrame frame;
ArrayList<JCheckBox> cb;
int[] numLabel={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
String[] nl={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
"16","17","18","19","20","21","22","23","24","25"};
public static void main(String[] args){
new game().go();
}
public void go(){
frame = new JFrame("lights out puzzle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
JPanel bg = new JPanel(layout);
bg.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
cb = new ArrayList<JCheckBox>();
Box box = new Box(BoxLayout.Y_AXIS);
JButton reset = new JButton("reset");
reset.addActionListener(new reset());
box.add(reset);
bg.add(BorderLayout.EAST,box);
frame.getContentPane().add(bg);
GridLayout g = new GridLayout(5,5);
g.setVgap(1);
g.setHgap(2);
panel = new JPanel(g);
bg.add(BorderLayout.CENTER,panel);
for(int i=0;i<25;i++){
JCheckBox c = new JCheckBox();
c.setSelected(false);
c.addActionListener(new click());
cb.add(c);
panel.add(c);
}
frame.setBounds(50,50,300,3000);
frame.pack();
frame.setVisible(true);
}
public class reset implements ActionListener {
public void actionPerformed(ActionEvent e) {
for(JCheckBox c:cb){
c.setSelected(false);
}
}
}
public class click implements ActionListener{
JCheckBox[] jc =(JCheckBox[]) cb.toArray();
public void actionPerformed(ActionEvent e) {
for(int i=0;i<25;i++){
if(jc[i].isSelected()){
reverse(jc[i-1]);
reverse(jc[i+1]);
reverse(jc[i-5]);
reverse(jc[i+5]);
}
}
}
public void reverse(JCheckBox jcb){
JCheckBox c = null;
if(c.isSelected()){
c.setSelected(false);
}
else{
c.setSelected(true);
}
}
}
}