点灯小游戏

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. public class LightGameAllFeel extends JFrame {
  5.   private JButton[] light;
  6.   private JLabel on, off, onInfo, offInfo;
  7.   private JButton Restart;
  8.   private JPanel user, lightContainer;
  9.   private boolean[] flag;
  10.   private JMenuBar bar;
  11.   private JMenu mFile;
  12.   private JMenuItem mnuLightGame;
  13.   private UIManager.LookAndFeelInfo[] looks = UIManager
  14.       .getInstalledLookAndFeels();;
  15.   private Container c = getContentPane();
  16.   private int rownum = 5, randomNum;
  17.   private final Color[] colors = { Color.black, Color.blue, Color.pink,
  18.       Color.red, Color.green, Color.lightGray, Color.magenta, Color.orange,
  19.       Color.cyan, Color.gray, Color.white, Color.yellow };
  20.   private final int colorNum = 11;
  21.   private Handler ActionHandler = new Handler();
  22.   private RestartHandler restartHandler = new RestartHandler();
  23.   //构造函数
  24.   public LightGameAllFeel() {
  25.     super("点灯游戏");
  26.     SwingUtilities.updateComponentTreeUI(this);
  27.     randomNum = getRandom();
  28.     lightContainer = new JPanel(new GridLayout(rownum, rownum));//制作一个面板,把25盏灯放在里面
  29.     user = new JPanel(new GridLayout(3, 2)); //制作另外一个面板,把用户信息和控制按钮放在上面
  30.     light = new JButton[rownum * rownum];
  31.     for (int i = 0; i < rownum * rownum; i++) { //实例化25盏灯
  32.       light[i] = new JButton();
  33.       light[i].setBackground(colors[randomNum]);
  34.       lightContainer.add(light[i]);
  35.       light[i].addActionListener(ActionHandler);
  36.     }
  37.     flag = new boolean[rownum * rownum]; //flag数组存放灯的亮灭
  38.     for (int i = 0; i < rownum * rownum; i++) {
  39.       flag[i] = false;
  40.     }
  41.     on = new JLabel("Lights on");
  42.     off = new JLabel("Lights off");
  43.     onInfo = new JLabel("0");
  44.     offInfo = new JLabel(String.valueOf(rownum * rownum));
  45.     Restart = new JButton("Restart");
  46.     user.add(on);
  47.     user.add(onInfo);
  48.     user.add(off);
  49.     user.add(offInfo);
  50.     user.add(Restart);
  51.     //给程序加上菜单
  52.     JMenuBar bar = new JMenuBar();
  53.     setJMenuBar(bar);
  54.     //"File"菜单
  55.     JMenu mFile = new JMenu("File");
  56.     mFile.setMnemonic('F');
  57.     JMenuItem mnuLightGame = new JMenuItem("Restart"); //设置"重新开始"菜单
  58.     mnuLightGame.addActionListener(restartHandler);
  59.     mFile.add(mnuLightGame);
  60.     mFile.addSeparator();
  61.     JMenuItem mnuExit = new JMenuItem("Exit");//设置"exit"菜单
  62.     mnuExit.addActionListener(new ActionListener() {
  63.       public void actionPerformed(ActionEvent e) {
  64.         System.exit(0);
  65.       }
  66.     });
  67.     mFile.add(mnuExit);
  68.     //"Format"菜单
  69.     ButtonGroup grp = new ButtonGroup();
  70.     JMenu mnuFormat = new JMenu("Format");
  71.     mnuFormat.setMnemonic('m');
  72.     String names[] = { "4个""5个""6个" };
  73.     JRadioButtonMenuItem numformat[];
  74.     numformat = new JRadioButtonMenuItem[names.length + 1];
  75.     for (int i = 0; i < names.length; i++) {
  76.       numformat[i] = new JRadioButtonMenuItem(names[i]);
  77.       mnuFormat.add(numformat[i]);
  78.       numformat[i].addActionListener(new menuHandler());
  79.       grp.add(numformat[i]);
  80.     }
  81.     numformat[3] = new JRadioButtonMenuItem("Custom...");
  82.     grp.add(numformat[3]);
  83.     mnuFormat.add(numformat[3]);
  84.     numformat[3].addActionListener(new menuHandler());
  85.     JMenu mnuOption = new JMenu("Option");
  86.     JMenu lookfeelMenu = new JMenu("Look&Feel");
  87.     mnuOption.add(lookfeelMenu);
  88.     ButtonGroup lookfeelGroup = new ButtonGroup();
  89.     JMenuItem menuItem;
  90.     for (int i = 0; i < looks.length; i++) {
  91.       menuItem = new JRadioButtonMenuItem(looks[i].getName());
  92.       lookfeelMenu.add(menuItem);
  93.       menuItem.addActionListener(new LookFeelAction(looks[i].getClassName()));
  94.       lookfeelGroup.add(menuItem);
  95.     }
  96.     bar.add(mFile);
  97.     bar.add(mnuFormat);
  98.     bar.add(mnuOption);
  99.     //把两个面板加到界面上
  100.     c.add(lightContainer, BorderLayout.CENTER);
  101.     c.add(user, BorderLayout.SOUTH);
  102.     setSize(200, 320);
  103.     show();
  104.     //处理用户按下"Restart"键,重新开始程序
  105.     Restart.addActionListener(restartHandler);
  106.   }
  107.   private void changeTheLookAndFeel(int value) {
  108.     try {
  109.       UIManager.setLookAndFeel(looks[value].getClassName());
  110.       SwingUtilities.updateComponentTreeUI(this);
  111.     }
  112.     catch (Exception e) {
  113.       e.printStackTrace();
  114.     }
  115.   }
  116.   private final int getRandom() {
  117.     return (int) (Math.random() * colors.length);
  118.   }
  119.   public static void main(String args[]) {
  120.     LightGameAllFeel app = new LightGameAllFeel();
  121.     app.addWindowListener(new WindowAdapter() {
  122.       public void windowClosing(WindowEvent e) {
  123.         System.exit(0);
  124.       }
  125.     });
  126.   }
  127.   private class RestartHandler implements ActionListener {
  128.     public void actionPerformed(ActionEvent e) {
  129.       randomNum = getRandom();
  130.       for (int i = 0; i < rownum * rownum; i++) {
  131.         light[i].setBackground(colors[randomNum]);
  132.         light[i].setEnabled(true);
  133.       }
  134.       for (int i = 0; i < rownum * rownum; i++)
  135.         flag[i] = false;
  136.       onInfo.setText("0");
  137.       offInfo.setText(String.valueOf(rownum * rownum));
  138.       changeTheLookAndFeel((int) (Math.random() * 3));
  139.     }
  140.   }
  141.   private class Handler implements ActionListener {
  142.     public void actionPerformed(ActionEvent e) {
  143.       int i, numon = 0;
  144.       for (i = 0; i < rownum * rownum; i++)
  145.         //找出用户点击的位置
  146.         if (e.getSource() == light[i]) break;
  147.       //如果点击第一列
  148.       if (i % rownum == 0) {
  149.         for (int j = 0; j < rownum * rownum; j++)
  150.           if (j - i == 1 || Math.abs(i - j) == rownum || i == j) flag[j] = !flag[j];
  151.       } else if (i % rownum == (rownum - 1)) { //如果点击最后一列
  152.         for (int j = 0; j < rownum * rownum; j++)
  153.           if (i - j == 1 || Math.abs(i - j) == rownum || i == j) flag[j] = !flag[j];
  154.       } else { //如果点击其他的列
  155.         for (int j = 0; j < rownum * rownum; j++)
  156.           if (Math.abs(i - j) == 1 || Math.abs(i - j) == rownum || i == j) flag[j] = !flag[j];
  157.       }
  158.       //先清空所有的格子
  159.       for (int j = 0; j < rownum * rownum; j++)
  160.         light[j].setBackground(colors[randomNum]);
  161.       //把标记为true的格子变相反色
  162.       for (int j = 0; j < rownum * rownum; j++)
  163.         if (flag[j]) {
  164.           light[j].setBackground(colors[colorNum - randomNum]);
  165.           numon++;
  166.         }
  167.       //改变提示信息
  168.       onInfo.setText(String.valueOf(numon));
  169.       offInfo.setText(String.valueOf(rownum * rownum - numon));
  170.       //判断有没有赢得比赛
  171.       for (i = 0; i < rownum * rownum; i++)
  172.         if (!flag[i]) break;
  173.       if (i == rownum * rownum) {
  174.         JOptionPane.showMessageDialog(null"你赢了!");
  175.         for (i = 0; i < rownum * rownum; i++) {
  176.           light[i].setEnabled(false);
  177.           flag[i] = false;
  178.         }
  179.       }
  180.     }
  181.   }
  182.   //处理用户选择游戏规模
  183.   private class menuHandler implements ActionListener {
  184.     public void actionPerformed(ActionEvent e) {
  185.       hide();
  186.       try {
  187.         if (e.getActionCommand().equals("4个")) rownum = 4;
  188.         else if (e.getActionCommand().equals("5个")) rownum = 5;
  189.         else if (e.getActionCommand().equals("6个")) rownum = 6;
  190.         else {
  191.           rownum = Integer.parseInt(JOptionPane.showInputDialog("输入想要的行数!"));
  192.           if (rownum > 20) {
  193.             JOptionPane.showMessageDialog(null"数字不对,请重新输入");
  194.             show();
  195.             return;
  196.           }
  197.         }
  198.       }
  199.       catch (NumberFormatException ne) {
  200.         JOptionPane.showMessageDialog(null"数字不对,请重新输入");
  201.         show();
  202.         return;
  203.       }
  204.       c.remove(lightContainer);
  205.       lightContainer.removeAll();
  206.       light = new JButton[rownum * rownum];
  207.       flag = new boolean[rownum * rownum];
  208.       lightContainer.setLayout(new GridLayout(rownum, rownum));
  209.       for (int i = 0; i < rownum * rownum; i++) {
  210.         light[i] = new JButton();
  211.         light[i].setBackground(colors[randomNum]);
  212.         light[i].addActionListener(ActionHandler);
  213.         lightContainer.add(light[i]);
  214.       }
  215.       c.add(lightContainer, 0);
  216.       setSize(40 * rownum, 64 * rownum);
  217.       show();
  218.       for (int i = 0; i < rownum * rownum; i++) {
  219.         flag[i] = false;
  220.       }
  221.       onInfo.setText(String.valueOf(0));
  222.       offInfo.setText(String.valueOf(rownum * rownum));
  223.     }
  224.   }
  225.   class LookFeelAction implements ActionListener {
  226.     final String lookfeelClassName;
  227.     public LookFeelAction(String name) {
  228.       lookfeelClassName = name;
  229.     }
  230.     public void actionPerformed(ActionEvent e) {
  231.       try {
  232.         // 设置新的外观
  233.         UIManager.setLookAndFeel(lookfeelClassName);
  234.         // 告诉每个组件更新其外观
  235.         SwingUtilities.updateComponentTreeUI(LightGameAllFeel.this);
  236.         LightGameAllFeel.this.repaint();
  237.       }
  238.       catch (Exception ex) {
  239.         ex.printStackTrace();
  240.       }
  241.     }
  242.   }
  243. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值