I-Dont-Know

ComboLock.java

Aug 4th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. @SuppressWarnings({ "rawtypes", "unchecked", "serial" })
  6. public class ComboLock extends JPanel{
  7.  
  8.     @SuppressWarnings("unused")
  9. public ComboLock() {
  10.     JPanel combo = new JPanel();
  11.     JLabel prompt = new JLabel("Please enter the combonation:");
  12.     combo.add(prompt);
  13.     prompt.setAlignmentY(Component.TOP_ALIGNMENT);
  14.     prompt.setAlignmentX(Component.CENTER_ALIGNMENT);
  15.    
  16.    
  17.    
  18.     JOptionPane JOptionPane = new JOptionPane();
  19.     int ccomp = 0;
  20.     int icombo1 = 1;
  21.     int icombo2 = 2;
  22.     int icombo3 = 3;
  23.     int ATS = 3;
  24.    
  25.     String[] lcombo = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
  26.    
  27.  
  28.     JComboBox c1 = new JComboBox(lcombo);
  29.     combo.add(c1);
  30.     c1.setAlignmentY(Component.CENTER_ALIGNMENT);
  31.     c1.setAlignmentX(Component.LEFT_ALIGNMENT);
  32.    
  33.    
  34.     JComboBox c2 = new JComboBox(lcombo);
  35.     combo.add(c2);
  36.     c2.setAlignmentY(Component.CENTER_ALIGNMENT);
  37.     c2.setAlignmentX(Component.CENTER_ALIGNMENT);
  38.    
  39.    
  40.     JComboBox c3 = new JComboBox(lcombo);
  41.     combo.add(c3);
  42.     c3.setAlignmentY(Component.CENTER_ALIGNMENT);
  43.     c3.setAlignmentX(Component.RIGHT_ALIGNMENT);
  44.  
  45.     JButton check = new JButton("Check");
  46.     combo.add(check);
  47.     check.setAlignmentY(Component.BOTTOM_ALIGNMENT);
  48.     check.setAlignmentX(Component.CENTER_ALIGNMENT);
  49.     check.addActionListener(this);
  50.    
  51.     @Override
  52.     public void actionPerformed(ActionEvent e) {
  53.    
  54.         int c1i = c1.getSelectedIndex();
  55.    
  56.         int c2i = c2.getSelectedIndex();
  57.    
  58.         int c3i = c3.getSelectedIndex();
  59.    
  60.    
  61.    
  62.    
  63.    
  64.         if (c1i == icombo1) {
  65.             ccomp = ccomp  + 1;
  66.         } else {
  67.             ccomp = ccomp  - 1;
  68.         }
  69.         if (c2i == icombo1) {
  70.             ccomp = ccomp + 1;
  71.         } else {
  72.             ccomp = ccomp  - 1;
  73.         }
  74.         if (c3i == icombo1) {
  75.             ccomp = ccomp  + 1;
  76.         } else {
  77.             ccomp = ccomp  - 1;
  78.         }
  79.    
  80.         if (ccomp == 3) {
  81.             JOptionPane.setMessage("Combo Lock Unlocked");
  82.             JOptionPane.setName("Info");
  83.             JOptionPane.setMessageType(1);
  84.             ATS = 3;
  85.         } else {
  86.             JOptionPane.setMessage("Combo Lock Not Correct, " + ATS + "attempts remaining!");
  87.             JOptionPane.setName("Error");
  88.             JOptionPane.setMessageType(3);
  89.             c1.setSelectedIndex(0);
  90.             c2.setSelectedIndex(0);
  91.             c3.setSelectedIndex(0);
  92.             ATS = ATS - 1;
  93.     }
  94.     }
  95.     }
  96.        
  97. private static void createAndShowGUI() {
  98.        
  99.        
  100.         JFrame frame = new JFrame("Combo Lock");
  101.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  102.  
  103.        
  104.         ComboLock newContentPane = new ComboLock();
  105.         newContentPane.setOpaque(true);
  106.         frame.setContentPane(newContentPane);
  107.  
  108.    
  109.         frame.pack();
  110.         frame.setSize(450, 260);
  111.        
  112.         frame.setVisible(true);
  113.        
  114. }
  115.    
  116.  
  117.     public static void main(String[] args) {
  118.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  119.             public void run() {
  120.                 createAndShowGUI();
  121.             }
  122.         });
  123.     }
  124.  
  125.  
  126.         }
Advertisement
Add Comment
Please, Sign In to add comment