Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////MAIN///////////////
- package com.cieslin;
- public class Main {
- public static void main(String[] args) {
- MainWindow mainWindow = new MainWindow();
- }
- }
- ////////////////////MyPanel//////////////////////////
- package com.cieslin;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- public class MyPanel extends JPanel implements ActionListener{
- private JButton clear;
- private JTextField text;
- private final String password = "5431";
- private String userPassword = "";
- private Rectangle rect;
- private String passwordText;
- private ArrayList<JButton> buttons;
- MyPanel() {
- this.setLayout(null);
- passwordText = new String("");
- rect = new Rectangle();
- rect.setBounds(600, 500, 640, 600);
- buttons = new ArrayList<>();
- buttons.add(new JButton("0"));
- buttons.get(0).setBounds(220, 500, 170, 50);
- buttons.get(0).addActionListener(this);
- this.add(buttons.get(0));
- int x = 220;
- int y = 440;
- for(int i = 1; i < 10; i++) {
- buttons.add(new JButton(String.valueOf(i)));
- buttons.get(i).setBounds(x, y, 50, 50);
- if(i % 3 == 0)
- {
- x = 160;
- y -= 60;
- }
- x += 60;
- this.add(buttons.get(i));
- buttons.get(i).addActionListener(this);
- }
- clear = new JButton("Czysc");
- text = new JTextField("Wprowadz kod...");
- text.setEditable(false);
- text.setBounds(220, 10, 200, 50);
- clear.setBounds(110, 70, 400, 50);
- clear.addActionListener((ActionEvent e) -> {
- text.setText("Wprowadz kod...");
- passwordText = "";
- });
- this.add(text);
- this.add(clear);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if((buttons.get(Integer.parseInt(e.getActionCommand()))).getText() != null){
- userPassword += (buttons.get(Integer.parseInt(e.getActionCommand()))).getText();
- passwordText += "*";
- if (userPassword.equals(password)){
- text.setText("WYGRALES!");
- return;
- }
- }
- text.setText(passwordText);
- }
- }
- ///////////////////MainWindow/////////////////////////
- package com.cieslin;
- import javax.swing.*;
- import java.awt.*;
- public class MainWindow {
- private JFrame mainWindow;
- private Rectangle rect;
- MainWindow(){
- rect = new Rectangle();
- rect.setBounds(600, 500, 640, 600);
- mainWindow = new JFrame();
- mainWindow.setBounds(rect);
- mainWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- mainWindow.setLayout(null);
- mainWindow.setResizable(false);
- mainWindow.setContentPane(new MyPanel());
- mainWindow.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement