2009年6月18日
来源:互联网 发布:htc windows phone 8x 编辑:程序博客网 时间:2024/05/17 06:22
FileChooser的使用:
http://czwangelo.javaeye.com/blog/92144(转载)
package org.song.test;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SimpleFileChooser extends JFrame {
public SimpleFileChooser( ) {
super("File Chooser Test Frame");
setSize(350, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane( );
c.setLayout(new FlowLayout( ));
JButton openButton = new JButton("Open");
JButton saveButton = new JButton("Save");
JButton dirButton = new JButton("Pick Dir");
final JLabel statusbar =
new JLabel("Output of your selection will go here");
// Create a file chooser that opens up as an Open dialog.
openButton.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser( );
chooser.setMultiSelectionEnabled(true);
int option = chooser.showOpenDialog(SimpleFileChooser.this);
if (option == JFileChooser.APPROVE_OPTION) {
File[] sf = chooser.getSelectedFiles( );
String filelist = "nothing";
if (sf.length > 0) filelist = sf[0].getName( );
for (int i = 1; i < sf.length; i++) {
filelist += ", " + sf[i].getName( );
}
statusbar.setText("You chose " + filelist);
}
else {
statusbar.setText("You canceled.");
}
}
});
// Create a file chooser that opens up as a Save dialog.
saveButton.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser( );
int option = chooser.showSaveDialog(SimpleFileChooser.this);
if (option == JFileChooser.APPROVE_OPTION) {
statusbar.setText("You saved " + ((chooser.getSelectedFile( )!=null)?
chooser.getSelectedFile( ).getName( ):"nothing"));
}
else {
statusbar.setText("You canceled.");
}
}
});
// Create a file chooser that allows you to pick a directory
// rather than a file.
dirButton.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser( );
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int option = chooser.showOpenDialog(SimpleFileChooser.this);
if (option == JFileChooser.APPROVE_OPTION) {
statusbar.setText("You opened " + ((chooser.getSelectedFile( )!=null)?
chooser.getSelectedFile( ).getName( ):"nothing"));
}
else {
statusbar.setText("You canceled.");
}
}
});
c.add(openButton);
c.add(saveButton);
c.add(dirButton);
c.add(statusbar);
}
public static void main(String args[]) {
SimpleFileChooser sfc = new SimpleFileChooser( );
sfc.setVisible(true);
}
}
- 2009年6月18日
- 2009年6月18日
- 2009年6月18日
- 6月18日
- 2009年12月18日
- 2009年6月6日
- 2009年6月1日星期一
- 2009年6月2日星期二
- 2009年6月4日星期四
- 2009年6月4日星期四
- 2009年6月4日星期四
- 2009年6月4日
- 2009年6月7日 星期日
- 2009年6月10日星期三
- 2009年6月10日星期三
- 2009年6月16日星期二
- 2009年6月16日星期二
- 2009年6月17日星期三
- 2009年6月15日
- 2009年6月17日
- 2009年6月17日
- 2009年6月18日
- Overload和Override的区别
- 2009年6月18日
- 2009年6月19日
- 转载
- 陈睿提供2009-06-19
- 面试问答题
- OA系统设计方案 Super OA 自动化办公系统
- 转载
- linux内核链表
- 测试题