Suppose you need to make a box with four buttons and two bulbs and have the following features:
⑴ has four button inputs, called B1,b2,b3 and B4, respectively;
⑵ has two bulbs as output, respectively called L1 and L2;
⑶B1 is the button that turns on the power supply;
⑷B4 is the button to turn off the power;
⑸B2 and B3 are operation buttons;
⑹ after B1 is pressed and B4 is pressed, the system should be called Power on state;
⑺ after B4 is pressed and B1 is pressed, the system should be called Power off state;
⑻ the B2 and B3 buttons do not work when the power is off;
⑼ in the power off state, the lamp should not light;
⑽ from the last power open state, if the B2 is pressed more times than B3 is pressed, L1 bright, otherwise L2 bright.
⑾ can not have more than one light bulb at any time;
⑿ if one of the bulbs fails, the other bulb should blink at a 2-second interval, regardless of the operation of the B2 and B3. When the B4 is pressed, the blinking stops, and when the B1 is pressed, the flashing starts Again. When the fault is excluded, the flashing stops and the system returns to its normal State.
Importjava.awt.*; Importjava.awt.event.ActionEvent;Importjava.awt.event.ActionListener;Importjavax.swing.*; public classBox {JFrame frame; JButton button1,button2,button3,button4; JLabel label1,label2; JPanel panel1,panel2; BooleanOff =false; public Static voidmain (String args[]) {box box=NewBox (); Box.go (); } voidGo () {frame=NewJFrame ("box"); Button1=NewJButton ("B1"); Button2=NewJButton ("B2"); Button3=NewJButton ("B3"); Button4=NewJButton ("B4"); Button1.settooltiptext ("turn on the power"); Button2.settooltiptext ("L1 light"); Button3.settooltiptext ("L2 light"); Button4.settooltiptext ("power off"); Label1=NewJLabel (); Label1.setborder (borderfactory.createlineborder (color.white,60)); Label2=NewJLabel (); Label2.setborder (borderfactory.createlineborder (color.white,60)); JPanel Panel1=NewJPanel (); JPanel Panel2=NewJPanel (); Panel1.add (label1,borderlayout.west); Panel1.add (label2,borderlayout.east); Panel2.add (button1); Panel2.add (button2); Panel2.add (button3); Panel2.add (button4); Listener Listener=NewListener (); Button1.addactionlistener (listener); Button2.addactionlistener (listener); Button3.addactionlistener (listener); Button4.addactionlistener (listener); Frame.add (panel1,borderlayout.north); Frame.add (panel2,borderlayout.south); Frame.setbackground (color.black); Frame.setsize (350,250); Frame.setdefaultcloseoperation (1); Frame.setvisible (true); Frame.setresizable (false); } classListenerImplementsActionListener//internal class for easy invocation of member variables in the box class On_off { public voidactionperformed (actionevent e) {String s=E.getactioncommand (); if(s.equals ("B1") ) {off=true; } if(off) {if(s.equals ("B2") {label1.setborder (borderfactory.createlineborder (color.yellow,60)); Label2.setborder (borderfactory.createlineborder (color.white,60)); Label1.setopaque (true); Label2.setopaque (true); } if(s.equals ("B3") {label1.setborder (borderfactory.createlineborder (color.white,60)); Label2.setborder (borderfactory.createlineborder (color.yellow,60)); Label1.setopaque (true); Label2.setopaque (true); } if(s.equals ("B4") {label1.setborder (borderfactory.createlineborder (color.white,60)); Label2.setborder (borderfactory.createlineborder (color.white,60)); Label1.setopaque (true); Label2.setopaque (true); Off=false; } } Else{label1.setborder (borderfactory.createlineborder (color.white,60)); Label2.setborder (borderfactory.createlineborder (color.white,60)); Label1.setopaque (true); Label2.setopaque (true); } } }}
Solving bulb problems with Java (job 1)