java资源管理器_Java中以资源管理器浏览方式选择文件打开

运行界面如下:

a4c26d1e5885305701be709a3d33442f.png

点击确定按钮后显示如下界面:

a4c26d1e5885305701be709a3d33442f.png

程序代码如下:

import java.awt.Button;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileFilter;

import javax.swing.JFileChooser;

import javax.swing.JTree;

import javax.swing.event.TreeExpansionEvent;

import javax.swing.event.TreeExpansionListener;

import javax.swing.event.TreeSelectionEvent;

import javax.swing.event.TreeSelectionListener;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.DefaultTreeModel;

import javax.swing.tree.TreePath;

public class MyTree extends Frame implements

ActionListener

{ Button ok,cancel;

public MyTree()

{

//super("随机抽取");

this.setBounds(280,120,200,240);

this.setBackground(Color.lightGray);

this.setLayout(new FlowLayout(1));

ok=new Button("确定");

this.add(ok);

ok.addActionListener(this);

//在命令按钮上注册被实现的监听 cancel= new

Button("取消"); this.add(cancel);

cancel.addActionListener(this); //在命令按钮上注册被实现的监听

this.setVisible(true);

}

public void actionPerformed(ActionEvent

t) //实现按钮ActionListener监听器的唯一抽象方法,定义按钮单击时的行为

{ if(t.getSource()==ok)

//区分在哪个按钮上发生的ActionEvent事件

{ //初始化文件选择框

JFileChooser

fDialog = new JFileChooser();

//设置文件选择框的标题

fDialog.setDialogTitle("请选择所要打开的文件");

//弹出选择框

int returnVal

= fDialog.showOpenDialog(null);

//

如果是选择了文件

if(JFileChooser.APPROVE_OPTION

== returnVal){

//打印出文件的路径,你可以修改位 把路径值 写到 textField 中

System.out.println(fDialog.getSelectedFile());

}

}

else System.exit(0);

}

FileFilter filterDirectory = new FileFilter()

{

public boolean accept(File

pathname) {

return

pathname.isDirectory();

}

};

FileFilter filterFile = new

FileFilter() {

public boolean accept(File

pathname) {

return

pathname.isFile();

}

};

public static void main(String

args[]) {

new

MyTree();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值