Java 如果不调整窗口大小,则不会显示内容

Java 如果不调整窗口大小,则不会显示内容,java,swing,user-interface,awt,Java,Swing,User Interface,Awt,我制作了一个小应用程序来打开一个文件,并在一些JTextField和jlabel中显示打开的文件的内容。我可以获取所有内容并填写文本字段和标签,但问题是这些jlabel和JTextFields直到我重新调整窗口大小时才会显示。我希望我的内容立即显示出来。我需要为此做些什么 WL 下面是一段初始化面板并将其添加到scrollpane的代码 panel = new JPanel(); scrollPane = new JScrollPane(panel); 在打开

我制作了一个小应用程序来打开一个文件,并在一些JTextField和jlabel中显示打开的文件的内容。我可以获取所有内容并填写文本字段和标签,但问题是这些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);

根据您添加组件的方式和使用的布局,调用layout.pack可以解决您的问题。也许添加一个代码片段也可以简化帮助您的工作:D.

根据您添加组件的方式和使用的布局,调用layout.pack可以解决您的问题。也许添加一个代码片段也可以帮助您:D.

在添加所有组件调用之后

revalidate();
repaint();

添加所有组件调用后容器的

revalidate();
repaint();
如果没有一个好的容器,我假设您一定忘了将负责创建和显示代码的代码放在事件调度程序线程中

例如:

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.
                */
           }
       });
}
在更改组件后,请使用验证和重新绘制方法。 希望这能在某种程度上有所帮助

关于

如果没有好的,我假设您一定忘了将负责创建和显示代码的代码放在事件调度程序线程中

例如:

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.
                */
           }
       });
}
在更改组件后,请使用验证和重新绘制方法。 希望这能在某种程度上有所帮助

关于

您应该调用JFrame的.repait方法

您应该调用JFrame的.repait方法

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.
有些问题可能是

将VisibleTrue设置为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();
            }
        });
    }
}
二,

应该有第二个区域,您的GUI不显示任何JComponent或没有值

成功地从文件结尾检查是流,将FIleStream包装为try-catch-finally块,添加到catch块printStackTrace以显示异常

在这种形式下,您可以在主线程和所有等待流结束的GUI期间读取数据

三,

在JTextFIelds中创建没有任何值的空GUI

显示这个图形用户界面

开始

将FileStream重定向到RunnableThread或SwingWorker

在FileStream上隐藏启动屏幕已结束

向JTextFields添加值

一,

有些问题可能是

将VisibleTrue设置为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();
            }
        });
    }
}
二,

应该有第二个区域,您的GUI不显示任何JComponent或没有值

成功地从文件结尾检查是流,将FIleStream包装为try-catch-finally块,添加到catch块printStackTrace以显示异常

在这种形式下,您可以在主线程和所有等待流结束的GUI期间读取数据

三,

在JTextFIelds中创建没有任何值的空GUI

显示这个图形用户界面

开始

将FileStream重定向到RunnableThread或SwingWorker

在FileStream上隐藏启动屏幕已结束

向JTextFields添加值


请寄密码。如果有任何混淆,发布的代码片段不是SSCCE。请发布代码。如果有任何混淆,发布的代码段不是SSCCE。我不认为调用repaint是最好的做法,因为GUI线程无论如何都会调用paint方法。如果布局被正确使用,并且执行更新的代码被正确调用,那么布局应该正确绘制。但是在没有代码片段的情况下……我认为调用repaint不是最好的方法,因为GUI线程无论如何都会调用paint方法。如果布局被正确使用,并且执行更新的代码被正确调用,那么布局应该正确绘制。但由于缺少一个片段…谢谢Gapandeep,我错过了验证和重新编写。现在一切都好了:@waqas:很高兴你通过了障碍物。:-关于Hanks Gapandeep,我错过了验证和重新安装。现在一切都好了:@waqas:很高兴你通过了障碍物。:-向+1致意,以获得一些精彩的技巧。可能会很快帮到我:-问候+1一些精彩的工作提示。可能很快就会帮到我的