簡體   English   中英

鍵盤按鈕僅在 cursor 懸停時顯示

[英]Keyboard buttons only shows when cursor hovers over

我有這個帶有文本字段的 GUI 和一個由 JButtons 組成的鍵盤:

在此處輸入圖像描述

但是當我第一次運行它時,它顯示如下:

在此處輸入圖像描述

鍵盤按鈕僅在我的鼠標懸停在其上時顯示:

在此處輸入圖像描述

調整 window 的大小也會再次隱藏所有鍵盤按鈕,我仍然需要 hover 才能顯示它們。 為什么是這樣?

注釋掉創建文本字段的代碼會使所有鍵盤按鈕在運行時默認顯示。 但我不太確定是什么原因造成的。 這只是 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM