Write a Java Swing application for an electronic lock as shown in the figure. Th
ID: 3822344 • Letter: W
Question
Write a Java Swing application for an electronic lock as shown in the figure. The display shall show the state of either "CLOSE" or "OPEN". In the "CLOSE" state, the user types his PIN followed by the "Enter" key to unlock the system. The display shall show an asterisk (*) for each number entered. The display shall show "WRONG PIN" if the PIN is incorrect The "Clear" button clears the number entered (if any), locks the system and sets the display to "CLOSE". Assume that the following methods are available: public boolean checkPIN(String PIN);//return true for correct PIN public void unlock();//Unlock the system public void lock();//Lock the systemExplanation / Answer
CODE FOR GUI :
NOW ADD A FUNCTION THAT DISPLAY THE FUNCTIONING THE CODE FOR FUNCTIONING IS AS FOLLOWS:
import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.Timer; import java.awt.event.*; public class ElectronicLockTestDriver implements ElectronicLock { private JLabel state = new JLabel("LOCKED"); private Timer timer; /** * Create an ElectronicLockTestDriver * @param doorIdentifier a string identifying the door, e.g. "entry" or "exit" */ public ElectronicLockTestDriver(String doorIdentifier) { JFrame frame = new JFrame(doorIdentifier); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); JPanel panel = new JPanel(); frame.add(panel); panel.add(state); frame.pack(); frame.setVisible(true); timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { state.setText("LOCKED"); timer.stop(); } }); timer.stop(); } /** * Open the lock. * @param timeOpen time it should be open (s) */ public void open(int timeOpen) { state.setText("OPEN"); try { timer.setInitialDelay(timeOpen*1000); timer.restart(); } catch (IllegalArgumentException e) { System.err.println("ERROR: IllegalArgumentException in " + "open in ElectronicLockTestDriver"); } } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.