import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;//=============================WindowsFrame.java========================public class WindowsFrame extends JFrame implements ActionListener{ //构造函数 public WindowsFrame(String str) { this.setTitle(str); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } //窗口面板 public JPanel win_panel = new JPanel(); win_panel.setLayout(new GridLayout(5,5)); //控制面板 public JPanel control_panel = new JPanel(); //主面板 public JPanel main_panel = new JPanel(); private JLabel mess_label = new JLabel(); private JButton restart_button = new JButton(); private JButton[] win_button = new JButton[25]; //窗口默认颜色 Color default_color; for(int i=0;i<25;i++ ) { win_button[i].setActionCommand(String.valueOf(i)); win_button[i].addActionListener(this); win_panel.add(win_button[i]); } default_color= win_button[0].getBackground(); win_panel.setPreferredSize(new Dimension(300,300)); control_panel.add(mess_label); restart_button.addActionListener(new Reset()); control_panel.add)(restart_button); main_panel.setLayout(new BorderLayout()); main_panel.add(win_panel,BorderLayout.CENTER); main_panel.add(control_panel,BorderLayout.SOUTH); public void actionPerformed(ActionEvent e) { int x= Integer.parseInt(e.getActionCommand()); select(x); isOK(); } //选中窗口后执行的动作 private void select(int x) { if (x == 0) { changeColor(win_button[x]); changeColor(win_button[x + 1]); changeColor(win_button[x + 5]); } else if (x > 0 && x < 4) { changeColor(win_button[x]); changeColor(win_button[x - 1]); changeColor(win_button[x + 1]); changeColor(win_button[x + 5]); } else if (x == 4) { changeColor(win_button[x]); changeColor(win_button[x - 1]); changeColor(win_button[x + 5]); } else if (x == 20) { changeColor(win_button[x]); changeColor(win_button[x - 5]); changeColor(win_button[x + 1]); } else if (x == 24) { changeColor(win_button[x]); changeColor(win_button[x - 5]); changeColor(win_button[x - 1]); } else if (x > 20 && x < 24) { changeColor(win_button[x]); changeColor(win_button[x - 5]); ==================接下页========问题补充:
private void isOK() { int num=0; for(int i=0;i<25;i++) { if(win_button[i].getBackground()==Color.GREEN) num++; } if(num==25) JOptionPane.showMessageDialog(null,"Congratulations!!!"); } //重新开始按钮的监听器 class Reset implements ActionListener { public void actionPerformed(ActionEvent e) { for(int i=0;i<25;i++) { win_panel.win_button[i].setBackground(default_color); } } }}//==================OpenWindowViewer.java============public class OpenWindowViewer{ public static void main(Sring[] args) { new WindowsFrame(“开窗游戏”); }}我编写的一个开窗游戏的代码,编译是有错。但我不知道错在哪里!请求各位帮助!!!
|