0

(顺便说一句,我不是在寻找对代码本身的批评;我知道它效率低下,我只是在测试不同的东西,并且想自己学习。)我正在运行这段代码,我可以'不知道为什么JFrame我创建的那个在我运行程序时没有出现。我在左上角看到程序名称(mac顺便说一句,我在 a 上,所以这意味着程序正在运行)大约 3-5 秒,然后它退出。它没有显示任何错误,也没有显示任何错误JFrame

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TicTacToe extends JFrame
{ 
    JPanel board= new JPanel();
    static boolean isXTurn = true;
    static boolean isXTopLeft = false;
    static boolean isOTopLeft = false;
    static boolean isXTopCenter = false;
    static boolean isOTopCenter = false;
    static boolean isXTopRight = false;
    static boolean isOTopRight = false;
    static boolean isXCenterLeft = false;
    static boolean isOCenterLeft = false;
    static boolean isXCenterCenter = false;
    static boolean isOCenterCenter = false;
    static boolean isXCenterRight = false;
    static boolean isOCenterRight = false;
    static boolean isXBottomLeft = false;
    static boolean isOBottomLeft = false;
    static boolean isXBottomCenter = false;
    static boolean isOBottomCenter = false;
    static boolean isXBottomRight = false;
    static boolean isOBottomRight = false;

    String letterTopLeft;
    String letterTopCenter;
    String letterTopRight;
    String letterCenterLeft;
    String letterCenterCenter;
    String letterCenterRight;
    String letterBottomLeft;
    String letterBottomCenter;
    String letterBottomRight;

public TicTacToe()
{   
    JButton btnTopLeft = new JButton(letterTopLeft);
    JButton btnTopCenter = new JButton(letterTopCenter);
    JButton btnTopRight = new JButton(letterTopRight);
    JButton btnCenterLeft = new JButton(letterCenterLeft);
    JButton btnCenterCenter = new JButton(letterCenterCenter);
    JButton btnCenterRight = new JButton(letterCenterRight);
    JButton btnBottomLeft = new JButton(letterBottomLeft);
    JButton btnBottomCenter = new JButton(letterBottomCenter);
    JButton btnBottomRight = new JButton(letterBottomRight);

    this.setLayout(null);

    btnTopLeft.setLayout(null);
    btnTopCenter.setLayout(null);
    btnTopRight.setLayout(null);
    btnCenterLeft.setLayout(null);
    btnCenterCenter.setLayout(null);
    btnCenterRight.setLayout(null);
    btnBottomLeft.setLayout(null);
    btnBottomCenter.setLayout(null);
    btnBottomRight.setLayout(null);

    btnTopLeft.setBounds(0, 0, 100, 100);
    btnTopCenter.setBounds(100, 0, 100, 100);
    btnTopRight.setBounds(200, 0, 100, 100);
    btnCenterLeft.setBounds(0, 100, 100, 100);
    btnCenterCenter.setBounds(100, 100, 100, 100);
    btnCenterRight.setBounds(200, 100, 100, 100);
    btnBottomLeft.setBounds(0, 200, 100, 100);
    btnBottomCenter.setBounds(100, 200, 100, 100);
    btnBottomRight.setBounds(200, 200, 100, 100);

    board.setBounds(0, 0, 300, 300);

    btnTopLeft.setPreferredSize(new Dimension(100,100));

    board.add(btnTopLeft);
    board.add(btnTopCenter);
    board.add(btnTopRight);
    board.add(btnCenterLeft);
    board.add(btnCenterCenter);
    board.add(btnCenterRight);
    board.add(btnBottomLeft);
    board.add(btnBottomCenter);
    board.add(btnBottomRight);



    setSize(300, 300);
    setBackground(Color.gray);
    setTitle("Tic-Tac-Toe");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

public static void main(String[] theory)
{

}
 }
4

3 回答 3

8

你有两个问题:

  • 如您收到的评论中所述,您的主要方法是空的。遵循您那里的建议。

  • 板 JPanel 不与 JFrame 关联。在构造函数方法的末尾添加以下行:

    this.add (board);
    
于 2012-09-18T01:58:14.497 回答
1

您的主要方法应如下所示:

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

这将调用您的井字游戏类的新实例,该实例将自动运行构造函数。

于 2012-09-18T01:58:40.360 回答
1
jframename  jf =  new jframename();
jf.setVisible(true);
于 2012-09-18T03:44:47.337 回答