读书人

【40分】帮小弟我修改下JAVA程序。拜托

发布时间: 2012-03-14 12:01:12 作者: rapoo

【40分】帮我修改下JAVA程序。。。拜托
我想做个文件管理器,在输入框里输入路径就在下面的表格里显示该文件夹的文件,以及信息。但是显示不了 啊,谁能帮我看看,40分拜托下。。。

Java code
package cn.mrpmh.bao;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.*;import java.text.SimpleDateFormat;import java.util.Date;//文件管理public class jtable {    JTable table;    String[] columnName;    JLabel xinxi;    File[] fileslist;    int photo=0,audio=0,video=0,txt=0;       JTextField lujin;   String lujinvalue="C:\\Users\\Administrator\\Pictures";   JButton go;   JButton open;   JFrame win;   JScrollPane scroll;    public void window(String title)    {        win=new JFrame(title);        win.setIconImage(Toolkit.getDefaultToolkit().createImage("file/image/icon.png"));        tool();        init();        win.setSize(600,400);        win.setLocation(300,100);        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        win.setVisible(true);    }        private void tool()    {        JToolBar tools=new JToolBar();         lujin=new JTextField(lujinvalue,15);        go=new JButton("转到");        open=new JButton("打开");        tools.add(lujin);        tools.add(go);        tools.add(open);        go.addActionListener(new Shijian());        open.addActionListener(new Shijian());        win.getContentPane().add(tools,BorderLayout.NORTH);    }        private String[][] files()    {        File file=new File(lujinvalue);                fileslist=file.listFiles();  //获取文件数组        String[][] getfile=new String[fileslist.length][fileslist.length];  //定义字符串数组                for(int i=0;i<fileslist.length;i++)  //遍历数组        {                    if(fileslist[i].getName().endsWith("jpg")||fileslist[i].getName().endsWith("gif")||fileslist[i].getName().endsWith("png")||                       fileslist[i].getName().endsWith("bmp"))                    photo++;                    if(fileslist[i].getName().endsWith("mp3")||fileslist[i].getName().endsWith("wav")||fileslist[i].getName().endsWith("wma")||                        fileslist[i].getName().endsWith("mid"))                    audio++;                    if(fileslist[i].getName().endsWith("mp4")||fileslist[i].getName().endsWith("3gp")||fileslist[i].getName().endsWith("avi")||                    fileslist[i].getName().endsWith("flv")||fileslist[i].getName().endsWith("f4v")||fileslist[i].getName().endsWith("rm")||                    fileslist[i].getName().endsWith("rmvb")||fileslist[i].getName().endsWith("wmv"))                    video++;                    if(fileslist[i].getName().endsWith("txt"))                    txt++;            for(int j=0;j<4;j++)            {                                                getfile[i][0]=fileslist[i].getName();    //文件名                getfile[i][1]=fileslist[i].toString();   //路径                getfile[i][2]=Integer.toString(((int)fileslist[i].length()));  //文件大小                getfile[i][3]=(new   SimpleDateFormat( "yyyy-MM-dd    HH:mm:ss")).format(new  Date( fileslist[i].lastModified()));                            }        }        return getfile;    }    private void init()    {        columnName=new String[]{"文件名","路径","大小(B)","最后修改时间"};        table=new JTable(files(),columnName);            scroll=new JScrollPane(table);        win.getContentPane().add(scroll,BorderLayout.CENTER);        xinxi=new JLabel("本目录共【"+fileslist.length+"】个文件:     图片【"+photo+"】,音频【"+audio+"】,视频【"+video+"】,txt文本【"+txt+"】");        xinxi.setFont(new Font("",Font.PLAIN,12));        win.getContentPane().add(xinxi,BorderLayout.SOUTH);    }        public class Shijian implements ActionListener     {        public void actionPerformed(ActionEvent e)         {                                if(e.getSource()==go)                {                    File file=new File(lujin.getText());                    if(file.exists())                    {                        lujinvalue=lujin.getText();                        new Thread(new shuaxin()).start();                    }                    else                    JOptionPane.showMessageDialog(null,"路径不存在!","提示",0);                }                if(e.getSource()==open)                {                    FileDialog openfile=new FileDialog(new JFrame(),"打开",FileDialog.LOAD);                    openfile.setVisible(true);                    if(openfile.getFile()!=null)                    {                    lujinvalue=openfile.getDirectory();//+openfile.getFile();                    lujin.setText(lujinvalue);                    }                }        }    }        class shuaxin implements Runnable    {        public void run()        {                        table=new JTable(files(),columnName);                scroll=new JScrollPane(table);        }    }        public static void main(String[] args)    {        new jtable().window("文件管理器");    }} 



