JFileChooser 里面的撤销按钮怎样加点击事件?

ddtqfly 2004-07-07 10:58:12
各位老大帮帮忙,找了半天也没有发现给JFileChooser 里面的撤销按钮加事件的地方,还有就是那个JTextField里面怎么设置文件名啊?
...全文
456 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
interhanchi 2005-08-19
  • 打赏
  • 举报
回复
public class FileAccessApplet extends JApplet {
private JTextField
filename = new JTextField(),
dir = new JTextField();
private JButton
open = new JButton("Open"),
save = new JButton("Save");
private JEditorPane ep = new JEditorPane();
private JScrollPane jsp = new JScrollPane();
private File file;
class OpenL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
c.setFileFilter(new TextFileFilter());
// Demonstrate "Open" dialog:
int rVal = c.showOpenDialog(FileAccessApplet.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
file = c.getSelectedFile();
filename.setText(file.getName());
dir.setText(c.getCurrentDirectory().toString());
try {
System.out.println("Url is " + file.toURL());
ep.setPage(file.toURL());
// ep.repaint();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
if(rVal == JFileChooser.CANCEL_OPTION) {
filename.setText("You pressed cancel");
dir.setText("");
} else {
save.setEnabled(true);
}
}
}
class SaveL implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser(file);
c.setSelectedFile(file);
// Demonstrate "Save" dialog:
int rVal = c.showSaveDialog(FileAccessApplet.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
filename.setText(c.getSelectedFile().getName());
dir.setText(c.getCurrentDirectory().toString());
try {
FileWriter fw = new FileWriter(file);
ep.write(fw);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
if(rVal == JFileChooser.CANCEL_OPTION) {
filename.setText("You pressed cancel");
dir.setText("");
}
}
}
}
wang_duncan 2005-08-19
  • 打赏
  • 举报
回复
int n = fc.showOpenDialog(this);
if (n == JFileChooser.APPROVE_OPTION) {}
else if (n == .....
我运行类似的代码,然后出现的对话框中,我选中一个txt文档,按“打开”,然后不停的出现这个对话框,根本打不开那个txt文档,为啥呢?
ylssww 2005-03-13
  • 打赏
  • 举报
回复
if (fileChooser.showOpenDialog(null) == JFileChooser.CANCEL_OPTION)// 这就构成一个Cancel事件啦
{

}
ProgramNoLove 2004-07-07
  • 打赏
  • 举报
回复
帮你顶~~~~~~~~~~~~
------------------------------------
体验速度,体验CSDN新版论坛助手:http://community.csdn.net/Expert/TopicView.asp?id=3108679
songlean 2004-07-07
  • 打赏
  • 举报
回复
JFileChooser jf = new JFileChooser();
int returnVal = jf.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
//添加点击打开按钮后的事件
} else {
//添加点击撤销按钮后的事件
}
JTextField里的文件名不是会自动根据你所选的文件设上去的吗?不需要你自己设啊
Acylas 2004-07-07
  • 打赏
  • 举报
回复
你干嘛要要给按钮加事件?
你直接给jFileChooser加就行了。
或者用int n = fc.showOpenDialog(this);
if (n == JFileChooser.APPROVE_OPTION) {}
else if (n == .....
做处理
jFileChooser.setSelectedFile可以设置textfield里面的文件名。

62,614

社区成员

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

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