-
-
Save ornirus/3d2dc1388c8aedce6f35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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