[解决办法]
你的程序初始能正确显示,但点‘转到’不会刷新列表,帮你改正了,并且修改了数组会越界的BUG,下列代码你测试可用。
Java code
package test;import java.awt.BorderLayout;import java.awt.FileDialog;import java.awt.Font;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.JToolBar;public class jtable {    private JTable table;    private String[] columnName;    private JLabel xinxi;    private File[] fileslist;    private int photo = 0;    private int audio = 0;    private int video = 0;    private int txt = 0;    private JTextField lujin;    private String lujinvalue = "C:\\Program Files\\Java\\jdk1.6.0_21\\bin";    private JButton go;    private JButton open;    private JFrame win;    private JScrollPane scroll;    public void window(String title) {    win = new JFrame(title);    win.setIconImage(Toolkit.getDefaultToolkit().createImage(        "file/image/icon.png"));    tool();    init();    win.setSize(600, 400);    win.setLocation(300, 100);    win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    win.setVisible(true);    }    private void tool() {    JToolBar tools = new JToolBar();    lujin = new JTextField(lujinvalue, 15);    go = new JButton("转到");    open = new JButton("打开");    tools.add(lujin);    tools.add(go);    tools.add(open);    go.addActionListener(new Shijian(this));    open.addActionListener(new Shijian(this));    win.getContentPane().add(tools, BorderLayout.NORTH);    }    private String[][] files() {    File file = new File(lujinvalue);    System.out.println(lujinvalue);    fileslist = file.listFiles(); // 获取文件数组    System.out.println(fileslist.length);    [color=#FF0000]String[][] getfile = new String[fileslist.length][4]; // 定义字符串数组,定义为4列[/color]    for (int i = 0; i < fileslist.length; i++) // 遍历数组    {        if (fileslist[i].getName().endsWith("jpg")            || fileslist[i].getName().endsWith("gif")            || fileslist[i].getName().endsWith("png")            || fileslist[i].getName().endsWith("bmp"))        photo++;        if (fileslist[i].getName().endsWith("mp3")            || fileslist[i].getName().endsWith("wav")            || fileslist[i].getName().endsWith("wma")            || fileslist[i].getName().endsWith("mid"))        audio++;        if (fileslist[i].getName().endsWith("mp4")            || fileslist[i].getName().endsWith("3gp")            || fileslist[i].getName().endsWith("avi")            || fileslist[i].getName().endsWith("flv")            || fileslist[i].getName().endsWith("f4v")            || fileslist[i].getName().endsWith("rm")            || fileslist[i].getName().endsWith("rmvb")            || fileslist[i].getName().endsWith("wmv"))        video++;        if (fileslist[i].getName().endsWith("txt"))        txt++;        for (int j = 0; j < 4; j++) {        getfile[i][0] = fileslist[i].getName(); // 文件名        getfile[i][1] = fileslist[i].toString(); // 路径        getfile[i][2] = Integer.toString(((int) fileslist[i].length())); // 文件大小        getfile[i][3] = (new SimpleDateFormat("yyyy-MM-dd    HH:mm:ss"))            .format(new Date(fileslist[i].lastModified()));        }    }    return getfile;    }    private void init() {    columnName = new String[] { "文件名", "路径", "大小(B)", "最后修改时间" };    table = new JTable(this.files(), columnName);    scroll = new JScrollPane(table);    win.getContentPane().add(scroll, BorderLayout.CENTER);    xinxi = new JLabel("本目录共【" + fileslist.length + "】个文件:     图片【" + photo        + "】,音频【" + audio + "】,视频【" + video + "】,txt文本【" + txt + "】");    xinxi.setFont(new Font("", Font.PLAIN, 12));    win.getContentPane().add(xinxi, BorderLayout.SOUTH);    }    public class Shijian implements ActionListener {    private jtable jtable;    Shijian(jtable jtable) {        this.jtable = jtable;    }    public void actionPerformed(ActionEvent e) {        if (e.getSource() == go) {        File file = new File(lujin.getText());        if (file.exists()) {            lujinvalue = lujin.getText();            new Thread(new shuaxin(jtable)).start();        } else            JOptionPane.showMessageDialog(null, "路径不存在!", "提示", 0);        }        if (e.getSource() == open) {        FileDialog openfile = new FileDialog(new JFrame(), "打开",            FileDialog.LOAD);        openfile.setVisible(true);        if (openfile.getFile() != null) {            lujinvalue = openfile.getDirectory();// +openfile.getFile();            lujin.setText(lujinvalue);        }        }    }    }    class shuaxin implements Runnable {    private jtable jtable;    shuaxin(jtable jtable) {        this.jtable = jtable;    }    public void run() {        jtable.window("文件管理器");    }    }    public static void main(String[] args) {    new jtable().window("文件管理器");    }} 

读书人网 >J2SE开发

热点推荐