JPanel只是白色/灰色

问题描述 投票:1回答:1

编辑2:如果我尝试将JLabel添加到您在第二张图片中看到的复选框所在的区域,我也会得到一个白色/灰色区域。

编辑:另请注意:当我刚创建一个JLabel并在将totalResultArea添加到panelResults之前添加它时,它也只显示一个白色/灰色区域。

我创建了两个JPanels panelResultstotalResultArea,但只看到第二个面板的白色/灰色区域(totalResultArea)。

首先,我将panelResults的布局设置为GridBagLayout

JPanel panelResults = new JPanel();
JPanel totalResultArea = new JPanel();
panelResults.setLayout(new GridBagLayout());

然后我将totalResultArea添加到panelResults

GridBagContraints  c = new GridBagConstraints();
totalResultArea.setLayout(new BoxLayout(totalResultArea, BoxLayout.Y_AXIS));
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
c.weightx = 2;
panelResults.add(totalResultArea, c);

在按钮的单击事件监听器中,我将JLabel添加到totalResultArea并将JFrame设置为可见:

JLabel totalResultsText = new JLabel("<html><body>...</body></html>");
totalResultArea.add(totalResultsText);

// revalidate and repaint totalResultArea and panelResults
totalResultArea.revalidate();
totalResultArea.repaint();
panelResults.revalidate();
panelResults.repaint();

// add panelResults to frame
frame.getContentPane().add(panelResults, BorderLayout.NORTH);

// invalidate, repaint the frame and make it visible 
frame.invalidate();
frame.repaint();
frame.setVisible(true);

这一切看起来像这样:

white/grey JPanel

enter image description here

我希望你能重现这一点。如果看不到图片。

可能是什么原因,我没有看到JLabel的文字,只有白色/灰色的区域?

java swing jpanel
1个回答
1
投票

标签的背景和字体颜色都是白色,因此您无法看到文本。

当您使用其他颜色进行绘画并填充标签时,您将看到有文本。

Image to see that there is text

所以检查你的风格和/或添加totalResultsText.setForeground(Color.BLACK);

© www.soinside.com 2019 - 2024. All rights reserved.