Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab52;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.Vector;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public class Main {
- public static int Kod = 8194;
- public static int Liczba = 0;
- public static String txtbuf = "";
- public static String gwiazdy = "";
- public static void DodajPrzycisk(final JButton b,final JTextField txt,final Container c,String Tekst)
- {
- b.setText(Tekst);
- b.setMargin(new Insets(2,2,2,2));
- b.setSize(30, 30);
- b.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- if(txt.getText().length()<4)
- {
- String tekst = txt.getText();
- txtbuf+=b.getText();
- tekst+=b.getText();
- gwiazdy+="*";
- txt.setText(gwiazdy);
- Liczba = Integer.parseInt(txtbuf);
- if(Liczba == Kod)
- {
- JOptionPane.showMessageDialog(c,"Gratulacje! Złamałeś szyfr!");
- txt.setText("");
- txtbuf = "";
- gwiazdy = "";
- }
- }
- }
- });
- b.show();
- }
- public static void main(String[] args) {
- JFrame okno = new JFrame("Szyfr");
- okno.setBounds(400, 100, 300, 300);
- Container cp = new Container();
- okno.setContentPane(cp);
- okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- okno.show();
- cp.setLayout(new GridLayout(6,1));
- final JTextField tf = new JTextField();
- JPanel L13 = new JPanel();
- L13.setLayout(new GridLayout(1,3));
- JPanel L46 = new JPanel();
- L46.setLayout(new GridLayout(1,3));
- JPanel L79 = new JPanel();
- L79.setLayout(new GridLayout(1,3));
- tf.setHorizontalAlignment((int)JFrame.CENTER_ALIGNMENT);
- tf.enable(false);
- Font F = new Font(tf.getFont().getName(), tf.getFont().getStyle(), 40);
- tf.setFont(F);
- tf.setDisabledTextColor(Color.GREEN);
- tf.show();
- cp.add(tf);
- Vector<JButton>Prz = new Vector<JButton>();
- int Polozeniex = 0;
- int Polozeniey = 0;
- for(int i = 0;i<9;i++)
- {
- Prz.add(new JButton());
- DodajPrzycisk(Prz.elementAt(i), tf, cp,Integer.toString(i+1));
- Polozeniex++;
- if(i == 2||i==5)
- {
- Polozeniex = 0;
- Polozeniey++;
- }
- if(i<3)L13.add(Prz.elementAt(i));
- else
- if(i<6)
- L46.add(Prz.elementAt(i));
- else
- L79.add(Prz.elementAt(i));
- }
- Prz.add(new JButton());
- DodajPrzycisk(Prz.elementAt(9),tf,cp,Integer.toString(0));
- cp.add(L13);
- cp.add(L46);
- cp.add(L79);
- cp.add(Prz.elementAt(9));
- JButton Wyczysc = new JButton("Wyczysc");
- Wyczysc.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent E)
- {
- tf.setText("");
- gwiazdy = "";
- txtbuf = "";
- }
- });
- Wyczysc.show();
- cp.add(Wyczysc);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement