Skip to content

Instantly share code, notes, and snippets.

@ornirus
Created October 17, 2014 07:58
Show Gist options
  • Save ornirus/3d2dc1388c8aedce6f35 to your computer and use it in GitHub Desktop.
Save ornirus/3d2dc1388c8aedce6f35 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Safe extends JFrame implements ActionListener {
private JLabel greetingLabel;
private JTextField codeField;
private JButton button;
private JTextField outcomeTextField;
public static void main(String[] args) {
Safe demo = new Safe();
demo.setSize(100,150);
demo.createGUI();
demo.setVisible(true);
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
greetingLabel = new JLabel("enter code");
window.add(greetingLabel);
codeField = new JTextField(5);
window.add(codeField);
button = new JButton("unlock");
window.add(button);
button.addActionListener(this);
outcomeTextField = new JTextField(5);
window.add(outcomeTextField);
outcomeTextField.setText("locked");
}
public void actionPerformed(ActionEvent event) {
String codeString;
int code;
codeString = codeField.getText();
code = Integer.parseInt(codeString);
if (code == 123) {
outcomeTextField.setText("unlocked");
}
else
{
outcomeTextField.setText("locked");
}
codeField.setText("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment