我做了一个小应用程序来打开一个文件并在一些 JTextField 和 JLabel 中显示打开文件的内容。我可以获取所有内容并填写 TextFields 和 Labels,但问题是这些 JLabels 和 JTextFields 直到我调整窗口大小(甚至一点点)后才会显示。我希望立即显示我的内容。我需要为此做什么。
工作人员
这里有一段代码来初始化面板并将其添加到滚动面板
panel = new JPanel();
scrollPane = new JScrollPane(panel);
在打开按钮的actionlistener中我得到了下面的代码
int returnVal = fc.showOpenDialog(FileReader.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String filePath = file.getPath();
// if(panel.isDisplayable()==true)panel.
if(scrollPane != null){
//panel.removeAll();
this.remove(scrollPane);
// scrollPane.add(panel);
//panel.add(panel);
//panel.validate();
//panel.repaint();
}
//pass the file to XMLparser
xmlParsing = new XMLParsing(filePath);
panel = new JPanel();
panel=fill();
panel.revalidate();
panel.repaint();
scrollPane = new JScrollPane(panel);
add(scrollPane, BorderLayout.CENTER);
//add(buttonPanel, BorderLayout.SOUTH);
saveButton.setEnabled(true);
saveasButton.setEnabled(true);
最佳答案
添加所有组件后调用
revalidate();
repaint();
容器
关于java - 不调整窗口大小不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9016580/