Hey,
ich bin noch ein ziemlicher Anfänger, was Java betrifft. Trotzdem versuche ich mich gerade an einem kleinen Tic-Tac-Toe mit GUI und habe derzeit ein Problem (was eigentlich OOP betrifft):
Alles was mit TODO ausgeklammert ist, passt noch nicht so ganz.
Also ich habe eine Methode resetButtonValues, in der ich die Attributwerte der Eigenschaft text von allen 9 Buttons zurücksetzen möchte. Allerdings hat die IDE Probleme, wenn ich mit button.setText(""); darauf zugreife, also stimmen anscheinend die Zugriffsrechte nicht so ganz.
Wie greife ich nun in der Methode auf die JButtons zu?
ich bin noch ein ziemlicher Anfänger, was Java betrifft. Trotzdem versuche ich mich gerade an einem kleinen Tic-Tac-Toe mit GUI und habe derzeit ein Problem (was eigentlich OOP betrifft):
Java:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TicTacToe {
private JFrame frmTictactoe;
private Object button;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TicTacToe window = new TicTacToe();
window.frmTictactoe.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TicTacToe() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmTictactoe = new JFrame();
frmTictactoe.setResizable(false);
frmTictactoe.setTitle("Tic-Tac-Toe");
frmTictactoe.setBounds(100, 100, 450, 300);
frmTictactoe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblTictactoe = new JLabel("Tic-Tac-Toe");
lblTictactoe.setFont(new Font("Tahoma", Font.PLAIN, 20));
final int[][] matrix = this.createGameMatrix(3, 3);
final JButton button = new JButton("");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (matrix[1][1] == 0) {
button.setText("X");
matrix[1][1] = 1;
if (!isPlayerWinner(matrix, 1)) {
handleChoiceOfAI(matrix);
}
else if (isPlayerWinner(matrix, 1)) {
JOptionPane.showMessageDialog(null, "Glückwunsch, du hast gewonnen!");
// resetButtonValues();
}
else if (sum3x3MatrixValues(matrix) >= 13) {
JOptionPane.showMessageDialog(null, "Das Spiel ist unentschieden ausgegangen!");
// resetButtonValues();
}
}
}
});
final JButton button_1 = new JButton("");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (matrix[1][2] == 0) {
button_1.setText("X");
matrix[1][2] = 1;
if (!isPlayerWinner(matrix, 1)) {
handleChoiceOfAI(matrix);
}
else if (isPlayerWinner(matrix, 1)) {
JOptionPane.showMessageDialog(null, "Glückwunsch, du hast gewonnen!");
// resetButtonValues();
}
else if (sum3x3MatrixValues(matrix) >= 13) {
JOptionPane.showMessageDialog(null, "Das Spiel ist unentschieden ausgegangen!");
// resetButtonValues();
}
}
}
});
final JButton button_2 = new JButton("");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (matrix[1][3] == 0) {
button_2.setText("X");
matrix[1][3] = 1;
if (!isPlayerWinner(matrix, 1)) {
handleChoiceOfAI(matrix);
}
else if (isPlayerWinner(matrix, 1)) {
JOptionPane.showMessageDialog(null, "Glückwunsch, du hast gewonnen!");
// resetButtonValues();
}
else if (sum3x3MatrixValues(matrix) >= 13) {
JOptionPane.showMessageDialog(null, "Das Spiel ist unentschieden ausgegangen!");
// resetButtonValues();
}
}
}
});
final JButton button_3 = new JButton("");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (matrix[2][1] == 0) {
button_3.setText("X");
matrix[2][1] = 1;
if (!isPlayerWinner(matrix, 1)) {
handleChoiceOfAI(matrix);
}
else if (isPlayerWinner(matrix, 1)) {
JOptionPane.showMessageDialog(null, "Glückwunsch, du hast gewonnen!");
// resetButtonValues();
}
else if (sum3x3MatrixValues(matrix) >= 13) {
JOptionPane.showMessageDialog(null, "Das Spiel ist unentschieden ausgegangen!");
// resetButtonValues();
}
}
}
});
final JButton button_4 = new JButton("");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (matrix[2][2] == 0) {
button_4.setText("X");
matrix[2][2] = 1;
if (!isPlayerWinner(matrix, 1)) {
handleChoiceOfAI(matrix);
}
else if (isPlayerWinner(matrix, 1)) {
JOptionPane.showMessageDialog(null, "Glückwunsch, du hast gewonnen!");
// resetButtonValues();
}
else if (sum3x3MatrixValues(matrix) >= 13) {
JOptionPane.showMessageDialog(null, "Das Spiel ist unentschieden ausgegangen!");
// resetButtonValues();
}
}
}
});
final JButton button_5 = new JButton("");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (matrix[2][3] == 0) {
button_5.setText("X");
matrix[2][3] = 1;
if (!isPlayerWinner(matrix, 1)) {
handleChoiceOfAI(matrix);
}
else if (isPlayerWinner(matrix, 1)) {
JOptionPane.showMessageDialog(null, "Glückwunsch, du hast gewonnen!");
// resetButtonValues();
}
else if (sum3x3MatrixValues(matrix) >= 13) {
JOptionPane.showMessageDialog(null, "Das Spiel ist unentschieden ausgegangen!");
// resetButtonValues();
}
}
}
});
final JButton button_6 = new JButton("");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (matrix[3][1] == 0) {
button_6.setText("X");
matrix[3][1] = 1;
if (!isPlayerWinner(matrix, 1)) {
handleChoiceOfAI(matrix);
}
else if (isPlayerWinner(matrix, 1)) {
JOptionPane.showMessageDialog(null, "Glückwunsch, du hast gewonnen!");
// resetButtonValues();
}
else if (sum3x3MatrixValues(matrix) >= 13) {
JOptionPane.showMessageDialog(null, "Das Spiel ist unentschieden ausgegangen!");
// resetButtonValues();
}
}
}
});
final JButton button_7 = new JButton("");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (matrix[3][2] == 0) {
button_7.setText("X");
matrix[3][2] = 1;
if (!isPlayerWinner(matrix, 1)) {
handleChoiceOfAI(matrix);
}
else if (isPlayerWinner(matrix, 1)) {
JOptionPane.showMessageDialog(null, "Glückwunsch, du hast gewonnen!");
// resetButtonValues();
}
else if (sum3x3MatrixValues(matrix) >= 13) {
JOptionPane.showMessageDialog(null, "Das Spiel ist unentschieden ausgegangen!");
// resetButtonValues();
}
}
}
});
final JButton button_8 = new JButton("");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (matrix[3][3] == 0) {
button_8.setText("X");
matrix[3][3] = 1;
if (!isPlayerWinner(matrix, 1)) {
handleChoiceOfAI(matrix);
}
else if (isPlayerWinner(matrix, 1)) {
JOptionPane.showMessageDialog(null, "Glückwunsch, du hast gewonnen!");
// resetButtonValues();
}
else if (sum3x3MatrixValues(matrix) >= 13) {
JOptionPane.showMessageDialog(null, "Das Spiel ist unentschieden ausgegangen!");
// resetButtonValues();
}
}
}
});
GroupLayout groupLayout = new GroupLayout(frmTictactoe.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(69)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button, GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
.addComponent(button_3, GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
.addComponent(button_6, GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button_7, GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
.addComponent(button_4, GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
.addComponent(button_1, GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(button_8, GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
.addComponent(button_5, GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
.addComponent(button_2, GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
.addGap(79))
.addGroup(groupLayout.createSequentialGroup()
.addGap(156)
.addComponent(lblTictactoe, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
.addContainerGap(157, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(35)
.addComponent(lblTictactoe)
.addGap(63)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addComponent(button, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button_3, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addComponent(button_4, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addComponent(button_5, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.UNRELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(button_6, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addComponent(button_7, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addComponent(button_8, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
.addContainerGap(48, Short.MAX_VALUE))
);
frmTictactoe.getContentPane().setLayout(groupLayout);
}
// create a new matrix for the game which uses currently 3 rows and 3 columns
public int[][] createGameMatrix(int rows, int cols) {
int[][] matrix = new int[rows + 1][cols + 1];
return matrix;
}
// sum the values of a 3x3 matrix
public int sum3x3MatrixValues(int[][] matrix) {
int result = matrix[1][1] + matrix[1][2] + matrix[1][3] + matrix[2][1] + matrix[2][2] + matrix[2][3] + matrix[3][1] + matrix[3][2] + matrix[3][3];
return result;
}
// check if the player is the winner (require a 3x3 matrix)
public boolean isPlayerWinner(int[][] matrix, int player) {
boolean isWinner;
if (matrix[1][1] == player && matrix[1][2] == player && matrix[1][3] == player || matrix[2][1] == player && matrix[2][2] == player && matrix[2][3] == player ||
matrix[3][1] == player && matrix[3][2] == player && matrix[3][3] == player || matrix[1][1] == player && matrix[2][1] == player && matrix[3][1] == player ||
matrix[1][2] == player && matrix[2][2] == player && matrix[3][2] == player || matrix[1][3] == player && matrix[2][3] == player && matrix[3][3] == player ||
matrix[1][1] == player && matrix[2][2] == player && matrix[3][3] == player || matrix[1][3] == player && matrix[2][2] == player && matrix[3][1] == player) {
isWinner = true;
}
else {
isWinner = false;
}
return isWinner;
}
// generate a random number and handle choice of the artificial intelligence
public void handleChoiceOfAI(int[][] matrix) {
int randNum = randInt(1, 9);
if (randNum == 1 && matrix[1][1] == 0) {
// TODO: button.setText("O");
matrix[1][1] = 2;
}
else if (randNum == 2 && matrix[1][2] == 0) {
// TODO: button_1.setText("O");
matrix[1][2] = 2;
}
else if (randNum == 3 && matrix[1][3] == 0) {
// TODO: button_2.setText("O");
matrix[1][3] = 2;
}
else if (randNum == 4 && matrix[2][1] == 0) {
// TODO: button_3.setText("O");
matrix[2][1] = 2;
}
else if (randNum == 5 && matrix[2][2] == 0) {
// TODO: button_4.setText("O");
matrix[2][2] = 2;
}
else if (randNum == 6 && matrix[2][3] == 0) {
// TODO: button_5.setText("O");
matrix[2][3] = 2;
}
else if (randNum == 7 && matrix[3][1] == 0) {
// TODO: button_6.setText("O");
matrix[3][1] = 2;
}
else if (randNum == 8 && matrix[3][2] == 0) {
// TODO: button_7.setText("O");
matrix[3][2] = 2;
}
else if (randNum == 9 && matrix[3][3] == 0) {
// TODO: button_8.setText("O");
matrix[3][3] = 2;
}
if (isPlayerWinner(matrix, 2)) {
JOptionPane.showMessageDialog(null, "Schade, du hast verloren!");
// resetButtonValues();
}
else if (sum3x3MatrixValues(matrix) >= 13) {
JOptionPane.showMessageDialog(null, "Das Spiel ist unentschieden ausgegangen!");
// resetButtonValues();
}
}
// reset button values with button.setText("");
public void resetButtonValues() {
/*
* TODO: button.setText("");
* TODO: button_1.setText("");
* TODO: button_2.setText("");
* ...
*/
}
// generate a random number between a and b
public int randInt(int a, int b) {
b++;
return (int) (Math.random() * (b - a) + a);
}
}
Alles was mit TODO ausgeklammert ist, passt noch nicht so ganz.
Also ich habe eine Methode resetButtonValues, in der ich die Attributwerte der Eigenschaft text von allen 9 Buttons zurücksetzen möchte. Allerdings hat die IDE Probleme, wenn ich mit button.setText(""); darauf zugreife, also stimmen anscheinend die Zugriffsrechte nicht so ganz.
Wie greife ich nun in der Methode auf die JButtons zu?