Advertisement
boxglue

Java Swing KeyListener secret code example

May 11th, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. /* Program to show how to detect one letter at a time in a JTextField */
  5.  
  6. public class SecretCode2 extends JFrame implements KeyListener {
  7.  
  8.     Font displayFont = new Font("Arial", Font.PLAIN, 18);
  9.     JTextField display;
  10.     JLabel d2;
  11.     JPanel panel;
  12.    
  13.     String s = "GFT";
  14.     char[] c = {'*','*','*'};
  15.     char[] d = {'*','*','*'};
  16.     String ds = "~!@#$%^&*()?.,";
  17.    
  18.     public static void main(String[] args) {
  19.         new SecretCode2();
  20.     }
  21.  
  22.     SecretCode2() {
  23.         this.setSize(200,130);
  24.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.         this.setLocationRelativeTo(null);
  26.         this.setTitle("Enter Secret Code");
  27.         this.setResizable(false);
  28.  
  29.         panel = new JPanel();
  30.         panel.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
  31.         panel.setBackground(new Color(70,70,90));
  32.         display = new JTextField("***",9);     
  33.         display.setHorizontalAlignment(JTextField.CENTER);
  34.         display.setFont(displayFont);
  35.         display.addKeyListener(this);
  36.         panel.add(display);
  37.         d2 = new JLabel("   ***   ");
  38.         d2.setPreferredSize(new Dimension(126,26));
  39.         d2.setBackground(Color.WHITE);
  40.         d2.setOpaque(true);
  41.         d2.setHorizontalAlignment(JTextField.CENTER);
  42.         d2.setFont(displayFont);
  43.        
  44.         panel.add(d2);
  45.         this.add(panel);
  46.         this.setVisible(true);
  47.     }
  48.  
  49.  
  50.     @Override
  51.     public void keyTyped( KeyEvent e ) {               
  52.         char key = e.getKeyChar();
  53.         // if ((int)key == 8)   return;
  54.         if ((key >='A' && key <='Z') || (key >='a' && key <= 'z')) {}
  55.         else {
  56.             e.consume();
  57.             return;
  58.         }
  59.        
  60.         c[0]=c[1];
  61.         c[1]=c[2];
  62.         c[2]=key;
  63.        
  64.         d[0]=d[1];
  65.         d[1]=d[2];
  66.         d[2]=ds.charAt((int)(Math.random() * ds.length()));
  67.        
  68.         String t = new String(c).toUpperCase();
  69.         display.setText(t);
  70.         d2.setText(new String(d));
  71.         //d2.setText(" "+(int)key);
  72.         e.consume();    //after it updates the display, the keypressed also is displayed. Must consume it!!!
  73.  
  74.         if (t.equals(s)) {
  75.             panel.setBackground(Color.RED);
  76.             display.setText("YOU WIN");
  77.             d2.setText("$$$");
  78.         }
  79.     }
  80.     @Override
  81.     public void keyReleased( KeyEvent e ) {}
  82.     @Override
  83.     public void keyPressed( KeyEvent e ) {}
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement