Java 如何设置Tictaoe游戏项目的界面?

Java 如何设置Tictaoe游戏项目的界面?,java,swing,jlabel,layout-manager,Java,Swing,Jlabel,Layout Manager,我正在尝试为Tic-tac-toe制作界面。我对我的9个字段使用JLabels方法。程序中没有错误,但我不知道怎么做。请阅读下面我的问题,请给我建议 import javax.swing.*; import java.awt.*; import java.awt.geom.*; public class ticTac extends JFrame { static JLabel label0 = new JLabel(); static JLabel label1

我正在尝试为Tic-tac-toe制作界面。我对我的9个字段使用JLabels方法。程序中没有错误,但我不知道怎么做。请阅读下面我的问题,请给我建议

 import javax.swing.*;
 import java.awt.*;
 import java.awt.geom.*;

 public class ticTac extends JFrame
   {
    static JLabel label0 = new JLabel();
    static JLabel label1 = new JLabel();
    static JLabel label2 = new JLabel();
    static JLabel label3 = new JLabel();
    static JLabel label4 = new JLabel();
    static JLabel label5 = new JLabel();
    static JLabel label6 = new JLabel();
    static JLabel label7 = new JLabel();
    static JLabel label8 = new JLabel();
    static JLabel[] choiceLabel = new JLabel[9];

    static ImageIcon burger = new ImageIcon("Cross.jpg");

 public static void main(String []args)
    {
        new ticTac().show();   
    }

 public ticTac()
    {
        setVisible(true);
        setSize(450,450);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     

        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints myGrid = new GridBagConstraints();

        GridBagConstraints gridConstraints;
        choiceLabel[0] = label0;
        choiceLabel[1] = label1;
        choiceLabel[2] = label2;
        choiceLabel[3] = label3;
        choiceLabel[4] = label4;
        choiceLabel[5] = label5;
        choiceLabel[6] = label6;
        choiceLabel[7] = label7;
        choiceLabel[8] = label8;
此方法在水平行中从1到9绘制标签,因此我不希望这样,我希望我的程序将在一行中绘制三个框,在第二行中绘制三个框和三个框。请帮帮我。

        for (int i = 0; i < 9; i++)
            {
              gridConstraints = new GridBagConstraints();
              choiceLabel[i].setPreferredSize(new Dimension(burger.getIconWidth(), burger.getIconHeight()));
              choiceLabel[i].setOpaque(true);
              choiceLabel[i].setBackground(Color.RED);
              gridConstraints.gridx = i;
              gridConstraints.gridy = 0;
              gridConstraints.insets = new Insets(10, 10, 10, 10);
              getContentPane().add(choiceLabel[i], gridConstraints);



        pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 * (screenSize.height - getHeight())), getWidth(), getHeight());
    }
}
}
for(int i=0;i<9;i++)
{
gridConstraints=新的GridBagConstraints();
choiceLabel[i].setPreferredSize(新维度(burger.getIconWidth(),burger.getIconHeight());
choiceLabel[i].Set不透明(真);
choiceLabel[i].挫折背景(颜色.红色);
gridConstraints.gridx=i;
gridConstraints.gridy=0;
gridConstraints.insets=新的insets(10,10,10,10);
getContentPane().add(choiceLabel[i],gridConstraints);
包装();
维度screenSize=Toolkit.getDefaultToolkit().getScreenSize();
设置边界((int)(0.5*(screenSize.width-getWidth()),(int)(0.5*(screenSize.height-getHeight())、getWidth()、getHeight());
}
}
}
您可以使用

使用反馈更新

只是一些友好的反馈

  • 扩展
    JFrame
    ,实际上没有任何意义,您没有为其功能添加任何重要内容。最好使用
    JPanel
    ,然后将其添加到
    JFrame
    的实例中。如果没有其他内容,您现在可以选择将其添加到您喜欢的任何其他容器中
  • 在大多数情况下,不需要使用
    JFrame#getContentPane
    (除非您使用的是Java 1.4或更低版本),因为
    setLayout
    add
    (在一大堆其他文件中)被映射为直接使用
    getContentPane
  • < LI>如果要在屏幕中间放置框架,请使用<代码> StestOrror相对应(NULL)< /代码>。它使用起来更简单。尽可能避免使用
    setBounds
  • 您应该确保UI是在事件调度线程的上下文中启动的。请查看以了解更多详细信息
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import static tictactoe.TicTacToe.choiceLabel;

public class LayoutTicTacToe {

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

    public LayoutTicTacToe() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridLayout(3, 3, 10, 10));

                choiceLabel[0] = new JLabel("_");
                choiceLabel[1] = new JLabel("_");
                choiceLabel[2] = new JLabel("_");
                choiceLabel[3] = new JLabel("_");
                choiceLabel[4] = new JLabel("_");
                choiceLabel[5] = new JLabel("_");
                choiceLabel[6] = new JLabel("_");
                choiceLabel[7] = new JLabel("_");
                choiceLabel[8] = new JLabel("_");

                for (int i = 0; i < 9; i++) {
                    choiceLabel[i].setText("_");
                    choiceLabel[i].setHorizontalAlignment(JLabel.CENTER);
                    choiceLabel[i].setVerticalAlignment(JLabel.CENTER);
                    choiceLabel[i].setOpaque(true);
                    choiceLabel[i].setBackground(Color.RED);
                    frame.add(choiceLabel[i]);
                }
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import static tictactoe.TicTacToe.choiceLabel;

public class LayoutTicTacToe {

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

    public LayoutTicTacToe() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());

                choiceLabel[0] = new JLabel("_");
                choiceLabel[1] = new JLabel("_");
                choiceLabel[2] = new JLabel("_");
                choiceLabel[3] = new JLabel("_");
                choiceLabel[4] = new JLabel("_");
                choiceLabel[5] = new JLabel("_");
                choiceLabel[6] = new JLabel("_");
                choiceLabel[7] = new JLabel("_");
                choiceLabel[8] = new JLabel("_");

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(10, 10, 10, 10);
                int col = 0;
                int row = 0;
                for (int i = 0; i < 9; i++) {
                    gbc.gridx = col;
                    gbc.gridy = row;
                    choiceLabel[i].setText("_");
                    choiceLabel[i].setHorizontalAlignment(JLabel.CENTER);
                    choiceLabel[i].setVerticalAlignment(JLabel.CENTER);
                    choiceLabel[i].setOpaque(true);
                    choiceLabel[i].setBackground(Color.RED);
                    frame.add(choiceLabel[i], gbc);
                    col++;
                    if (col > 2) {
                        col = 0;
                        row++;
                    }
                }
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}