Java程序设计 图形用户界面 【十四】文件选择框JFileChooser
文件选择框JFileChooser
JFileChooser组件实现通过文件选择框选择要打开或保存的文件
JFileChooser类
方法 | 作用 |
---|---|
public JFileChooser() | 指向用户默认的目录 |
public JFileChooser(String currentDirectiryPath) | 指定文件选择的目录 |
public File getSelectedFile() | 返回选择的文件 |
public int showSaveDialog(Component parent) | 打开保存对话框,返回保存状态 |
public int showOpenDialog(Component parent)throws HeadlessException | 打开文件选择对话框,返回打开状态 |
public void setDialogTitle(String dialogTitle) | 设置文件选择框的标题 |
public void setApproveButtonText(String approveButtomText) | 设置文件选择的接收按钮内容 |
常量 | 作用 |
---|---|
public static final int APPROVE_OPTION | 选择确定后返回该值 |
public static final int CANCEL_OPTION | 选择取消后返回该值 |
public static final int ERROR_OPTION | 发生错误后返回该值 |
实现打开文件和保存文件
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.rmi.UnmarshalException;
import java.util.Scanner;
class Note implements ActionListener{
private JTextArea area = new JTextArea(8,10);
private JFrame frame = new JFrame("一");
private JButton open = new JButton("打开文件");
private JButton save = new JButton("保存文件");
private JLabel label = new JLabel("未打开文件");
private JPanel butPan = new JPanel();
public Note(){
this.butPan.add(open);
this.butPan.add(save);
frame.setLayout(new BorderLayout(3,3));
frame.add(this.label,BorderLayout.NORTH);
frame.add(this.butPan,BorderLayout.SOUTH);
frame.add(new JScrollPane(this.area),
BorderLayout.CENTER);
this.open.addActionListener(this);
this.save.addActionListener(this);
frame.setSize(300,200);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
File file = null;
int result = 0;
JFileChooser fileChooser = new JFileChooser();
if (e.getSource()==this.open){
this.area.setText("");
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("打开文件");
result = fileChooser.showOpenDialog(this.frame);
if (result == JFileChooser.APPROVE_OPTION){
file = fileChooser.getSelectedFile();
this.label.setText("打开的文件名:"+file.getName());
}else if (result == JFileChooser.CANCEL_OPTION){
this.label.setText("没有选择任何文件");
}else {
this.label.setText("操作出错");
}
if(file!=null){
try {
Scanner scanner = new Scanner(new FileInputStream(file));
scanner.useDelimiter("\n");
while (scanner.hasNext()){
this.area.append(scanner.next());
this.area.append("\n");
}
}catch (Exception a){
this.label.setText("文件读取错误");
}
}
}
if (e.getSource()==this.save){
result = fileChooser.showSaveDialog(this.frame);
if(result==JFileChooser.APPROVE_OPTION){
file=fileChooser.getSelectedFile();
this.label.setText("保存的文件名为:"+file.getName());
}else if(result==JFileChooser.CANCEL_OPTION){
this.label.setText("没有选择任何文件");
}else {
this.label.setText("操作出错");
}
if(file!=null){
try {
PrintStream out = new PrintStream(new FileOutputStream(file));
out.print(this.area.getText());
out.close();
}catch (Exception a){
this.label.setText("文件读取错误");
}
}
}
}
}
public class Hello {
public static void main(String[] args) {
new Note();
}
}
首先通过“打开文件”按钮触发了一个事件,然后在此事件中使用JFileChooser打开了一个文件的选择框,并使用Scanner类将内容全部显示到文本区中。当单击“储存文件”按钮时会打开“保存”对话框,可以输入文件名称,然后会通过打印流将全部的内容输出到文件中