键盘按钮仅在光标悬停时显示

Keyboard buttons only shows when cursor hovers over

我有这个带有文本字段的 GUI 和一个由 JButton 组成的键盘:

但是我一开始运行的时候,显示是这样的:

只有当我的鼠标悬停在键盘上时,键盘按钮才会显示:

调整 window 的大小也会再次隐藏所有键盘按钮,我仍然需要将鼠标悬停在它们上面才能显示。这是为什么?

在 运行 时,注释掉创建文本字段的代码会使所有键盘按钮默认显示。但我不太确定是什么原因造成的。这只是 Swing 的问题吗?如果有帮助,我正在 Windows + IntelliJ 上执行此操作。虽然,我 运行 这个代码在 Mac 上,它实际上是同一个问题,除了我必须点击按钮才能显示。

这是 GUI 的完整代码:

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

public class GameGUI extends JFrame
{
    private final JPanel letters; // for textfields
    private final JPanel keyboard; // for buttons
    private final JTextField[][] tfs; // array for textfields
    private final JButton[] top, home, bottom; // buttons for rows
    private final String[] topRow = {"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"};
    private final String[] homeRow = {"A", "S", "D", "F", "G", "H", "J", "K", "L"};
    private final String[] bottomRow = {"Enter", "Z", "X", "C", "V", "B", "N", "M", "Backspace"};

    public GameGUI()
    {
        setSize(200, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        letters = new JPanel();
        letters.setLayout(new GridLayout(6, 5));

        // 30 textfields
        tfs = new JTextField[6][5];
        JPanel letterPlaceholder = new JPanel(new GridLayout(6, 5));
        for (int r = 0; r < tfs.length; r++)
        {
            for (int c = 0; c < tfs[r].length; c++)
            {
                tfs[r][c] = new JTextField();
                tfs[r][c].setActionCommand(r + ":" + c);
                letterPlaceholder.add(tfs[r][c]);
            }
        }
        letters.add(letterPlaceholder);

        keyboard = new JPanel();
        keyboard.setLayout(new GridLayout(3, 1));

        // top row
        top = new JButton[topRow.length];
        JPanel keys = new JPanel(new GridLayout(1, topRow.length));
        for (int i = 0; i < topRow.length; i++)
        {
            JButton topsize = new JButton(topRow[i]);
            top[i] = topsize;
            keys.add(top[i]);
        }
        keyboard.add(keys);

        // home row
        home = new JButton[homeRow.length];
        keys = new JPanel(new GridLayout(1, homeRow.length));
        for (int i = 0; i < homeRow.length; i++)
        {
            JButton homesize = new JButton(homeRow[i]);
            home[i] = homesize;
            keys.add(home[i]);
        }
        keyboard.add(keys);

        // bottom row
        bottom = new JButton[bottomRow.length];
        keys = new JPanel(new GridLayout(1, bottomRow.length));
        for (int i = 0; i < bottomRow.length; i++)
        {
            JButton bottomsize = new JButton(bottomRow[i]);
            bottom[i] = bottomsize;
            keys.add(bottom[i]);
        }
        keyboard.add(keys);

        this.getContentPane().add(letters, BorderLayout.NORTH);
        getContentPane().add(keyboard, BorderLayout.SOUTH);
        setVisible(true);
    }

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

似乎是一个快速修复。

已将 letters.setLayout(new GridLayout(6, 5)); 更改为 letters.setLayout(new GridLayout());

编辑:

另一种(更好的)方法:

-删除了 letters 面板

-添加:

letterPlaceholder = new JPanel();
letterPlaceholder.setLayout(new GridLayout(0, 5));

-将 getContentPane().add(letters, BorderLayout.NORTH); 更改为 getContentPane().add(letterPlaceholder, BorderLayout.NORTH);