- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class LightGameAllFeel extends JFrame {
- private JButton[] light;
- private JLabel on, off, onInfo, offInfo;
- private JButton Restart;
- private JPanel user, lightContainer;
- private boolean[] flag;
- private JMenuBar bar;
- private JMenu mFile;
- private JMenuItem mnuLightGame;
- private UIManager.LookAndFeelInfo[] looks = UIManager
- .getInstalledLookAndFeels();;
- private Container c = getContentPane();
- private int rownum = 5, randomNum;
- private final Color[] colors = { Color.black, Color.blue, Color.pink,
- Color.red, Color.green, Color.lightGray, Color.magenta, Color.orange,
- Color.cyan, Color.gray, Color.white, Color.yellow };
- private final int colorNum = 11;
- private Handler ActionHandler = new Handler();
- private RestartHandler restartHandler = new RestartHandler();
- //构造函数
- public LightGameAllFeel() {
- super("点灯游戏");
- SwingUtilities.updateComponentTreeUI(this);
- randomNum = getRandom();
- lightContainer = new JPanel(new GridLayout(rownum, rownum));//制作一个面板,把25盏灯放在里面
- user = new JPanel(new GridLayout(3, 2)); //制作另外一个面板,把用户信息和控制按钮放在上面
- light = new JButton[rownum * rownum];
- for (int i = 0; i < rownum * rownum; i++) { //实例化25盏灯
- light[i] = new JButton();
- light[i].setBackground(colors[randomNum]);
- lightContainer.add(light[i]);
- light[i].addActionListener(ActionHandler);
- }
- flag = new boolean[rownum * rownum]; //flag数组存放灯的亮灭
- for (int i = 0; i < rownum * rownum; i++) {
- flag[i] = false;
- }
- on = new JLabel("Lights on");
- off = new JLabel("Lights off");
- onInfo = new JLabel("0");
- offInfo = new JLabel(String.valueOf(rownum * rownum));
- Restart = new JButton("Restart");
- user.add(on);
- user.add(onInfo);
- user.add(off);
- user.add(offInfo);
- user.add(Restart);
- //给程序加上菜单
- JMenuBar bar = new JMenuBar();
- setJMenuBar(bar);
- //"File"菜单
- JMenu mFile = new JMenu("File");
- mFile.setMnemonic('F');
- JMenuItem mnuLightGame = new JMenuItem("Restart"); //设置"重新开始"菜单
- mnuLightGame.addActionListener(restartHandler);
- mFile.add(mnuLightGame);
- mFile.addSeparator();
- JMenuItem mnuExit = new JMenuItem("Exit");//设置"exit"菜单
- mnuExit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- mFile.add(mnuExit);
- //"Format"菜单
- ButtonGroup grp = new ButtonGroup();
- JMenu mnuFormat = new JMenu("Format");
- mnuFormat.setMnemonic('m');
- String names[] = { "4个", "5个", "6个" };
- JRadioButtonMenuItem numformat[];
- numformat = new JRadioButtonMenuItem[names.length + 1];
- for (int i = 0; i < names.length; i++) {
- numformat[i] = new JRadioButtonMenuItem(names[i]);
- mnuFormat.add(numformat[i]);
- numformat[i].addActionListener(new menuHandler());
- grp.add(numformat[i]);
- }
- numformat[3] = new JRadioButtonMenuItem("Custom...");
- grp.add(numformat[3]);
- mnuFormat.add(numformat[3]);
- numformat[3].addActionListener(new menuHandler());
- JMenu mnuOption = new JMenu("Option");
- JMenu lookfeelMenu = new JMenu("Look&Feel");
- mnuOption.add(lookfeelMenu);
- ButtonGroup lookfeelGroup = new ButtonGroup();
- JMenuItem menuItem;
- for (int i = 0; i < looks.length; i++) {
- menuItem = new JRadioButtonMenuItem(looks[i].getName());
- lookfeelMenu.add(menuItem);
- menuItem.addActionListener(new LookFeelAction(looks[i].getClassName()));
- lookfeelGroup.add(menuItem);
- }
- bar.add(mFile);
- bar.add(mnuFormat);
- bar.add(mnuOption);
- //把两个面板加到界面上
- c.add(lightContainer, BorderLayout.CENTER);
- c.add(user, BorderLayout.SOUTH);
- setSize(200, 320);
- show();
- //处理用户按下"Restart"键,重新开始程序
- Restart.addActionListener(restartHandler);
- }
- private void changeTheLookAndFeel(int value) {
- try {
- UIManager.setLookAndFeel(looks[value].getClassName());
- SwingUtilities.updateComponentTreeUI(this);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- private final int getRandom() {
- return (int) (Math.random() * colors.length);
- }
- public static void main(String args[]) {
- LightGameAllFeel app = new LightGameAllFeel();
- app.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- }
- private class RestartHandler implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- randomNum = getRandom();
- for (int i = 0; i < rownum * rownum; i++) {
- light[i].setBackground(colors[randomNum]);
- light[i].setEnabled(true);
- }
- for (int i = 0; i < rownum * rownum; i++)
- flag[i] = false;
- onInfo.setText("0");
- offInfo.setText(String.valueOf(rownum * rownum));
- changeTheLookAndFeel((int) (Math.random() * 3));
- }
- }
- private class Handler implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- int i, numon = 0;
- for (i = 0; i < rownum * rownum; i++)
- //找出用户点击的位置
- if (e.getSource() == light[i]) break;
- //如果点击第一列
- if (i % rownum == 0) {
- for (int j = 0; j < rownum * rownum; j++)
- if (j - i == 1 || Math.abs(i - j) == rownum || i == j) flag[j] = !flag[j];
- } else if (i % rownum == (rownum - 1)) { //如果点击最后一列
- for (int j = 0; j < rownum * rownum; j++)
- if (i - j == 1 || Math.abs(i - j) == rownum || i == j) flag[j] = !flag[j];
- } else { //如果点击其他的列
- for (int j = 0; j < rownum * rownum; j++)
- if (Math.abs(i - j) == 1 || Math.abs(i - j) == rownum || i == j) flag[j] = !flag[j];
- }
- //先清空所有的格子
- for (int j = 0; j < rownum * rownum; j++)
- light[j].setBackground(colors[randomNum]);
- //把标记为true的格子变相反色
- for (int j = 0; j < rownum * rownum; j++)
- if (flag[j]) {
- light[j].setBackground(colors[colorNum - randomNum]);
- numon++;
- }
- //改变提示信息
- onInfo.setText(String.valueOf(numon));
- offInfo.setText(String.valueOf(rownum * rownum - numon));
- //判断有没有赢得比赛
- for (i = 0; i < rownum * rownum; i++)
- if (!flag[i]) break;
- if (i == rownum * rownum) {
- JOptionPane.showMessageDialog(null, "你赢了!");
- for (i = 0; i < rownum * rownum; i++) {
- light[i].setEnabled(false);
- flag[i] = false;
- }
- }
- }
- }
- //处理用户选择游戏规模
- private class menuHandler implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- hide();
- try {
- if (e.getActionCommand().equals("4个")) rownum = 4;
- else if (e.getActionCommand().equals("5个")) rownum = 5;
- else if (e.getActionCommand().equals("6个")) rownum = 6;
- else {
- rownum = Integer.parseInt(JOptionPane.showInputDialog("输入想要的行数!"));
- if (rownum > 20) {
- JOptionPane.showMessageDialog(null, "数字不对,请重新输入");
- show();
- return;
- }
- }
- }
- catch (NumberFormatException ne) {
- JOptionPane.showMessageDialog(null, "数字不对,请重新输入");
- show();
- return;
- }
- c.remove(lightContainer);
- lightContainer.removeAll();
- light = new JButton[rownum * rownum];
- flag = new boolean[rownum * rownum];
- lightContainer.setLayout(new GridLayout(rownum, rownum));
- for (int i = 0; i < rownum * rownum; i++) {
- light[i] = new JButton();
- light[i].setBackground(colors[randomNum]);
- light[i].addActionListener(ActionHandler);
- lightContainer.add(light[i]);
- }
- c.add(lightContainer, 0);
- setSize(40 * rownum, 64 * rownum);
- show();
- for (int i = 0; i < rownum * rownum; i++) {
- flag[i] = false;
- }
- onInfo.setText(String.valueOf(0));
- offInfo.setText(String.valueOf(rownum * rownum));
- }
- }
- class LookFeelAction implements ActionListener {
- final String lookfeelClassName;
- public LookFeelAction(String name) {
- lookfeelClassName = name;
- }
- public void actionPerformed(ActionEvent e) {
- try {
- // 设置新的外观
- UIManager.setLookAndFeel(lookfeelClassName);
- // 告诉每个组件更新其外观
- SwingUtilities.updateComponentTreeUI(LightGameAllFeel.this);
- LightGameAllFeel.this.repaint();
- }
- catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- }
点灯小游戏
最新推荐文章于 2023-04-19 10:58:01 发布