(顺便说一句,我不正在寻找对代码本身的批评;我知道它效率低下,我只是测试不同的东西,并且想自己学习。)
我正在运行此代码,我无法弄清楚为什么运行该程序时我创建的JFrame
没有出现。顺便提一下,我看到左上角的程序名称(我在mac
上,这意味着程序正在运行)大约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)
{
}
}
答案 0 :(得分:8)
您有两个问题:
如您收到的评论中所述,您的主要方法是空的。按照你在那里的建议。
主板JPanel与JFrame无关。在构造函数方法的末尾添加以下行:
this.add (board);
答案 1 :(得分:1)
您的主要方法应如此:
public static void main(String[] args) {
new TicTacToe();
}
这将调用TicTacToe类的新实例,该实例将自动运行构造函数。
答案 2 :(得分:1)
jframename jf = new jframename();
jf.setVisible(true);