Avatar
ced
Člen
Avatar
ced:

Zdravím, mám tři třídy (MixIt - main, Kostka - vrací pole 16ti "hozených" stringů, Gui - vlastní přidání tlačítek) Na co za boha nemůžu přijít, je to jak pomocí tlačítka znova zamíchat hodnoty na tlačítkách. Napadlo mě zavřít celej kód v Gui do cyklu a ten provést vždy po stisknutí tlačítka, ale nejsem schopnej v OOP to zrealizovat.

public static void main(String[] args) {

   Gui okno = new Gui();
   okno.setVisible(true);
   okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   okno.setSize(400,300);

class Kostka

public class Kostka {

   String[] p1 = new String[]{"K","U","N","D","T","O"};
   String[] p2 = new String[]{"K","L","E","U","G","Y"};
   String[] p3 = new String[]{"F","E","I","E","H","Y"};
   String[] p4 = new String[]{"N","H","P","E","S","I"};
   String[] p5 = new String[]{"Qu","J","M","B","O","A"};
   String[] p6 = new String[]{"F","I","X","B","R","O"};
   String[] p7 = new String[]{"B","A","L","I","T","Y"};
   String[] p8 = new String[]{"V","E","T","I","G","N"};
   String[] p9 = new String[]{"W","U","I","L","G","R"};
   String[] p10 = new String[]{"M","A","D","P","C","E"};
   String[] p11 = new String[]{"T","A","C","I","O","A"};
   String[] p12 = new String[]{"S","E","L","C","A","R"};
   String[] p13 = new String[]{"Z","A","N","V","E","D"};
   String[] p14 = new String[]{"H","O","A","S","R","M"};
   String[] p15 = new String[]{"W","E","D","O","N","S"};
   String[] p16 = new String[]{"T","U","P","L","E","S"};
   String[][] arrays = new String[][]{p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16};
   public String pismeno;
   String[] pismena = new String[16];



    public String[] Mix()
    {

        for (int i = 0; i < arrays.length;i++)
        {
            pismena[i] = (arrays[i][new Random().nextInt(6)]);
        }


    for (int i = pismena.length - 1; i > 0; i--)
    {
      Random rnd = new Random();
      int index = rnd.nextInt(i + 1);
      // Simple swap
      String a = pismena[index];
      pismena[index] = pismena[i];
      pismena[i] = a;
    }
    return pismena;
    }

class Gui

public Gui()
   {
  Kostka kostka = new Kostka();
  kostka.Mix();

  for (int i = 0; i < 16; i++)
  {
  System.out.println(kostka.pismena[i]);
  }



   Container con = this.getContentPane();
   con.setLayout(new GridBagLayout());
   GridBagConstraints g = new GridBagConstraints();

   g.insets = new Insets(3,3,3,3);
   JButton[] buttons = new JButton[16];
   JButton mix = new JButton();
   for (int i = 0; i < 16; i++)
   {
   buttons[i] = new JButton(kostka.pismena[i]);
   buttons[i].setPreferredSize(new Dimension(50,50));

   }
   for (int i = 0; i <= 3; i++)
   {
   g.gridx = i;
   g.gridy = 0;
   con.add(buttons[i],g);
   }
    for (int i = 4; i <= 7; i++)
       {
        g.gridy = 1;
        g.gridx = i-4;

       con.add(buttons[i], g);
       }

     for (int i = 8; i <= 11; i++)
       {
        g.gridy = 2;
        g.gridx = i-8;

       con.add(buttons[i], g);
       }
     for (int i = 12; i < 16; i++)
       {
        g.gridy = 3;
        g.gridx = i-12;

       con.add(buttons[i], g);
       }

    g.gridy = 7;
    g.gridx = 2;
    g.gridheight = 1;
    g.gridwidth = 10;
    g.anchor = GridBagConstraints.SOUTH;
    mix.setText("MixIT!");
    con.add(mix,g);
   }
 
Odpovědět 11.3.2014 22:55
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 1 zpráv z 1.