跪求:java 如何在button点击后弹出目录路径选择对话框

elvisleung113 2011-08-19 09:41:10
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setText("BROWSE");
//static JTextField TextField;
//static interf testFrame;
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
//static JTextField TextField;
//static interf testFrame;
JFileChooser chooser = new JFileChooser("./");
FileNameExtensionFilter filter = new FileNameExtensionFilter("");
chooser.setFileFilter(filter);
testFrame = new interf();
TextField = new JTextField("");
int returnVal = chooser.showOpenDialog(testFrame);
if(returnVal == JFileChooser.APPROVE_OPTION){
TextField.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
}
);
Container contentPane = testFrame.getContentPane();
contentPane.setLayout(new FlowLayout());

}
return jButton2;
}
以上是我自己写的,那位高手帮忙看看了,运行不了。
...全文
746 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoxiong5227 2011-08-19
  • 打赏
  • 举报
回复
JFileChooser jfc = new JFileChooser();
//System.out.println("choose done");
jfc.setFileSelectionMode(0);//设定只能选择到文件
int state = jfc.showOpenDialog(null);//此句是打开文件选择器界面的触发语句
if (state == 1) {
return;//撤销则返回
} else {
File f = jfc.getSelectedFile();//f为选择到的文件
jTextField1.setText(f.getAbsolutePath());
}
24K純帥 2011-08-19
  • 打赏
  • 举报
回复
void chooseButtonDirectory_actionPerformed(ActionEvent e) {
try {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(fileChooser.DIRECTORIES_ONLY);
System.out.println( "here is directories ");

int n = fileChooser.showOpenDialog(this.getContentPane());
if (n == fileChooser.APPROVE_OPTION)
{

textFieldFileLocalDirectory.setText(fileChooser.getSelectedFile().getPath());

}
} catch (Exception ex) {
ex.printStackTrace();
}

}
softroad 2011-08-19
  • 打赏
  • 举报
回复
JFileChooser.DIRECTORIES_ONLY
指示仅显示目录。
茫茫大海 2011-08-19
  • 打赏
  • 举报
回复

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;


public class MainFrame extends JFrame implements ActionListener {

private JButton button;
private JFileChooser fileChooser;

public MainFrame() {
button = new JButton("浏览");
button.addActionListener(this);
fileChooser = new JFileChooser("./");

this.add(button,BorderLayout.NORTH);

this.setSize(800, 600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}



public void actionPerformed(ActionEvent e) {
fileChooser.showOpenDialog(this);
}

public static void main(String[] args) {
new MainFrame();
}

}
softroad 2011-08-19
  • 打赏
  • 举报
回复
JFileChooser.setFileSelectionMode
softroad 2011-08-19
  • 打赏
  • 举报
回复
JFileChooser.DIALOG_TYPE_CHANGED_PROPERTY
softroad 2011-08-19
  • 打赏
  • 举报
回复

JFileChooser chooser = new JFileChooser();
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
// under the demo/jfc directory in the JDK.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}

茫茫大海 2011-08-19
  • 打赏
  • 举报
回复
这点代码怎么看啊???

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