⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filechooserexam.java

📁 Java程序设计实用教程源代码 本书源代码按章分别放置在不同的文件夹中,所有程序均在JDK1.6环境下编译运行正常,除了第13章需要建立ODBC数据源之外,其他程序只要有Java运行环境即可直接运行
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class fileChooserExam
    extends JFrame implements ActionListener {
  TextArea ta;
  JButton open, save, quit;
  public fileChooserExam() {
    super("使用JFileChooser");
    ta = new TextArea(10, 45);
    open = new JButton("打开");
    save = new JButton("另存为");
    quit = new JButton("关闭");
    open.addActionListener(this);
    save.addActionListener(this);
    quit.addActionListener(this);
    setLayout(new FlowLayout());
    add(ta);
    add(open);
    add(save);
    add(quit);
    setSize(350, 250);
    setVisible(true);
    setResizable(false);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent event) {
        System.exit(0);
      }
    });
  }

  public void actionPerformed(ActionEvent e) {
    JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    if (e.getActionCommand() == "打开") {
      int result = fileChooser.showOpenDialog(this);
      if (result == JFileChooser.CANCEL_OPTION)
        return;
      File fileName = fileChooser.getSelectedFile();
      if (fileName == null || fileName.getName().equals(""))
        JOptionPane.showMessageDialog(this, "无效文件名", "无效的文件名",
                                      JOptionPane.ERROR_MESSAGE);
      else {
        try {
          String str = "";
          ta.setText("");
          BufferedReader br = new BufferedReader(new FileReader(fileName));
          while ( (str = br.readLine()) != null) {
            ta.append(str + "\n");
          }
          br.close();
        }
        catch (IOException ioException) {
          JOptionPane.showMessageDialog(this, "打开文件时出现错误!", "错误",
                                        JOptionPane.ERROR_MESSAGE);
        }
      }
    }
    if (e.getActionCommand() == "另存为") {
      int result = fileChooser.showSaveDialog(this);
      if (result == JFileChooser.CANCEL_OPTION)
        return;
      File fileName = fileChooser.getSelectedFile();
      if (fileName == null || fileName.getName().equals(""))
        JOptionPane.showMessageDialog(this, "无效文件名", "无效的文件名",
                                      JOptionPane.ERROR_MESSAGE);
      else {
        try {
          BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
          bw.write(ta.getText().toString());
          bw.close();
        }
        catch (IOException ioException) {
          JOptionPane.showMessageDialog(this, "打开文件时出现错误!", "错误",
                                        JOptionPane.ERROR_MESSAGE);
        }
      }
    }
    if (e.getActionCommand() == "关闭") {
      dispose();
      System.exit(0);
    }
  }

  public static void main(String args[]) {
    fileChooserExam choosefile = new fileChooserExam();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -