Cześć wszystkim.
Postanowiłem po raz drugi napisać grę kółko i krzyżyk w Javie, z tą różnicą, że chciałem podzielić program na 2 klasy: klasę, w której znajduje się tylko część GUI oraz klasę, w której znajduje się część logiczna, odpowiedzialna za działanie programu. Chciałbym się Was zapytać czy danie dostępu do buttonów i etykiet poprzez settery i gettery, potem utworzenie obiektu danej klasy i wywoływanie na nim tych metod to dobra praktyka? Czy jest jakiś inny, lepszy sposób? Tak właśnie zrobiłem, ale i tak coś jest nie tak, bo po kliknięciu na etykietę obraz nie zmienia się na kółko lub krzyżyk.
Z góry dziękuję za odpowiedź :)
EDIT
Kod z klasy GUI:
package Tics;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class TicGUI {
private JFrame frame;
ImageIcon nothing = new ImageIcon("img/nic.jpg");
ImageIcon x = new ImageIcon("img/x.jpg");
ImageIcon xsmall = new ImageIcon("img/xsmall.jpg");
ImageIcon o = new ImageIcon("img/o.jpg");
ImageIcon osmall = new ImageIcon("img/osmall.jpg");
private JLabel lblA1, lblA2, lblA3, lblA4, lblA5, lblA6, lblA7, lblA8, lblA9, lblPlayer;
private JButton btnRes;
public ImageIcon getNothing() {
return nothing;
}
public void setNothing(ImageIcon nothing) {
this.nothing = nothing;
}
public ImageIcon getX() {
return x;
}
public void setX(ImageIcon x) {
this.x = x;
}
public ImageIcon getXsmall() {
return xsmall;
}
public void setXsmall(ImageIcon xsmall) {
this.xsmall = xsmall;
}
public ImageIcon getO() {
return o;
}
public void setO(ImageIcon o) {
this.o = o;
}
public ImageIcon getOsmall() {
return osmall;
}
public void setOsmall(ImageIcon osmall) {
this.osmall = osmall;
}
public JLabel getLblA1() {
return lblA1;
}
public void setLblA1(JLabel lblA1) {
this.lblA1 = lblA1;
}
public JLabel getLblA2() {
return lblA2;
}
public void setLblA2(JLabel lblA2) {
this.lblA2 = lblA2;
}
public JLabel getLblA3() {
return lblA3;
}
public void setLblA3(JLabel lblA3) {
this.lblA3 = lblA3;
}
public JLabel getLblA4() {
return lblA4;
}
public void setLblA4(JLabel lblA4) {
this.lblA4 = lblA4;
}
public JLabel getLblA5() {
return lblA5;
}
public void setLblA5(JLabel lblA5) {
this.lblA5 = lblA5;
}
public JLabel getLblA6() {
return lblA6;
}
public void setLblA6(JLabel lblA6) {
this.lblA6 = lblA6;
}
public JLabel getLblA7() {
return lblA7;
}
public void setLblA7(JLabel lblA7) {
this.lblA7 = lblA7;
}
public JLabel getLblA8() {
return lblA8;
}
public void setLblA8(JLabel lblA8) {
this.lblA8 = lblA8;
}
public JLabel getLblA9() {
return lblA9;
}
public void setLblA9(JLabel lblA9) {
this.lblA9 = lblA9;
}
public JLabel getLblPlayer() {
return lblPlayer;
}
public void setLblPlayer(JLabel lblPlayer) {
this.lblPlayer = lblPlayer;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TicGUI window = new TicGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TicGUI() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 563, 415);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
lblA1 = new JLabel("");
lblA1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Tic object = new Tic();
object.play();
}
});
lblA1.setBounds(29, 37, 100, 100);
frame.getContentPane().add(lblA1);
lblA1.setIcon(nothing);
lblA2 = new JLabel("");
lblA2.setBounds(129, 37, 100, 100);
frame.getContentPane().add(lblA2);
lblA2.setIcon(nothing);
lblA3 = new JLabel("");
lblA3.setBounds(229, 37, 100, 100);
frame.getContentPane().add(lblA3);
lblA3.setIcon(nothing);
lblA4 = new JLabel("");
lblA4.setBounds(29, 137, 100, 100);
frame.getContentPane().add(lblA4);
lblA4.setIcon(nothing);
lblA5 = new JLabel("");
lblA5.setBounds(129, 137, 100, 100);
frame.getContentPane().add(lblA5);
lblA5.setIcon(nothing);
lblA6 = new JLabel("");
lblA6.setBounds(229, 137, 100, 100);
frame.getContentPane().add(lblA6);
lblA6.setIcon(nothing);
lblA7 = new JLabel("");
lblA7.setBounds(29, 237, 100, 100);
frame.getContentPane().add(lblA7);
lblA7.setIcon(nothing);
lblA8 = new JLabel("");
lblA8.setBounds(129, 237, 100, 100);
frame.getContentPane().add(lblA8);
lblA8.setIcon(nothing);
lblA9 = new JLabel("");
lblA9.setBounds(229, 237, 100, 100);
frame.getContentPane().add(lblA9);
lblA9.setIcon(nothing);
btnRes = new JButton("Zacznij od nowa");
btnRes.setBounds(367, 62, 151, 50);
frame.getContentPane().add(btnRes);
JLabel lblSite = new JLabel("Tura gracza:");
lblSite.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblSite.setBounds(356, 205, 89, 32);
frame.getContentPane().add(lblSite);
lblPlayer = new JLabel("");
lblPlayer.setBounds(455, 207, 30, 30);
frame.getContentPane().add(lblPlayer);
lblPlayer.setIcon(osmall);
}
}
A tutaj kod z klasy z częścią logiczną. Jest tylko na razie 1 etykieta, bo chciałem sprawdzić czy to będzie działać.
package Tics;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
public class Tic {
private char p1, p2, p3, p4, p5, p6, p7, p8, p9, who;
public void play(){
TicGUI tic = new TicGUI();
if(tic.getLblA1().getIcon().equals(tic.getNothing())){
if(tic.getLblPlayer().getIcon().equals(tic.getOsmall())){
tic.getLblA1().setIcon(tic.getO());
p8='o';
who='x';
tic.getLblPlayer().setIcon(tic.getXsmall());
}
else if(tic.getLblPlayer().getIcon().equals(tic.getXsmall())){
tic.getLblA1().setIcon(tic.getX());
p8='x';
who='x';
tic.getLblPlayer().setIcon(tic.getOsmall());
}
}
}
}
Generalnie jest trochę burdel z tymi getterami, ale nie mam na razie pomysłu jak w inny sposób dać dostęp do etykiet i buttonów.