gpt4 book ai didi

Java - 将标签定位在 GridLayout 上方

转载 作者:行者123 更新时间:2023-12-01 11:41:17 32 4
gpt4 key购买 nike

我正在尝试制作井字游戏,我有一个 3 x 3 按钮网格,但我想在按钮上方有一个栏,显示分数和玩家姓名,有人可以帮助我吗?到java,不知道从哪里开始。

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

public class NoughtsCrosses extends JFrame {
JPanel gamePanel = new JPanel();
JPanel scorePanel = new JPanel();
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("");
JLabel label = new JLabel("test");



public NoughtsCrosses(){
super("Noughts & Crosses");
setSize(400,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);

gamePanel.setLayout(new GridLayout(3,3));

gamePanel.add(button1);
gamePanel.add(button2);
gamePanel.add(button3);
gamePanel.add(button4);
gamePanel.add(button5);
gamePanel.add(button6);
gamePanel.add(button7);
gamePanel.add(button8);
gamePanel.add(button9);

add(gamePanel);
setVisible(true);
}



public static void main(String args[]){

new NoughtsCrosses();

}



}

最佳答案

其中一种解决方案是将 JFrame 设置为边框布局,然后在北位置添加一个新的 JLabel,并在中心位置添加 3x3 网格。

这是有关使用边框布局的 java 文档

https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

关于Java - 将标签定位在 GridLayout 上方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29503120/

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