Advertisement
Cieslin

PW_Swing_#1

Apr 9th, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. ////////////////////////////MAIN///////////////
  2.  
  3. package com.cieslin;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         MainWindow mainWindow = new MainWindow();
  9.     }
  10. }
  11.  
  12. ////////////////////MyPanel//////////////////////////
  13. package com.cieslin;
  14.  
  15. import javax.swing.*;
  16. import java.awt.*;
  17. import java.awt.event.ActionEvent;
  18. import java.awt.event.ActionListener;
  19. import java.util.ArrayList;
  20.  
  21. public class MyPanel extends JPanel implements ActionListener{
  22.  
  23.     private JButton clear;
  24.     private JTextField text;
  25.     private final String password = "5431";
  26.     private String userPassword = "";
  27.     private Rectangle rect;
  28.     private String passwordText;
  29.  
  30.     private ArrayList<JButton> buttons;
  31.  
  32.     MyPanel() {
  33.         this.setLayout(null);
  34.         passwordText = new String("");
  35.         rect = new Rectangle();
  36.         rect.setBounds(600, 500, 640, 600);
  37.         buttons = new ArrayList<>();
  38.         buttons.add(new JButton("0"));
  39.         buttons.get(0).setBounds(220, 500, 170, 50);
  40.         buttons.get(0).addActionListener(this);
  41.         this.add(buttons.get(0));
  42.         int x = 220;
  43.         int y = 440;
  44.         for(int i = 1; i < 10; i++) {
  45.             buttons.add(new JButton(String.valueOf(i)));
  46.             buttons.get(i).setBounds(x, y, 50, 50);
  47.             if(i % 3 == 0)
  48.             {
  49.                 x = 160;
  50.                 y -= 60;
  51.             }
  52.             x += 60;
  53.             this.add(buttons.get(i));
  54.             buttons.get(i).addActionListener(this);
  55.         }
  56.  
  57.         clear = new JButton("Czysc");
  58.         text = new JTextField("Wprowadz kod...");
  59.         text.setEditable(false);
  60.         text.setBounds(220, 10, 200, 50);
  61.         clear.setBounds(110, 70, 400, 50);
  62.         clear.addActionListener((ActionEvent e) -> {
  63.             text.setText("Wprowadz kod...");
  64.             passwordText = "";
  65.         });
  66.         this.add(text);
  67.         this.add(clear);
  68.     }
  69.  
  70.     @Override
  71.     public void actionPerformed(ActionEvent e) {
  72.         if((buttons.get(Integer.parseInt(e.getActionCommand()))).getText() != null){
  73.             userPassword += (buttons.get(Integer.parseInt(e.getActionCommand()))).getText();
  74.             passwordText += "*";
  75.             if (userPassword.equals(password)){
  76.                 text.setText("WYGRALES!");
  77.                 return;
  78.             }
  79.         }
  80.         text.setText(passwordText);
  81.     }
  82. }
  83.  
  84. ///////////////////MainWindow/////////////////////////
  85.  
  86. package com.cieslin;
  87.  
  88. import javax.swing.*;
  89. import java.awt.*;
  90.  
  91. public class MainWindow {
  92.     private JFrame mainWindow;
  93.     private Rectangle rect;
  94.  
  95.     MainWindow(){
  96.         rect = new Rectangle();
  97.         rect.setBounds(600, 500, 640, 600);
  98.         mainWindow = new JFrame();
  99.         mainWindow.setBounds(rect);
  100.         mainWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  101.         mainWindow.setLayout(null);
  102.         mainWindow.setResizable(false);
  103.         mainWindow.setContentPane(new MyPanel());
  104.         mainWindow.setVisible(true);
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement