JLabel 没有出现

JLabel not showing up

出于某种原因,我的 JLabel 没有显示,我也不知道为什么 (editString 确实有一个值)。

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);

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

compFrame.getContentPane().removeAll();

removeAll 的 API 文档说:

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