我做了一个小应用程序来打开一个文件,并在一些JTextField和JLabel中显示打开文件的内容。我可以获取所有内容并填写TextFields和Labels,但问题是这些JLabel和JTextFields不会显示,除非我调整窗口大小(甚至一点点)。我希望我的内容能够立即显示出来。我需要做些什么。
WL
这是一段初始化面板并将其添加到scrollpane
的代码 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);
答案 0 :(得分:2)
添加所有组件后调用
revalidate();
repaint();
容器的
答案 1 :(得分:2)
在没有好SSCCE的情况下,我假设您必须忘记在Event Dispatcher Thread中放置负责创建和显示代码的代码。
例如:
public static void main(String... args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
/* Put the method or code, which is responsible for
* creating and displaying your GUI, here.
* That can be the issue too, what you are facing.
*/
}
});
}
完成更改组件后,请使用validate()和repaint()方法。 希望这可能会有所帮助。
此致
答案 2 :(得分:2)
1)
but the problem is these JLabels and JTextFields are not shown untill
and unless I resize(even a little) the windows. I want my contents to
be shown straight away.
一些问题可能是
setVisible(true)作为Swing GUI构造函数中的最后一行,检查并删除重复项
main方法必须包含在invokeLater
例如,
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class BorderPanels extends JFrame {
private static final long serialVersionUID = 1L;
public BorderPanels() {
setLayout(new GridBagLayout());// set LayoutManager
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel1 = new JPanel();
Border eBorder = BorderFactory.createEtchedBorder();
panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
gbc.gridx = gbc.gridy = 0;
gbc.gridwidth = gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = gbc.weighty = 70;
add(panel1, gbc); // add compoenet to the COntentPane
JPanel panel2 = new JPanel();
panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
gbc.gridy = 1;
gbc.weightx = 30;
gbc.weighty = 30;
gbc.insets = new Insets(2, 2, 2, 2);
add(panel2, gbc); // add component to the COntentPane
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // important
pack();
setVisible(true); // important
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() { // important
public void run() {
BorderPanels borderPanels = new BorderPanels();
}
});
}
}
2)
small application to open a file and show the contents of opened file
in some JTextField and JLabels
应该有第二个区域,您的GUI不显示任何JComponent或没有值
检查是从文件结束流成功,包装FIleStream尝试 - catch - finally块,添加到catch块printStackTrace以显示异常
以这种形式在主线程和所有等待Stream结束的GUI中读取数据
3)
在JTextFIelds
diplay this GUI
将FileStream重定向到Runnable #Thread或SwingWorker
隐藏FileStream上的启动画面已结束
将值添加到JTextFields
答案 3 :(得分:0)
根据您添加组件的方式以及您使用的布局,对layout.pack()的调用可以解决您的问题。也许添加代码片段也可以帮助您轻松完成工作:D。
答案 4 :(得分:0)
你应该调用JFrame的.repaint()
方法