so I wanted to programm a tictactoe and when I created the getters and setters they didnt give me the variable back. And I looked up what might be wrong about them but I didn't find the error. I hope someone can explain me what is wrong. Its just about setting a panel and getting it back and setting a string and getting it back. Im new to programming and would appreciate if someone could help me with this problem.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* @author Anid Memisi
*
* @date 21.02.2020
*
* @version 1.0
*/
public class TicTacToe {
public void tictactoe() {
//basic constructor
TicTacToe t = new TicTacToe();
t.GridLayout();
}
public void Entscheid() {
TicTacToe t = new TicTacToe();
t.Update();
}
public void Update() {
Setter_and_Getter s = new Setter_and_Getter();
String feldI=s.getFeldi();
JPanel panel=s.getPanel();
System.out.print(feldI);
System.out.print(panel);
String[] feld = new String[] {" "," "," "," "," "," "," "," "," "};
feld[0] = feldI;
System.out.print(feldI);
if(!feldI.equals(" ")){
if(panel != null) {
panel.removeAll();
panel.invalidate();
panel.repaint();
}
panel.setLayout(new GridLayout(3, 3));
//creates the tiles for the tictactoe
JButton button1 = new JButton(feld[0]);
JButton button2 = new JButton(feld[1]);
JButton button3 = new JButton(feld[2]);
JButton button4 = new JButton(feld[3]);
JButton button5 = new JButton(feld[4]);
JButton button6 = new JButton(feld[5]);
JButton button7 = new JButton(feld[6]);
JButton button8 = new JButton(feld[7]);
JButton button9 = new JButton(feld[8]);
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(button7);
panel.add(button8);
panel.add(button9);
panel.revalidate();
panel.repaint();
panel.requestFocus();
}
}
public void GridLayout() {
TicTacToe t = new TicTacToe();
Setter_and_Getter s = new Setter_and_Getter();
String[] feld = new String[] {" "," "," "," "," "," "," "," "," "};
JFrame frame = new JFrame("Grid Layout");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,600);
JPanel panel = new JPanel();
//creates the tiles for the tictactoe
panel.setLayout(new GridLayout(3,3));
JButton button1 = new JButton(feld[0]);
JButton button2 = new JButton(feld[1]);
JButton button3 = new JButton(feld[2]);
JButton button4 = new JButton(feld[3]);
JButton button5 = new JButton(feld[4]);
JButton button6 = new JButton(feld[5]);
JButton button7 = new JButton(feld[6]);
JButton button8 = new JButton(feld[7]);
JButton button9 = new JButton(feld[8]);
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(button7);
panel.add(button8);
panel.add(button9);
frame.add(panel);
s.setPanel(panel);
//sends the input to the listener
DerHandler handler = new DerHandler();
button1.addActionListener(handler);
DerHandler2 handler2 = new DerHandler2();
button2.addActionListener(handler2);
DerHandler3 handler3 = new DerHandler3();
button3.addActionListener(handler3);
DerHandler4 handler4 = new DerHandler4();
button4.addActionListener(handler4);
DerHandler5 handler5 = new DerHandler5();
button5.addActionListener(handler5);
DerHandler6 handler6 = new DerHandler6();
button6.addActionListener(handler6);
DerHandler7 handler7 = new DerHandler7();
button7.addActionListener(handler7);
DerHandler8 handler8 = new DerHandler8();
button8.addActionListener(handler8);
DerHandler9 handler9 = new DerHandler9();
button9.addActionListener(handler9);
}
private class DerHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TicTacToe t = new TicTacToe();
Setter_and_Getter s = new Setter_and_Getter();
String feldI =" ";
//gets the text of the tile
Object source = e.getSource();
//compares it
if (source instanceof JButton) {
feldI = "1";
}
s.setFeldI(feldI);
System.out.print(feldI);
t.Entscheid();
}
}
private class DerHandler2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TicTacToe t = new TicTacToe();
Setter_and_Getter s = new Setter_and_Getter();
String feldI =" ";
//gets the text of the tile
Object source = e.getSource();
//compares it
if (source instanceof JButton) {
feldI = "2";
}
s.setFeldI(feldI);
System.out.print(feldI);
t.Entscheid();
}
}
private class DerHandler3 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TicTacToe t = new TicTacToe();
Setter_and_Getter s = new Setter_and_Getter();
String feldI =" ";
//gets the text of the tile
Object source = e.getSource();
//compares it
if (source instanceof JButton) {
feldI = "3";
}
s.setFeldI(feldI);
System.out.print(feldI);
t.Entscheid();
}
}
private class DerHandler4 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TicTacToe t = new TicTacToe();
Setter_and_Getter s = new Setter_and_Getter();
String feldI =" ";
//gets the text of the tile
Object source = e.getSource();
//compares it
if (source instanceof JButton) {
feldI = "4";
}
s.setFeldI(feldI);
System.out.print(feldI);
t.Entscheid();
}
}
private class DerHandler5 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TicTacToe t = new TicTacToe();
Setter_and_Getter s = new Setter_and_Getter();
String feldI =" ";
//gets the text of the tile
Object source = e.getSource();
//compares it
if (source instanceof JButton) {
feldI = "5";
}
s.setFeldI(feldI);
System.out.print(feldI);
t.Entscheid();
}
}
private class DerHandler6 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TicTacToe t = new TicTacToe();
Setter_and_Getter s = new Setter_and_Getter();
String feldI =" ";
//gets the text of the tile
Object source = e.getSource();
//compares it
if (source instanceof JButton) {
feldI = "6";
}
s.setFeldI(feldI);
System.out.print(feldI);
t.Entscheid();
}
}
private class DerHandler7 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TicTacToe t = new TicTacToe();
Setter_and_Getter s = new Setter_and_Getter();
String feldI =" ";
//gets the text of the tile
Object source = e.getSource();
//compares it
if (source instanceof JButton) {
feldI = "7";
}
s.setFeldI(feldI);
System.out.print(feldI);
t.Entscheid();
}
}
private class DerHandler8 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TicTacToe t = new TicTacToe();
Setter_and_Getter s = new Setter_and_Getter();
String feldI =" ";
//gets the text of the tile
Object source = e.getSource();
//compares it
if (source instanceof JButton) {
feldI = "8";
}
s.setFeldI(feldI);
System.out.print(feldI);
t.Entscheid();
}
}
private class DerHandler9 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TicTacToe t = new TicTacToe();
Setter_and_Getter s = new Setter_and_Getter();
String feldI =" ";
//gets the text of the tile
Object source = e.getSource();
//compares it
if (source instanceof JButton) {
feldI = "9";
}
s.setFeldI(feldI);
System.out.print(feldI);
t.Entscheid();
}
}
public static void main(String[] args) {
TicTacToe t = new TicTacToe();
t.tictactoe();
}
}
import javax.swing.*;
public class Setter_and_Getter {
private String feldI;
private JPanel panel;
public void setFeldI(String feldI){
this.feldI=feldI;
}
public void setPanel(JPanel panel){
this.panel=panel;
}
public String getFeldi(){
return this.feldI;
}
public JPanel getPanel(){
return this.panel;
}
}
In each actionPerformed() method you create a new instance of Setter_and_Getter and set a value. At the end of these methods, the scope of the method ends, therefore the Setter_and_Getter object gets removed by the garbage collector.
So there is no way to get access to these object instances later.
In your Update() method you create again a new instance of Setter_and_Getter and read two values out. The getters return null because the values are really null. You did not call any setter of that object instance before.
You need to store the object instances somewhere outside your methods and then read from the previously filled instance instead of reading a fresh new empty instance.
By the way: you should use lowe-case names for all methods of the TicTacToe class. Upper case names indicate a class name by convention.