Liny_@NotePad

沉迷ACG中

灯泡异常问题模拟^^

YOYO posted @ 2009年10月20日 22:08 in 【Java SE】 with tags 模拟 灯泡 , 1934 阅读

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

  1. 有四个按钮输入,分别称为B1,B2,B3和B4;
  2. 有两个灯泡作为输出,分别称为L1和L2;
  3. B1是打开电源的按钮;
  4. B4是关闭电源的按钮;
  5. B2和B3 是操作按钮;
  6. 在B1被按下后及B4被按下前,系统应称为电源打开状态;
  7. 在B4被按下后及B1被按下前,系统应称为电源关闭状态;
  8. 在电源关闭状态下,B2和B3按钮不起作用;
  9. 在电源关闭状态下,灯应不亮;
  10. 从最近一次电源打开状态算起,如果B2被按下的次数比B3被按下的次数多,L1亮,否则L2亮。
  11. 任何时候都不能有一个以上的灯泡亮;
  12. 如果其中的一个灯泡出现故障,另一个灯泡应以2秒钟的间隔闪烁,而不管B2和B3的操作过程。当B4按下时,闪烁停止;当B1被按下时,闪烁重新开始。当故障被排除后闪烁停止,系统恢复正常状态。

在问问上看到的问题 = = 觉得好玩就模拟了,代码比较乱 > <

