在写框体程序的时候,发现网上没有找到用java实现展示文件目录并打开文件目录并打开文件的操作,对于新手来说这是一个不调简单的操作,所以在这就把这部分的代码贴出来,供我这样的小白学习一下。
JBUtton jb2=new JButton();
JFileChooser chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//show file chooser dialog
int result =chooser.showOpenDialog(null);
//if file selected,set it as icon of the label
if(result==JFileChooser.APPROVE_OPTION)
{
String name=chooser.getSelectedFile().getPath();
System.out.println(name);
File file = new File(name);
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
这部分的代码可以直接粘贴使用,希望能对新手有所帮助。