Problem polega na tym, ze za kazdym razem po nacisnieciu JButton, wywolywana jest metoda, ktora zwraca karte, nie potrafie zrobic tak aby w polu JTextField wyswietlane byly numery z poprzednich losowan. Za kazdym razem jest nowe losowanie i w efekcie w polu tHand
zawsze wyswietlana jest tylko jedna liczba. Chce zrobic tak, aby wylosowane liczby byly przechowywane po nacisnieciu JButton'a, a nastepnie wyswietlane wszystkie losowania(zaleznie od ilosci nacisniec Buttona).
public class ramkaGry extends JFrame implements ActionListener{
private JLabel jPowitanie, jCardInfo;
private JButton bLosuj;
private JTextField tHand;
public ramkaGry(){
setSize(600,600);
setTitle("Czarny Jacek");
setLayout(null);
jPowitanie = new JLabel("Witaj w Czarnym Jacku v1.0");
jPowitanie.setBounds(150, 50, 300, 30);
jPowitanie.setFont(new Font("",Font.BOLD,20));
add(jPowitanie);
bLosuj = new JButton("Losuj");
bLosuj.setBounds(225, 500, 100, 30);
add(bLosuj);
bLosuj.addActionListener(this);
tHand = new JTextField();
tHand.setBounds(200, 250, 80, 30);
add(tHand);
jCardInfo = new JLabel("Posiadasz nastepujace karty");
jCardInfo.setBounds(20, 250, 300, 30);
add(jCardInfo);
}
public static void main(String[] args) {
ramkaGry entryWindow = new ramkaGry();
entryWindow.frameSettings();
}
@Override
public void actionPerformed(ActionEvent e) {
Object zrodlo = e.getSource();
BLACKJACKa gracz = new BLACKJACKa();
gracz.setTaliaPole();
int schowek=0;
if (zrodlo==bLosuj)
schowek = gracz.losuje();
ArrayList<Integer> posiadaneKarty = gracz.stanKart(schowek);
String wynik = String.valueOf(posiadaneKarty);
tHand.setText(wynik);
}
public void frameSettings(){
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
}
ponizej klasa BLACKJACKa
public class BLACKJACKa {
private ArrayList<Integer> taliaPole;
private int counter = 52;
private ArrayList<Integer> sumaKart = new ArrayList<Integer>();
public void setTaliaPole() {
ArrayList<Integer> talia = new ArrayList<Integer>();
int n = 2;
int k = 48;
int size = 52;
for(int i=0;i<size;i++)
{
talia.add(n);
if(talia.size()%4==0)
n++;
if (n>10)
n=10;
}
for(int i=0;i<4;i++)
{
talia.set(k, 11);
k++;
}
taliaPole = talia;
}
public ArrayList<Integer> getTaliaPole(){
return taliaPole;
}
public int losuje(){
Random rand = new Random();
ArrayList<Integer> Karta = new ArrayList<Integer>();
int randum = rand.nextInt(counter);
Karta.add(taliaPole.get(randum));
taliaPole.remove(randum);
counter--;
//
// int sum=0;
// for(int i:reka){
// sum = sum+i;
// if(sum>21)
// return "Przegrales, wylosowales" + reka + " Twoja reka wynosi "+ sum;
// }
int liczba = Karta.get(0);
return liczba;
}
public ArrayList<Integer> stanKart(int l){
sumaKart.add(l);
return sumaKart;
}