Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. package lab52;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Container;
  5. import java.awt.Font;
  6. import java.awt.GridLayout;
  7. import java.awt.Insets;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.util.Vector;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JOptionPane;
  14. import javax.swing.JPanel;
  15. import javax.swing.JTextField;
  16.  
  17. public class Main {
  18. public static int Kod = 8194;
  19. public static int Liczba = 0;
  20. public static String txtbuf = "";
  21. public static String gwiazdy = "";
  22.  
  23. public static void DodajPrzycisk(final JButton b,final JTextField txt,final Container c,String Tekst)
  24. {
  25. b.setText(Tekst);
  26. b.setMargin(new Insets(2,2,2,2));
  27. b.setSize(30, 30);
  28. b.addActionListener(new ActionListener()
  29. {
  30. public void actionPerformed(ActionEvent e)
  31. {
  32. if(txt.getText().length()<4)
  33. {
  34. String tekst = txt.getText();
  35. txtbuf+=b.getText();
  36. tekst+=b.getText();
  37. gwiazdy+="*";
  38. txt.setText(gwiazdy);
  39. Liczba = Integer.parseInt(txtbuf);
  40. if(Liczba == Kod)
  41. {
  42. JOptionPane.showMessageDialog(c,"Gratulacje! Złamałeś szyfr!");
  43. txt.setText("");
  44. txtbuf = "";
  45. gwiazdy = "";
  46. }
  47. }
  48. }
  49. });
  50. b.show();
  51. }
  52.  
  53. public static void main(String[] args) {
  54. JFrame okno = new JFrame("Szyfr");
  55. okno.setBounds(400, 100, 300, 300);
  56. Container cp = new Container();
  57. okno.setContentPane(cp);
  58. okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59. okno.show();
  60. cp.setLayout(new GridLayout(6,1));
  61. final JTextField tf = new JTextField();
  62. JPanel L13 = new JPanel();
  63. L13.setLayout(new GridLayout(1,3));
  64. JPanel L46 = new JPanel();
  65. L46.setLayout(new GridLayout(1,3));
  66. JPanel L79 = new JPanel();
  67. L79.setLayout(new GridLayout(1,3));
  68. tf.setHorizontalAlignment((int)JFrame.CENTER_ALIGNMENT);
  69. tf.enable(false);
  70. Font F = new Font(tf.getFont().getName(), tf.getFont().getStyle(), 40);
  71. tf.setFont(F);
  72. tf.setDisabledTextColor(Color.GREEN);
  73. tf.show();
  74. cp.add(tf);
  75. Vector<JButton>Prz = new Vector<JButton>();
  76. int Polozeniex = 0;
  77. int Polozeniey = 0;
  78. for(int i = 0;i<9;i++)
  79. {
  80. Prz.add(new JButton());
  81. DodajPrzycisk(Prz.elementAt(i), tf, cp,Integer.toString(i+1));
  82. Polozeniex++;
  83. if(i == 2||i==5)
  84. {
  85. Polozeniex = 0;
  86. Polozeniey++;
  87. }
  88. if(i<3)L13.add(Prz.elementAt(i));
  89. else
  90. if(i<6)
  91. L46.add(Prz.elementAt(i));
  92. else
  93. L79.add(Prz.elementAt(i));
  94. }
  95. Prz.add(new JButton());
  96. DodajPrzycisk(Prz.elementAt(9),tf,cp,Integer.toString(0));
  97. cp.add(L13);
  98. cp.add(L46);
  99. cp.add(L79);
  100. cp.add(Prz.elementAt(9));
  101. JButton Wyczysc = new JButton("Wyczysc");
  102. Wyczysc.addActionListener(new ActionListener()
  103. {
  104. public void actionPerformed(ActionEvent E)
  105. {
  106. tf.setText("");
  107. gwiazdy = "";
  108. txtbuf = "";
  109. }
  110. });
  111. Wyczysc.show();
  112. cp.add(Wyczysc);
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement