Tic Tac Toe ゲームを Java で実装していますが、プログラムを実行してボタンを押すと、クリック メソッドが原因で例外が発生します。どういうわけか、JLabel が初期化されていないようです。なんで?
package piskvorky;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Piskvorky extends JFrame {
String[] tahy = {"O", "X"};
int tah = 0;
JButton[][] buttons;
JLabel stav;
int x, y;
public Piskvorky(int x, int y) {
this.x = x;
this.y = y;
setTitle("Piskvorky");
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new GridLayout(x, y+1));
JButton[][] buttons = new JButton[x][y];
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
buttons[i][j] = new JButton();
buttons[i][j].setPreferredSize(new Dimension(50, 50));
buttons[i][j].addActionListener(new MyTextListener());
add(buttons[i][j]);
}
}
JLabel stav = new JLabel();
add(stav);
pack();
}
class MyTextListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
JButton kliknute = (JButton) e.getSource();
setProperties(kliknute);
}
};
private void setProperties(JButton source)
{
source.setText(tahy[tah]);
source.setEnabled(false);
tah = 1-tah;
if(tah == 0)
stav.setText("Current player: o");
else
stav.setText("Current player: x");
}
public static void main(String[] args) {
new Piskvorky(10, 10).setVisible(true);
}
}