我有一个导入到另一个项目的 Java 项目(俄罗斯方块游戏)。基本上我想通过一个简单的按钮单击运行游戏(在 NetBeans JFrame 中,而不是一个类)。
我可以让它这样工作:
public static void main(String args[])
{
TicTacToe.main(args)
}
它工作正常,但是当我尝试按钮单击事件时:
private void TetrisButtonActionPerformed(java.awt.event.ActionEvent evt) {
TicTacToe.main(new String[0]);
}
它给了我一个空白框架,我想这可能是参数,因为它在主方法中工作得很好,所以我创建了一个变量和方法来从主方法返回参数,但它没有解决问题。
任何提示或解释为什么它不能工作?
完整代码:
package tictactoe;
import java.awt.Component;
import javax.swing.JOptionPane;
public class TicTacToe {
public static boolean playerTurn = true;
public static boolean playerWon = false;
public static boolean computerWon = false;
public static boolean playgame = true;
public static game board = new game();
public static void main(String[] args) {
//startGame();
while(playgame == true)
{
if(board.isVisible() == false)
{
board.setVisible(true);
}
}
}
public static void checkforwin()
{
//player win
if(board.button1.getText().equals("X") )
{
if(board.button4.getText().equals("X") )
{
if(board.button7.getText().equals("X") )
{
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(null, "Player won!");
}
}
}
if(board.button1.getText().equals("X") )
{
if(board.button5.getText().equals("X") )
{
if(board.button9.getText().equals("X") )
{
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(null, "Player won!");
}
}
}
if(board.button1.getText().equals("X") )
{
if(board.button2.getText().equals("X") )
{
if(board.button3.getText().equals("X") )
{
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(null, "Player won!");
}
}
}
if(board.button3.getText().equals("X") )
{
if(board.button5.getText().equals("X") )
{
if(board.button7.getText().equals("X") )
{
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(null, "Player won!");
}
}
}
if(board.button3.getText().equals("X") )
{
if(board.button6.getText().equals("X") )
{
if(board.button9.getText().equals("X") )
{
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(null, "Player won!");
}
}
}
if(board.button7.getText().equals("X") )
{
if(board.button8.getText().equals("X") )
{
if(board.button9.getText().equals("X") )
{
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(null, "Player won!");
}
}
}
if(board.button4.getText().equals("X") )
{
if(board.button5.getText().equals("X") )
{
if(board.button6.getText().equals("X") )
{
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(null, "Player won!");
}
}
}
if(board.button2.getText().equals("X") )
{
if(board.button5.getText().equals("X") )
{
if(board.button8.getText().equals("X") )
{
playerWon = true;
computerWon = false;
JOptionPane.showMessageDialog(null, "Player won!");
}
}
}
//computer won
if(board.button1.getText().equals("O") )
{
if(board.button4.getText().equals("O") )
{
if(board.button7.getText().equals("O") )
{
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(null, "Computer won!");
}
}
}
if(board.button1.getText().equals("O") )
{
if(board.button5.getText().equals("O") )
{
if(board.button9.getText().equals("O") )
{
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(null, "Computer won!");
}
}
}
if(board.button1.getText().equals("O") )
{
if(board.button2.getText().equals("O") )
{
if(board.button3.getText().equals("O") )
{
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(null, "Computer won!");
}
}
}
if(board.button3.getText().equals("O") )
{
if(board.button5.getText().equals("O") )
{
if(board.button7.getText().equals("O") )
{
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(null, "Computer won!");
}
}
}
if(board.button3.getText().equals("O") )
{
if(board.button6.getText().equals("O") )
{
if(board.button9.getText().equals("O") )
{
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(null, "Computer won!");
}
}
}
if(board.button7.getText().equals("O") )
{
if(board.button8.getText().equals("O") )
{
if(board.button9.getText().equals("O") )
{
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(null, "Computer won!");
}
}
}
if(board.button4.getText().equals("O") )
{
if(board.button5.getText().equals("O") )
{
if(board.button6.getText().equals("O") )
{
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(null, "Computer won!");
}
}
}
if(board.button2.getText().equals("O") )
{
if(board.button5.getText().equals("O") )
{
if(board.button8.getText().equals("O") )
{
playerWon = false;
computerWon = true;
JOptionPane.showMessageDialog(null, "Computer won!");
}
}
}
if(playerWon == true || computerWon == true)
{
board.setVisible(false);
int dialogresult = JOptionPane.showConfirmDialog(null, "Désirez vous jouer une autre partie? ","test",JOptionPane.YES_NO_OPTION);
//ask user to select if he want to begin a new game
if(dialogresult == JOptionPane.YES_OPTION)
{
playerWon = false;
computerWon = false;
board.setVisible(true);
board.button1.setText("");
board.button2.setText("");
board.button3.setText("");
board.button4.setText("");
board.button5.setText("");
board.button6.setText("");
board.button7.setText("");
board.button8.setText("");
board.button9.setText("");
}
else
{
playgame = false;
}
}
}
public static void startGame()
{
while(playgame == true)
{
if(board.isVisible() == false)
{
board.setVisible(true);
}
}
}
}
public class game extends JFrame{
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
JButton button7 = new JButton();
JButton button8 = new JButton();
JButton button9 = new JButton();
TicTacToe ttt = new TicTacToe();
public game()
{
initComponents();
button1.setText("");
button2.setText("");
button3.setText("");
button4.setText("");
button5.setText("");
button6.setText("");
button7.setText("");
button8.setText("");
button9.setText("");
}
private void button1ActionPerformed(ActionEvent e) {
if(button1.getText().equals("") );
{
if(TicTacToe.playerTurn == true)
{
button1.setText("X");
TicTacToe.checkforwin();
TicTacToe.playerTurn = false;
}
else
{
button1.setText("O");
TicTacToe.checkforwin();
TicTacToe.playerTurn = true;
}
}
}
private void button2ActionPerformed(ActionEvent e) {
if(TicTacToe.playerTurn == true)
{
button2.setText("X");
TicTacToe.checkforwin();
TicTacToe.playerTurn = false;
}
else
{
button2.setText("O");
TicTacToe.checkforwin();
TicTacToe.playerTurn = true;
}
}
private void button3ActionPerformed(ActionEvent e) {
if(TicTacToe.playerTurn == true)
{
button3.setText("X");
TicTacToe.checkforwin();
TicTacToe.playerTurn = false;
}
else
{
button3.setText("O");
TicTacToe.checkforwin();
TicTacToe.playerTurn = true;
}
}
private void button4ActionPerformed(ActionEvent e) {
if(TicTacToe.playerTurn == true)
{
button4.setText("X");
TicTacToe.checkforwin();
TicTacToe.playerTurn = false;
}
else
{
button4.setText("O");
TicTacToe.checkforwin();
TicTacToe.playerTurn = true;
}
}
private void button5ActionPerformed(ActionEvent e) {
if(TicTacToe.playerTurn == true)
{
button5.setText("X");
TicTacToe.checkforwin();
TicTacToe.playerTurn = false;
}
else
{
button5.setText("O");
TicTacToe.checkforwin();
TicTacToe.playerTurn = true;
}
}
private void button6ActionPerformed(ActionEvent e) {
if(TicTacToe.playerTurn == true)
{
button6.setText("X");
TicTacToe.checkforwin();
TicTacToe.playerTurn = false;
}
else
{
button6.setText("O");
TicTacToe.checkforwin();
TicTacToe.playerTurn = true;
}
}
private void button7ActionPerformed(ActionEvent e) {
if(TicTacToe.playerTurn == true)
{
button7.setText("X");
TicTacToe.checkforwin();
TicTacToe.playerTurn = false;
}
else
{
button7.setText("O");
TicTacToe.checkforwin();
TicTacToe.playerTurn = true;
}
}
private void button8ActionPerformed(ActionEvent e) {
if(TicTacToe.playerTurn == true)
{
button8.setText("X");
TicTacToe.checkforwin();
TicTacToe.playerTurn = false;
}
else
{
button8.setText("O");
TicTacToe.checkforwin();
TicTacToe.playerTurn = true;
}
}
private void button9ActionPerformed(ActionEvent e) {
if(TicTacToe.playerTurn == true)
{
button9.setText("X");
TicTacToe.checkforwin();
TicTacToe.playerTurn = false;
}
else
{
button9.setText("O");
TicTacToe.checkforwin();
TicTacToe.playerTurn = true;
}
}