繁体   English   中英

不调整窗口大小就不会显示内容

[英]Contents are not shown without resizing the windows

我制作了一个小应用程序来打开文件,并在某些JTextField和JLabel中显示已打开文件的内容。 我可以获取所有内容并填写TextFields和Labels,但是问题是这些JLabel和JTextFields直到我调整大小(甚至略微调整)窗口时才会显示。 我希望立即显示我的内容。 我需要为此做什么。

WL

这是一段代码,用于初始化面板并将其添加到滚动窗格

        panel = new JPanel();

        scrollPane = new JScrollPane(panel);

在打开按钮的动作侦听器中,我得到了以下代码

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

的容器

在缺少良好的SSCCE的情况下,我假设您必须忘记将负责创建和显示代码的代码放在Event Dispatcher线程中。

例如 :

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()方法。 希望这会有所帮助。

问候

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构造函数中的最后一行,检查并删除重复项

  • 主方法必须包装到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以显示Exception

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

3)

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

  • 显示此GUI

  • 启动启动画面

  • 将FileStream重定向到Runnable#Thread或SwingWorker

  • 隐藏FileStream上的启动画面已结束

  • 将值添加到JTextFields

根据您添加组件的方式以及所使用的布局,对layout.pack()的调用可以解决您的问题。 也许添加代码段也可以简化您的工作:D。

您应该调用JFrame的.repaint()方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM