实时刷新frame内容 [英] refresh the contents of frame in real time

查看:59
本文介绍了实时刷新frame内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
什么时候需要在 Swing 组件上调用 revalidate() 以使其刷新,什么时候不需要?

我正在用 Java 制作一个资源管理器程序,它的工作原理如下
我从用户输入路径,在用户按下输入路径后设置一个文本框,并计算文件夹列表并创建相应的标签,这些标签应该显示在窗口上
但应用程序没有't 显示文件夹的新内容,如果我更改文本,那么它会定向到一个新目录,所以当我按 Enter 时,它必须显示加载的新目录的内容,但只有在调整窗口大小后,它才会显示新目录的内容框架

i am making a Explorer program in Java it works as follows
i input the path from the user, a textbox after the user presses enter the path is set and the list is computed of the folder and corresponding labels are created which are supposed to be displayed on the window
but the application doesn't display the new contents of the folder, if i change the text then it directs to a new directory so when i press enter then it must show the contents of the new directory loaded but only after resizing the window does it show the new contents of the frame

代码如下

package explorer;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Explorer extends JFrame{
    File file;
    Scanner scan;
    String path;
    String [] listOfFiles;
    JTextField txtPath;
    JLabel lblLocation;
    JLabel child[];
    JPanel childPanel;
    JPanel masterPanel;

       public Explorer(){
      lblLocation = new JLabel("Location: ");
      /*
       * the declaration of panels
       */
          masterPanel = new JPanel();
          childPanel = new JPanel();
      JPanel panel = new JPanel();

      /*declaration of other components*/

      txtPath = new JTextField("",20);
      /*addition of components to panel for layout*/
      panel.add(lblLocation);
      panel.add(txtPath);
      /*adding to master panel, for sophisticated layout*/
      masterPanel.add(panel, BorderLayout.NORTH);
      masterPanel.add(childPanel, BorderLayout.SOUTH);  

      getContentPane().add(masterPanel);
      /*this place from where address is fetched like /home/revolution/Desktop etc on ubuntu*/

      txtPath.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ev){
            childPanel.removeAll();
        path = new String(txtPath.getText());//the absolute path
        file = new File(path);
        File childFiles[];
        String name = path.substring(path.lastIndexOf('/')+1, path.length());//the name of the directory being displayed
        setTitle(name);

          if(!file.isDirectory()){
            JOptionPane.showMessageDialog(null, "Error file is not a directory");
          } else {
            listOfFiles = file.list();
            child = new JLabel[listOfFiles.length];// labels equal to the number fo files and with name of the coresponding file/folder

            childFiles = new File[listOfFiles.length];//references to files
            childPanel.setLayout(new GridLayout(listOfFiles.length/2,listOfFiles.length/2));//setting grid layout

            for(int i=0; i<listOfFiles.length;i++){
                childFiles[i] = new File(listOfFiles[i]);
                child[i] = new JLabel(listOfFiles[i]);
                child[i].setToolTipText(childFiles[i].isFile()?"File":"Folder");
                childPanel.add(child[i]);
            }
          childPanel.setVisible(true);  
          }

        }
      });
}   
}

怎么了?我怎样才能刷新"窗口的内容??

what is wrong? how can i 'refresh' the contents of the window??

推荐答案

我认为你需要 revalidate() 面板.

I think you need to revalidate() the panel.

更准确地说,您可以在动作侦听器的末尾添加 childPanel.revalidate();

More precisely at the end of your action listener you can add childPanel.revalidate();

      childPanel.setVisible(true);  
      }
    }
    childPanel.revalidate();
});

这篇关于实时刷新frame内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