首页 > 解决方案 > JLabel not showing up

问题描述

For some reason my JLabel isn't displaying and I can't figure out why (editString DOES have a value).

compFrame.removeAll();
JPanel editPane = new JPanel();
editPane.setLayout(new GridLayout(0,1));
compFrame.add(editPane);
//JLabel lastValue = new JLabel(editString);
editPane.add(new JLabel(editString));

compFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
compFrame.setVisible(true);

标签: javaswingawtjlabel

解决方案


您应该在 ContentPane 上使用 removeAll。尝试这个

compFrame.getContentPane().removeAll();

removeAll 的 API 文档说:

此方法更改与布局相关的信息,因此使组件层次结构无效。如果容器已显示,则必须随后验证层次结构以反映更改。


推荐阅读