另外有些修改了题目的地方:

  • 设置成B1按下时B4弹起,B4按下时B1弹起,不然有的时候电源是开是关不好判定。。如果B1/B4是一个按钮就好了 = =。
  • 为方便观察,间隔时间设为0.5s。
  • 另外加了两个按钮用来控制灯泡1、2是否故障。
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. class Bulb extends JFrame
  6. {
  7.  
  8.         private final int INTER_SECOND = 500;   // 间隔时间
  9.  
  10.         private BulbButton b1 = new BulbButton("B1");
  11.         private BulbButton b2 = new BulbButton("B2");
  12.         private BulbButton b3 = new BulbButton("B3");
  13.         private BulbButton b4 = new BulbButton("B4");
  14.  
  15.         private BulbLight l1 = new BulbLight(1);
  16.         private BulbLight l2 = new BulbLight(2);
  17.  
  18.         private JButton bb1 = new BuldStateButton(l1);
  19.         private JButton bb2 = new BuldStateButton(l2);
  20.  
  21.         private JLabel state = new JLabel("关闭");
  22.  
  23.         private CheckLightThread thread;
  24.  
  25.         public Bulb() {
  26.                 this.setTitle("灯泡模拟");
  27.                 this.setSize(300, 200);
  28.                 this.setLayout(new GridLayout(4, 1));
  29.  
  30.                 this.add(new JPanel() {
  31.                         {
  32.                                 this.setLayout(new FlowLayout(FlowLayout.CENTER));
  33.                                 this.add(new JLabel("电源状态:"));
  34.                                 this.add(state);
  35.                         }
  36.                 });
  37.  
  38.                 this.add(new JPanel() {
  39.                         {
  40.                                 this.setLayout(new FlowLayout(FlowLayout.CENTER));
  41.                                 this.add(l1);
  42.                                 this.add(l2);
  43.                         }
  44.                 });
  45.  
  46.                 this.add(new JPanel() {
  47.                         {
  48.                                 this.setLayout(new FlowLayout(FlowLayout.CENTER));
  49.                                 this.add(b1);
  50.                                 this.add(b2);
  51.                                 this.add(b3);
  52.                                 this.add(b4);
  53.                         }
  54.                 });
  55.  
  56.                 this.add(new JPanel() {
  57.                         {
  58.                                 this.setLayout(new FlowLayout(FlowLayout.CENTER));
  59.                                 this.add(bb1);
  60.                                 this.add(bb2);
  61.                         }
  62.                 });
  63.  
  64.                 setButtons();
  65.  
  66.                 this.setVisible(true);     
  67.         }
  68.  
  69.         private void setButtons() {
  70.                 b2.setEnabled(false);
  71.                 b3.setEnabled(false);
  72.                 b4.setSelected(true);
  73.  
  74.                 b1.addActionListener(new ActionListener() {
  75.                         public void actionPerformed(ActionEvent e) {
  76.                                 b1.changeState();
  77.                                 if(b1.isSelected()) {
  78.                                         b4.setSelected(false);
  79.                                         b2.setEnabled(true);
  80.                                         b3.setEnabled(true);
  81.                                 } else {
  82.                                         b2.setSelected(false);
  83.                                         b3.setSelected(false);
  84.                                         b4.setSelected(true);
  85.                                         b2.setEnabled(false);
  86.                                         b3.setEnabled(false);
  87.                                 }
  88.                                 changeSystemState();
  89.                         }
  90.                 });
  91.  
  92.                 b2.addActionListener(new ActionListener() {
  93.                         public void actionPerformed(ActionEvent e) {
  94.                                 if(!b1.isSelected() && b4.isSelected()) return//       在电源关闭状态下,B2和B3按钮不起作用
  95.                                 b2.changeState();
  96.                                 if(b2.getCount() > b3.getCount()) {          //   如果B2被按下的次数比B3被按下的次数多,L1亮
  97.                                         l1.change(true);
  98.                                         l2.change(false);
  99.                                 }
  100.                         }
  101.                 });
  102.  
  103.                 b3.addActionListener(new ActionListener() {
  104.                         public void actionPerformed(ActionEvent e) {
  105.                                 if(!b1.isSelected() && b4.isSelected()) return//       在电源关闭状态下,B2和B3按钮不起作用
  106.                                 b3.changeState();
  107.                                 if(b3.getCount() >= b2.getCount()) {    //  如果B2被按下的次数比B3被按下的次数少,L2亮
  108.                                         l1.change(false);
  109.                                         l2.change(true);
  110.                                 }
  111.                         }
  112.                 });
  113.  
  114.                 b4.addActionListener(new ActionListener() {
  115.                         public void actionPerformed(ActionEvent e) {
  116.                                 b4.changeState();
  117.                                 if(b4.isSelected()) {
  118.                                         b1.setSelected(false);
  119.                                         b2.setSelected(false);
  120.                                         b3.setSelected(false);
  121.                                         b2.setEnabled(false);
  122.                                         b3.setEnabled(false);
  123.                                 } else {
  124.                                         b1.setSelected(true);
  125.                                         b2.setEnabled(true);
  126.                                         b3.setEnabled(true);
  127.                                 }
  128.                                 changeSystemState();
  129.                         }
  130.                 });
  131.         }
  132.  
  133.         private void changeSystemState() {
  134.                 if(!b1.isSelected() && b4.isSelected()) {       //     在电源关闭状态下,灯应不亮
  135.                         state.setText("关闭");
  136.                         l1.change(false);
  137.                         l2.change(false);
  138.                         thread.pause();
  139.                 }
  140.                 if(b1.isSelected() && !b4.isSelected()) {       //     从最近一次电源打开状态算起
  141.                         state.setText("开启");
  142.                         b2.clearCount();
  143.                         b3.clearCount();
  144.                         thread = new CheckLightThread();
  145.                         thread.start();
  146.                 }
  147.         }
  148.  
  149.         public static void main(String[] args)
  150.         {
  151.                 new Bulb();
  152.         }
  153.  
  154.         private class BulbButton extends JToggleButton
  155.         {
  156.                 private int count = 0;
  157.  
  158.                 public BulbButton(String text) {
  159.                         super(text);
  160.                 }
  161.  
  162.                 public void changeState() {
  163.                         if(isSelected()) {
  164.                                 ++count;
  165.                         }
  166.                 }
  167.  
  168.                 public void clearCount() {
  169.                         count = 0;
  170.                 }
  171.  
  172.                 public int getCount() {
  173.                         return count;
  174.                 }
  175.  
  176.         }
  177.  
  178.         private class BulbLight extends JPanel
  179.         {
  180.  
  181.                 private int id;
  182.  
  183.                 private boolean bad;
  184.  
  185.                 private boolean lightState;
  186.  
  187.                 private JLabel label;
  188.  
  189.                 private JLabel state = new JLabel("【暗】");
  190.  
  191.                 private JLabel state2 = new JLabel("-正常");
  192.  
  193.                 public BulbLight(int id) {
  194.                         this.id = id;
  195.                         label = new JLabel("灯泡" + id);
  196.                         this.add(label);
  197.                         this.add(state);
  198.                         this.add(state2);
  199.                 }
  200.  
  201.                 public int getId() {
  202.                         return id;
  203.                 }
  204.  
  205.                 public void light() {
  206.                         this.state.setText("【" + ("【暗】".equals(this.state.getText())?"亮":"暗") + "】");
  207.                 }
  208.  
  209.                 public void change(boolean light) {
  210.                         if(bad)return;
  211.                         lightState = light;
  212.                         this.state.setText("【" + (lightState?"亮":"暗") + "】");
  213.                 }
  214.  
  215.                 public boolean isBad() {
  216.                         return bad;
  217.                 }
  218.  
  219.                 public void setState(boolean bad) {
  220.                         this.bad = bad;
  221.                         state2.setText((bad?"-故障":"-正常"));
  222.                         if(bad) {
  223.                                 this.state.setText("【暗】");
  224.                         } else {
  225.                                 this.state.setText("【" + (lightState?"亮":"暗") + "】");
  226.                         }
  227.                 }
  228.  
  229.         }
  230.  
  231.         private class BuldStateButton extends JButton implements ActionListener
  232.         {
  233.                 private BulbLight light;
  234.  
  235.                 public BuldStateButton(BulbLight light) {
  236.                         super("破坏灯泡" + light.getId());
  237.                         this.light = light;
  238.                         this.addActionListener(this);
  239.                 }
  240.  
  241.                 public void actionPerformed(ActionEvent e) {
  242.                         if(("破坏灯泡" + light.getId()).equals(this.getActionCommand())) {
  243.                                 this.setText("修复灯泡" + light.getId());
  244.                                 light.setState(true);
  245.                         }
  246.                         else {
  247.                                 this.setText("破坏灯泡" + light.getId());
  248.                                 light.setState(false);
  249.                         }
  250.                 }
  251.         }
  252.  
  253.         private class CheckLightThread extends Thread
  254.         {
  255.  
  256.                 private boolean runFlag = true;
  257.  
  258.                 public void pause() {
  259.                         this.runFlag = false;
  260.                 }
  261.  
  262.                 public void run() {
  263.                         while(runFlag) {
  264.                                 try
  265.                                 {
  266.                                         if (l1.isBad() && !l2.isBad())
  267.                                         {
  268.                                                 l2.light();
  269.                                         }
  270.                                         if (!l1.isBad() && l2.isBad())
  271.                                         {
  272.                                                 l1.light();
  273.                                         }
  274.                                         sleep(INTER_SECOND);
  275.                                 } catch (Exception e) {
  276.                                         System.out.println(e.getMessage());
  277.                                 }
  278.                         }
  279.                 }
  280.         }
  281. }

运行截图:


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter