【问题标题】:refresh the contents of frame in real time [duplicate]实时刷新帧的内容[重复]
【发布时间】:2012-05-28 06:55:15
【问题描述】:

可能重复:
When does one need to call revalidate() on a swing component to make it refresh, and when not?

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

代码如下

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

        }
      });
}   
}

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

【问题讨论】:

  • 使用 revalidate() 方法根据新的组件列表进行重置。
  • revalidate() 方法有什么作用?我还没有在任何书中遇到它.....
  • @lucifer 看看this
  • @NitinChhajer 发布的方法的更多信息

标签: java swing


【解决方案1】:

我认为您需要revalidate() 面板。

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

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

【讨论】:

  • 由于他要删除组件,他还必须在包含 JPanel 上调用 repaint()
  • 但由于他只是更改子面板,子面板上的revalidate 应该处理所有事情,因为整个子面板被重绘
  • 不,如果他也在移除组件,则不会——他就是这样。注意第一次调用actionPerformed(...)方法——childPanel.removeAll();
猜你喜欢
  • 1970-01-01
  • 2012-02-17
  • 2017-09-02
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多