-
January 14th, 2018, 10:00 AM
#1
Combination Lock for begginers
anyone can help improve my code. I need to simulate a combination lock on the door. He must notify that the door is open when entering the code, and also close automatically after 20 seconds. Аlso need password reset function. The password is only changed when the door is open.
Code:
/*
public class ComboNumber extends JFrame implements ActionListener{
int one, two, three;
String inData1, inData2, inData3;
JButton[] button;
private String secret = "5689";
private String guess = "";
public ComboNumber()
{
getContentPane().setLayout(new FlowLayout());
Container c = getContentPane();
button = new JButton[10];
for(int i = 0; i < button.length; ++i)
{
button[i] = new JButton("" + i);
c.add(button[i]);
button[i].addActionListener(this);
}
setTitle("Comboination Lock");
JPanel panel = new JPanel();
}
public void actionPerformed(ActionEvent evt) {
Object o = evt.getSource();
if (o instanceof JButton) {
JButton btn = (JButton) o;
guess += btn.getText();
if (guess.equals(secret)) {
JOptionPane.showMessageDialog(this, "Welcome Overloard Master");
dispose();
} else if (guess.length() >= 4) {
JOptionPane.showMessageDialog(this, "WRONG", "Wrong", JOptionPane.ERROR_MESSAGE);
guess = "";
}
}
}
public static void main (String[] args)
{
ComboNumber frm = new ComboNumber();
WindowQuitter wQuit = new WindowQuitter();
frm.addWindowListener(wQuit);
frm.setSize(500, 500);
frm.setVisible(true);
}
}
class WindowQuitter extends WindowAdapter{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
-
January 14th, 2018, 11:05 AM
#2
Re: Combination Lock for begginers
Does the posted code do what you want it to do?
If not, please explain what is wrong with what it does and explain how you want it to act differently.
Also posted at: https://coderanch.com/t/689561/java/...giners#3236253
Norm
-
January 14th, 2018, 11:37 AM
#3
Re: Combination Lock for begginers
unfortunately not, I want to add a library import javax.swing.Timer; which will ask for a password for example in 20 seconds. And password reset function
-
January 14th, 2018, 11:48 AM
#4
Re: Combination Lock for begginers
Ok, what have you tried?
Be sure to wrap any posted code in code tags.
Norm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|