Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- @SuppressWarnings({ "rawtypes", "unchecked", "serial" })
- public class ComboLock extends JPanel{
- @SuppressWarnings("unused")
- public ComboLock() {
- JPanel combo = new JPanel();
- JLabel prompt = new JLabel("Please enter the combonation:");
- combo.add(prompt);
- prompt.setAlignmentY(Component.TOP_ALIGNMENT);
- prompt.setAlignmentX(Component.CENTER_ALIGNMENT);
- JOptionPane JOptionPane = new JOptionPane();
- int ccomp = 0;
- int icombo1 = 1;
- int icombo2 = 2;
- int icombo3 = 3;
- int ATS = 3;
- String[] lcombo = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
- JComboBox c1 = new JComboBox(lcombo);
- combo.add(c1);
- c1.setAlignmentY(Component.CENTER_ALIGNMENT);
- c1.setAlignmentX(Component.LEFT_ALIGNMENT);
- JComboBox c2 = new JComboBox(lcombo);
- combo.add(c2);
- c2.setAlignmentY(Component.CENTER_ALIGNMENT);
- c2.setAlignmentX(Component.CENTER_ALIGNMENT);
- JComboBox c3 = new JComboBox(lcombo);
- combo.add(c3);
- c3.setAlignmentY(Component.CENTER_ALIGNMENT);
- c3.setAlignmentX(Component.RIGHT_ALIGNMENT);
- JButton check = new JButton("Check");
- combo.add(check);
- check.setAlignmentY(Component.BOTTOM_ALIGNMENT);
- check.setAlignmentX(Component.CENTER_ALIGNMENT);
- check.addActionListener(this);
- @Override
- public void actionPerformed(ActionEvent e) {
- int c1i = c1.getSelectedIndex();
- int c2i = c2.getSelectedIndex();
- int c3i = c3.getSelectedIndex();
- if (c1i == icombo1) {
- ccomp = ccomp + 1;
- } else {
- ccomp = ccomp - 1;
- }
- if (c2i == icombo1) {
- ccomp = ccomp + 1;
- } else {
- ccomp = ccomp - 1;
- }
- if (c3i == icombo1) {
- ccomp = ccomp + 1;
- } else {
- ccomp = ccomp - 1;
- }
- if (ccomp == 3) {
- JOptionPane.setMessage("Combo Lock Unlocked");
- JOptionPane.setName("Info");
- JOptionPane.setMessageType(1);
- ATS = 3;
- } else {
- JOptionPane.setMessage("Combo Lock Not Correct, " + ATS + "attempts remaining!");
- JOptionPane.setName("Error");
- JOptionPane.setMessageType(3);
- c1.setSelectedIndex(0);
- c2.setSelectedIndex(0);
- c3.setSelectedIndex(0);
- ATS = ATS - 1;
- }
- }
- }
- private static void createAndShowGUI() {
- JFrame frame = new JFrame("Combo Lock");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- ComboLock newContentPane = new ComboLock();
- newContentPane.setOpaque(true);
- frame.setContentPane(newContentPane);
- frame.pack();
- frame.setSize(450, 260);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment