目录不改变-java gui

时间:2018-03-14 18:01:21

标签: java

所以,我在这里尝试使用java gui模拟filemanager。 文本字段显示当前目录,我们也可以通过编辑并单击“go”按钮来更改目录。 我的问题是目录没有改变,即它没有显示它下面的任何变化。 在此先感谢。enter image description here

代码是

 import javax.swing.*;
import javax.swing.table.*;
import java.io.File;
import java.util.Date;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.EmptyBorder;

public class Try extends JFrame{
    JPanel centerp,topp,downp; 
    JTextField addr;
    JButton go;
    int h,w;
     public Try(){
      setLayout(new BorderLayout());
      centerp= new JPanel(new BorderLayout());
      topp=new JPanel(new BorderLayout());
      addr=new JTextField(50);
      go=new JButton("GO");

    File dir;
    dir = new File(System.getProperty("user.dir"));
    addr.setText(dir.getAbsolutePath());
    topp.add(addr,BorderLayout.WEST);

    go.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae) {
                    try {
                       String a = addr.getText();
                       System.setProperty("user.dir", a);

                    } catch(Exception t) {
                       System.out.println("error");
                    }

                }});
   topp.add(go);

    FileTableModl model = new FileTableModl(dir);

      JTable table = new JTable(model);
      centerp= new JPanel(new BorderLayout());
       centerp.add(new JScrollPane(table) );
       add(topp,BorderLayout.NORTH);
       add(centerp,BorderLayout.CENTER);
  }
}

class FileTableModl extends AbstractTableModel {
  protected File dir;
  protected String[] filenames;

  protected String[] columnNames = new String[] {
    "name", "size", "last modified"  };

  protected Class[] columnClasses = new Class[] { 
    String.class, Long.class, Date.class  };

    public FileTableModl(File dir) { 
    this.dir = dir; 
    this.filenames = dir.list();  
  }


  public int getColumnCount() { return 3; } 
  public int getRowCount() { return filenames.length; }  


  public String getColumnName(int col) { return columnNames[col]; }
 public Class getColumnClass(int col) { return columnClasses[col]; }

   public Object getValueAt(int row, int col) {
    File f = new File(dir, filenames[row]);
    switch(col) {
    case 0: return filenames[row];
    case 1: return new Long(f.length());
    case 2: return new Date(f.lastModified());
    default: return null;
    }

  }
  public static void main(String[] args)
  {
      Try gui=new Try();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       gui.pack();
        gui.setVisible(true);
    gui.setTitle("Try");
        }
}

1 个答案:

答案 0 :(得分:0)

调用System.setProperty("user.dir", a);对您的用户界面没有任何影响。您需要告诉UI某些内容已发生变化。也许是这样的:

go.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae)
    {
        try {
            File candidate = new File(addr.getText());
            if (candidate.exists() && candidate.isDirectory()) {
                table.setModel(new FileTableModl(candidate));
            }
        } catch (Exception t) {
            t.printStackTrace(System.err);
        }
    }
});

为此,您需要tableJTable)成员字段,而不是Try构造函数的本地变量。