灯泡问题

假设需要制造一个带有四个按钮和两个灯泡的盒子并具有以下功能:

    ⑴ 有四个按钮输入,分别称为B1,B2,B3和B4;

    ⑵ 有两个灯泡作为输出,分别称为L1和L2;

    ⑶ B1是打开电源的按钮;

    ⑷ B4是关闭电源的按钮;

    ⑸ B2和B3 是操作按钮;

    ⑹ 在B1被按下后及B4被按下前,系统应称为电源打开状态;

    ⑺ 在B4被按下后及B1被按下前,系统应称为电源关闭状态;

    ⑻ 在电源关闭状态下,B2和B3按钮不起作用;

    ⑼ 在电源关闭状态下,灯应不亮;

    ⑽ 从最近一次电源打开状态算起,如果B2被按下的次数比B3被按下的次数多,L1亮,否则L2亮。

    ⑾ 任何时候都不能有一个以上的灯泡亮;

    ⑿ 如果其中的一个灯泡出现故障,另一个灯泡应以2秒钟的间隔闪烁,而不管B2和B3的操作过程。当B4按下时,闪烁停止;当B1被按下时,闪烁重新开始。当故障被排除后闪烁停止,系统恢复正常状态。

 

 

 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Bulb extends JFrame {
    private final int INTER_SECOND = 500; // 间隔时间
    private BulbButton b1 = new BulbButton("B1");
    private BulbButton b2 = new BulbButton("B2");
    private BulbButton b3 = new BulbButton("B3");
    private BulbButton b4 = new BulbButton("B4");
    private BulbLight l1 = new BulbLight(1);
    private BulbLight l2 = new BulbLight(2);
    private JButton bb1 = new BuldStateButton(l1);
    private JButton bb2 = new BuldStateButton(l2);
    private JLabel state = new JLabel("关闭");
    private CheckLightThread thread;

    public Bulb() {
        this.setTitle("灯泡模拟");
        this.setSize(300, 200);
        this.setLayout(new GridLayout(4, 1));
        this.add(new JPanel() {
            {
                this.setLayout(new FlowLayout(FlowLayout.CENTER));
                this.add(new JLabel("电源状态:"));
                this.add(state);
            }
        });
        this.add(new JPanel() {
            {
                this.setLayout(new FlowLayout(FlowLayout.CENTER));
                this.add(l1);
                this.add(l2);
            }
        });
        this.add(new JPanel() {
            {
                this.setLayout(new FlowLayout(FlowLayout.CENTER));
                this.add(b1);
                this.add(b2);
                this.add(b3);
                this.add(b4);
            }
        });
        this.add(new JPanel() {
            {
                this.setLayout(new FlowLayout(FlowLayout.CENTER));
                this.add(bb1);
                this.add(bb2);
            }
        });
        setButtons();
        this.setVisible(true);
    }

    private void setButtons() {
        b2.setEnabled(false);
        b3.setEnabled(false);
        b4.setSelected(true);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b1.changeState();
                if (b1.isSelected()) {
                    b4.setSelected(false);
                    b2.setEnabled(true);
                    b3.setEnabled(true);
                } else {
                    b2.setSelected(false);
                    b3.setSelected(false);
                    b4.setSelected(true);
                    b2.setEnabled(false);
                    b3.setEnabled(false);
                }
                changeSystemState();
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!b1.isSelected() && b4.isSelected())
                    return; // 在电源关闭状态下,B2和B3按钮不起作用
                b2.changeState();
                if (b2.getCount() > b3.getCount()) { // 如果B2被按下的次数比B3被按下的次数多,L1亮
                    l1.change(true);
                    l2.change(false);
                }
            }
        });
        b3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!b1.isSelected() && b4.isSelected())
                    return; // 在电源关闭状态下,B2和B3按钮不起作用
                b3.changeState();
                if (b3.getCount() >= b2.getCount()) { // 如果B2被按下的次数比B3被按下的次数少,L2亮
                    l1.change(false);
                    l2.change(true);
                }
            }
        });
        b4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                b4.changeState();
                if (b4.isSelected()) {
                    b1.setSelected(false);
                    b2.setSelected(false);
                    b3.setSelected(false);
                    b2.setEnabled(false);
                    b3.setEnabled(false);
                } else {
                    b1.setSelected(true);
                    b2.setEnabled(true);
                    b3.setEnabled(true);
                }
                changeSystemState();
            }
        });
    }

    private void changeSystemState() {
        if (!b1.isSelected() && b4.isSelected()) { // 在电源关闭状态下,灯应不亮
            state.setText("关闭");
            l1.change(false);
            l2.change(false);
            thread.pause();
        }
        if (b1.isSelected() && !b4.isSelected()) { // 从最近一次电源打开状态算起
            state.setText("开启");
            b2.clearCount();
            b3.clearCount();
            thread = new CheckLightThread();
            thread.start();
        }
    }

    public static void main(String[] args) {
        new Bulb();
    }

    private class BulbButton extends JToggleButton {
        private int count = 0;

        public BulbButton(String text) {
            super(text);
        }

        public void changeState() {
            if (isSelected()) {
                ++count;
            }
        }

        public void clearCount() {
            count = 0;
        }

        public int getCount() {
            return count;
        }
    }

    private class BulbLight extends JPanel {
        private int id;
        private boolean bad;
        private boolean lightState;
        private JLabel label;
        private JLabel state = new JLabel("【暗】");
        private JLabel state2 = new JLabel("-正常");

        public BulbLight(int id) {
            this.id = id;
            label = new JLabel("灯泡" + id);
            this.add(label);
            this.add(state);
            this.add(state2);
        }

        public int getId() {
            return id;
        }

        public void light() {
            this.state.setText("【"
                    + ("【暗】".equals(this.state.getText()) ? "亮" : "暗") + "】");
        }

        public void change(boolean light) {
            if (bad)
                return;
            lightState = light;
            this.state.setText("【" + (lightState ? "亮" : "暗") + "】");
        }

        public boolean isBad() {
            return bad;
        }

        public void setState(boolean bad) {
            this.bad = bad;
            state2.setText((bad ? "-故障" : "-正常"));
            if (bad) {
                this.state.setText("【暗】");
            } else {
                this.state.setText("【" + (lightState ? "亮" : "暗") + "】");
            }
        }
    }

    private class BuldStateButton extends JButton implements ActionListener {
        private BulbLight light;

        public BuldStateButton(BulbLight light) {
            super("破坏灯泡" + light.getId());
            this.light = light;
            this.addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            if (("破坏灯泡" + light.getId()).equals(this.getActionCommand())) {
                this.setText("修复灯泡" + light.getId());
                light.setState(true);
            } else {
                this.setText("破坏灯泡" + light.getId());
                light.setState(false);
            }
        }
    }

    private class CheckLightThread extends Thread {
        private boolean runFlag = true;

        public void pause() {
            this.runFlag = false;
        }

        public void run() {
            while (runFlag) {
                try {
                    if (l1.isBad() && !l2.isBad()) {
                        l2.light();
                    }
                    if (!l1.isBad() && l2.isBad()) {
                        l1.light();
                    }
                    sleep(INTER_SECOND);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }
}
posted @ 2016-11-17 19:38  LLSS皮  阅读(191)  评论(0编辑  收藏  举报