简体   繁体   中英

JFrame window won't show up

(By the way, I am not looking for critique on the code itself; I know it's inefficient, I'm simply testing out different things, and would like to learn for myself.) I'm running this code, and I can't figure out why the JFrame that I've created doesn't appear when I run the program. I see the program name in the top left (I'm on a mac , by the way, so this means that the program is running) for about 3-5 seconds, then it quits. It doesn't show any errors, it just doesn't show a JFrame either.

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)
{

}
 }

You have two issues:

  • As stated in the comments you received, your main method is empty. Follow the suggestions you're having there.

  • The board JPanel is not associated with the JFrame. At the end of your constructor method add this line:

     this.add (board); 

Your main method should look as such:

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

This calls a new instance of your TicTacToe class, which will automatically run the constructor.

jframename  jf =  new jframename();
jf.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM