文件选择和文件夹选择

4 篇文章 0 订阅

import org.eclipse.swt.SWT;


public class Fileopen {

protected Shell shell;

/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
Fileopen window = new Fileopen();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}

/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");

//文件选择按钮
Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
//新建文件对话框,并设置为打开的方式
FileDialog filedlg=new FileDialog(shell,SWT.OPEN);
filedlg.setText("文件选择");
//设置初始路径
filedlg.setFilterPath("SystemRoot");
//打开文件对话框,返回选中文件的绝对路径
String selected=filedlg.open();


System.out.println("您选中的文件路径为:"+selected);
}
});
button.setBounds(174, 92, 72, 22);
button.setText("\u6587\u4EF6\u9009\u62E9");

Button button_1 = new Button(shell, SWT.NONE);
//文件夹选择按钮
button_1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
//新建文件夹(目录)对话框
DirectoryDialog folderdlg=new DirectoryDialog(shell);
//设置文件对话框的标题
folderdlg.setText("文件选择");
//设置初始路径
folderdlg.setFilterPath("SystemDrive");
//设置对话框提示文本信息
folderdlg.setMessage("请选择相应的文件夹");
//打开文件对话框,返回选中文件夹目录
String selecteddir=folderdlg.open();
if(selecteddir==null){
return ;
}
else{
System.out.println("您选中的文件夹目录为:"+selecteddir);
}

}
});
button_1.setBounds(148, 163, 72, 22);
button_1.setText("\u6587\u4EF6\u5939\u9009\u62E9");

Button button_2 = new Button(shell, SWT.NONE);
button_2.addSelectionListener(new SelectionAdapter() {
//保存文件
@Override
public void widgetSelected(SelectionEvent e) {
//新建文件对话框,并设置为打开的方式
FileDialog filedlg=new FileDialog(shell,SWT.SAVE);
filedlg.setText("文件选择");
//设置初始路径
filedlg.setFilterPath("C:\\");
//设置文件为。bak的文件
filedlg.setFilterExtensions(new String[]{".bak"});
//打开对话框,并且得到文件的路径
String filename=filedlg.open();
System.out.println(filename);
}
});
button_2.setBounds(48, 33, 80, 27);
button_2.setText("\u4FDD\u5B58\u6587\u4EF6");
//设置文件对话框的标题


}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值