一波大作业临近~图像处理~网管。。赶了一晚上把这个写出来了~
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()); } } } } }
运行结果:
时间: 2024-10-10 05:20:42