我正在做一个井字游戏,我希望能够在单击时使按钮交替显示x和o。现在,它们在第一次单击时都是x,在第二次单击时都是o。我也尝试过使用和不使用关键字。
这是按钮类
public class ticTacBoard extends JFrame
{
Toebuttons toe[] = new Toebuttons[9];
public ticTacBoard()
{
super("Tic tac board");
setSize(500,500);
setLayout(new GridLayout(3,3));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
toFront();
for(int i = 0; i<toe.length; i++)
{
toe[i] = new Toebuttons();
add(toe[i]);
}
setVisible(true);
}
}
这是董事会课程
global_queue
答案 0 :(得分:4)
您的问题是无效的对象设计之一:每个按钮都有其自己的布尔x
状态,该状态独立于所有其他按钮,您不应这样做。实际上,您应该有一个Game对象,该对象包含按钮并且与按钮分开,并保持此状态并基于此状态控制按钮对按下的响应。
侧面说明:我不会扩展JButton,而是会使用 JButtons。
例如,您可以为所有JButton提供 相同 ActionListener:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TicTacFoo extends JPanel {
private static final long serialVersionUID = 1L;
private static final int ROWS = 3;
private static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 80);
private JButton[][] buttons = new JButton[ROWS][ROWS];
private boolean xTurn = true;
private int count = 0;
public TicTacFoo() {
JPanel buttonPanel = new JPanel(new GridLayout(ROWS, ROWS));
setLayout(new GridLayout(ROWS, ROWS));
// single ActionListener for all buttons
ButtonListener buttonListener = new ButtonListener();
// create buttons in nested for loop
for (int row = 0; row < buttons.length; row++) {
for (int col = 0; col < buttons[row].length; col++) {
buttons[row][col] = new JButton(" ");
buttons[row][col].setFont(BTN_FONT);
buttons[row][col].addActionListener(buttonListener);
buttonPanel.add(buttons[row][col]);
}
}
setLayout(new BorderLayout());
add(buttonPanel);
add(new JButton(new ResetAction()), BorderLayout.PAGE_END);
}
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// get the button that was pressed
JButton buttonPressed = (JButton) e.getSource();
String text = buttonPressed.getText().trim();
if (!text.isEmpty()) {
// button already has been pressed
// so exit from the listener
return;
}
if (xTurn) {
buttonPressed.setText("X");
} else {
buttonPressed.setText("O");
}
int rowPressed = -1;
int colPressed = -1;
// which button pressed?
for (int row = 0; row < buttons.length; row++) {
for (int col = 0; col < buttons[row].length; col++) {
if (buttons[row][col] == buttonPressed) {
rowPressed = row;
colPressed = col;
break;
}
}
}
// TODO: here code where you would test for win
// ......
// swap turn
xTurn = !xTurn;
// increment count:
count++;
System.out.printf("count: %d, [row, col]: [%d, %d]%n", count, rowPressed, colPressed);
}
}
// resets program back to initial state
@SuppressWarnings("serial")
class ResetAction extends AbstractAction {
public ResetAction() {
super("Reset");
}
@Override
public void actionPerformed(ActionEvent e) {
// loop through all buttons, resetting state back to initial
for (JButton[] buttonRow : buttons) {
for (JButton button : buttonRow) {
button.setText(" ");
}
}
xTurn = true;
count = 0;
}
}
private static void createAndShowGui() {
TicTacFoo mainPanel = new TicTacFoo();
JFrame frame = new JFrame("TicTacFoo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
答案 1 :(得分:0)
这不是C ++,也不是
boolean x = true;
在Java中不是global
。要模拟Java中可以理解为“全局”(对于所有类实例而言常见)的变量,您需要将其声明为static
,例如
static boolean x = true;