简体   繁体   English

键盘按钮仅在 cursor 悬停时显示

[英]Keyboard buttons only shows when cursor hovers over

I have this GUI with textfields and a keyboard made of JButtons:我有这个带有文本字段的 GUI 和一个由 JButtons 组成的键盘:

在此处输入图像描述

But when I run it at first, it shows like this:但是当我第一次运行它时,它显示如下:

在此处输入图像描述

And the buttons for the keyboard only show when my mouse hovers over it:键盘按钮仅在我的鼠标悬停在其上时显示:

在此处输入图像描述

Resizing the window also hides all the keyboard buttons again, and I'd still have to hover over them for it to show.调整 window 的大小也会再次隐藏所有键盘按钮,我仍然需要 hover 才能显示它们。 Why is this?为什么是这样?

Commenting out the code to create the textfields makes all the keyboard buttons appear on default when ran.注释掉创建文本字段的代码会使所有键盘按钮在运行时默认显示。 But I'm not too sure what causes this.但我不太确定是什么原因造成的。 Is it just an issue with Swing?这只是 Swing 的问题吗? If it helps, I am doing this on Windows + IntelliJ.如果有帮助,我正在 Windows + IntelliJ 上执行此操作。 Although, I ran this code on a Mac and it's practically the same issue, except I'd have to click the buttons for it to show.虽然,我在 Mac 上运行了这段代码,但它实际上是同一个问题,只是我必须单击按钮才能显示。

Here is the entire code for the GUI:这是 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();
    }
}

Seemed like a quick fix.似乎是一个快速修复。

Changed letters.setLayout(new GridLayout(6, 5));更改了letters.setLayout(new GridLayout(6, 5)); to letters.setLayout(new GridLayout());letters.setLayout(new GridLayout()); . .

Edit:编辑:

Another (and better) way of doing it:另一种(更好的)方法:

-removed letters panel - 删除letters面板

-added: -添加:

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

-changed getContentPane().add(letters, BorderLayout.NORTH); -更改了getContentPane().add(letters, BorderLayout.NORTH); to getContentPane().add(letterPlaceholder, BorderLayout.NORTH); getContentPane().add(letterPlaceholder, BorderLayout.NORTH);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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