Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. class Komorka{
  6. private int wartosc=0;
  7. void setWartosc(int w){ wartosc=w;}
  8. int getWartosc(){ return wartosc; }
  9. }
  10.  
  11. class TrzyKomorki{
  12. Komorka[] tk = new Komorka[3];
  13. TrzyKomorki(){
  14. int i;
  15. for (i=0;i<3;i++)
  16. tk[i] = new Komorka();
  17. }
  18. public String dajWartosci() {
  19. return tk[0].getWartosc() + " " + tk[1].getWartosc() + " " + tk[2].getWartosc();
  20. }
  21. void losuj(){
  22. for (int i=0;i<3;i++)
  23. tk[i].setWartosc( (int)(Math.random()*10));
  24. }
  25.  
  26. void zamienMiejscami() {
  27. Komorka temp = tk[0];
  28. tk[0] = tk[2];
  29. tk[2] = temp;
  30. }
  31. }
  32.  
  33.  
  34. class GI extends JFrame {
  35. private TrzyKomorki komorki = new TrzyKomorki();
  36. private JButton pokazKomorkiB = new JButton("Pokaz komorki");
  37. private JButton losujB = new JButton("Losuj komorki");
  38. private JButton zamienWartosciB = new JButton("Zmien wartosci");
  39. private JLabel wynikT = new JLabel();
  40.  
  41. GI() {
  42. setTitle("Trzy komorki GUI");
  43. Container container = getContentPane();
  44. container.setLayout(new GridLayout(2, 2));
  45. container.add(pokazKomorkiB);
  46. container.add(wynikT);
  47. container.add(losujB);
  48. container.add(zamienWartosciB);
  49.  
  50. // pokazKomorkiB.addActionListener(e -> {
  51. // wynikT.setText(komorki.dajWartosci());
  52. // });
  53. pokazKomorkiB.addActionListener(new PokazKomorki());
  54. losujB.addActionListener(new LosujKomorki());
  55. zamienWartosciB.addActionListener(new ZamienMiejscami());
  56. // losujB.addActionListener(e -> {
  57. // komorki.losuj();
  58. // });
  59. //
  60. // zamienWartosciB.addActionListener(e -> {
  61. // komorki.zamienMiejscami();
  62. // });
  63.  
  64. setDefaultCloseOperation(EXIT_ON_CLOSE);
  65. wynikT.setEnabled(false);
  66. setVisible(true);
  67. }
  68. class PokazKomorki implements ActionListener {
  69. public void actionPerformed(ActionEvent e) {
  70. wynikT.setText(komorki.dajWartosci());
  71. }
  72. }
  73.  
  74. class LosujKomorki implements ActionListener {
  75. public void actionPerformed(ActionEvent e) {
  76. komorki.losuj();
  77. }
  78. }
  79. class ZamienMiejscami implements ActionListener {
  80. public void actionPerformed(ActionEvent e) {
  81. komorki.zamienMiejscami();
  82. }
  83. }
  84. public static void main(String[] args) {
  85. JFrame f = new GI();
  86. f.setSize(400,400);
  87. }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement