我有9个场景制作一个9条件语句,将查看是否所有我的JButton已按下,没有找到赢家,但它给我" error: ' void '类型不允许这里。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JFrame
{
private final int HEIGHT = 450;
private final int WIDTH = 500;
private static JButton [] button = new JButton[9];
private static Action [] playerTurn = new Action[9];
private static JLabel [] label;
private int player = 1;
private Font arial = new Font("Arial", Font.BOLD, 20);
private static int lockButtons = 0;
public TicTacToe ()
{
setTitle( " tic tak tow ");
setSize( HEIGHT, WIDTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4,3));
int num = 0;
for(int i = 0; i < 9; i++ )
{
button[i] = new JButton( "B" + i + 1);
playerTurn[i] = new Action();
add(button[i]);
button[i].setFont(arial);
button[i].addActionListener(playerTurn[i]);
}
setVisible(true);
}
private class Action implements ActionListener
{
public void actionPerformed(ActionEvent playerMove)
{
//Get button pressed using GetSource Command
JButton whatPlayer=(JButton)(playerMove.getSource());
if(player == 1)
{
player++;
whatPlayer.setText("player1");
whatPlayer.setEnabled(false);
validate();
return;
}
if (player == 2)
{
player--;
whatPlayer.setText("player2");
whatPlayer.setEnabled(false);
validate();
return;
}
}
public void validate()
{
if(button[0].getText().equals(button[1].getText()) && button[1].getText().equals(button[2].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
gameOver();
}
if(button[3].getText().equals(button[4].getText()) && button[4].getText().equals(button[5].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[3].getText());
gameOver();
}
if(button[6].getText().equals(button[7].getText()) && button[7].getText().equals(button[8].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[6].getText());
gameOver();
}
if(button[0].getText().equals(button[3].getText()) && button[3].getText().equals(button[6].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
gameOver();
}
if(button[1].getText().equals(button[4].getText()) && button[4].getText().equals(button[7].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[1].getText());
gameOver();
}
if(button[1].getText().equals(button[4].getText()) && button[4].getText().equals(button[7].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[1].getText());
gameOver();
}
if(button[2].getText().equals(button[5].getText()) && button[5].getText().equals(button[8].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[2].getText());
gameOver();
}
if(button[0].getText().equals(button[4].getText()) && button[4].getText().equals(button[8].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[0].getText());
gameOver();
}
if(button[2].getText().equals(button[4].getText()) && button[4].getText().equals(button[6].getText()))
{
JOptionPane.showMessageDialog(null,"Thank you the winner is" + button[2].getText());
gameOver();
}
if ( button.length == setEnabled(false)) // this is the problem. Don't know if I worded it correctly
{
JOptionPane.showMessageDialog(null,"This was a Draw");
}
}
public void gameOver()
{
for( int x = 0; x < button.length; x++)
{
button[x].setEnabled(false);
}
}
}
public static void main(String[] arg)
{
new TicTacToe();
}
}
按钮。Length将等于9,因为您定义了一个包含9个元素的数组
private static JButton [] button = new JButton[9];
如何在if循环中进行if ( button.length == setEnabled(false))
比较?setEnabled(false)
将禁用JFrame并且返回类型为void,这里你将int与void进行比较,这就是为什么在编译时抛出错误。
int i;
for(i=0;i<button.length;i++){
if(b[i].isEnabled()){
break;
}
}
if(i==button.length){
JOptionPane.showMessageDialog(null,"This was a Draw");
}