gpt4 book ai didi

java - 使用 JButton 从另一个类运行主类

转载 作者:行者123 更新时间:2023-11-29 07:06:08 26 4
gpt4 key购买 nike

我将一个 Java 项目(俄罗斯方 block 游戏)导入到另一个项目中。基本上我想通过简单的按钮点击运行游戏(在 NetBeans JFrame 中,而不是类)。

我可以让它这样工作:

public static void main(String args[])
{
TicTacToe.main(args)
}

它工作正常,但是当我在按钮点击事件中尝试时:

private void TetrisButtonActionPerformed(java.awt.event.ActionEvent evt) {       
TicTacToe.main(new String[0]);
}

它给了我一个空白框,我想这可能是参数,因为它在 main 方法中工作得很好所以我创建了一个变量和方法来从 main 方法返回 args 但它没有解决问题。

关于为什么它不能工作的任何提示或解释?

完整代码:

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;
}
}

最佳答案

这是有问题的代码:

while(playgame == true)
{
if(board.isVisible() == false)
{
board.setVisible(true);
}
}

当从 Action 监听器调用时,您处于事件分派(dispatch)线程中。当 playgame 为真时,该代码会循环运行,永远不会将控制权交还给 EDT 以便它可以进行绘图。

你的 main 应该看起来像这样:

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Create board here, if needed
createBoard();
board.setVisible(true);
// start a new game here
startGame();
}
});
}

invokeLater() 将代码推送到事件调度线程,这是线程安全访问和创建 swing 组件必需的,所以不要跳过它,即使没有它,您的代码似乎也能正常工作。

此外,如果您根本不从 Action 监听器调用 main(),而只调用启动新游戏的方法,可能会更简洁。

关于java - 使用 JButton 从另一个类运行主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19564219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com