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

📄 filedialogdemo.java

📁 此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码。
💻 JAVA
字号:
// FileDialogDemo.java: Demonstrate using JFileDialog to display// file dialog boxes for opening and saving filesimport java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;public class FileDialogDemo extends JFrame  implements ActionListener{  // Menu items Open, Save, exit, and About  private JMenuItem jmiOpen, jmiSave,jmiExit, jmiAbout;  // Text area for displaying and editing text files  private JTextArea jta = new JTextArea();  // Status label for displaying operation status  private JLabel jlblStatus = new JLabel();  // File dialog box  private JFileChooser jFileChooser = new JFileChooser();  // Main method  public static void main(String[] args)  {    FileDialogDemo frame = new FileDialogDemo();    frame.setSize(300, 150);    frame.setVisible(true);  }  public FileDialogDemo()  {    setTitle("Test JFileChooser");    // Create a menu bar mb and attach to the frame    JMenuBar mb = new JMenuBar();    setJMenuBar(mb);    // Add a "File" menu in mb    JMenu fileMenu = new JMenu("File");    mb.add(fileMenu);    //add a "Help" menu in mb    JMenu helpMenu = new JMenu("Help");    mb.add(helpMenu);    // Create and add menu items to the menu    fileMenu.add(jmiOpen = new JMenuItem("Open"));    fileMenu.add(jmiSave = new JMenuItem("Save"));    fileMenu.addSeparator();    fileMenu.add(jmiExit = new JMenuItem("Exit"));    helpMenu.add(jmiAbout = new JMenuItem("About"));    // Set default directory to the current directory    jFileChooser.setCurrentDirectory(new File("."));    // Set BorderLayout for the frame    getContentPane().add(new JScrollPane(jta),      BorderLayout.CENTER);    getContentPane().add(jlblStatus, BorderLayout.SOUTH);    // Register listeners    jmiOpen.addActionListener(this);    jmiSave.addActionListener(this);    jmiAbout.addActionListener(this);    jmiExit.addActionListener(this);  }  // Handle ActionEvent for menu items  public void actionPerformed(ActionEvent e)  {    String actionCommand = e.getActionCommand();    if (e.getSource() instanceof JMenuItem)    {      if ("Open".equals(actionCommand))        open();      else if ("Save".equals(actionCommand))        save();      else if ("About".equals(actionCommand))        JOptionPane.showMessageDialog(this,          "Demonstrate Using File Dialogs",          "About This Demo",          JOptionPane.INFORMATION_MESSAGE);      else if ("Exit".equals(actionCommand))        System.exit(0);    }  }  // Open file  private void open()  {    if (jFileChooser.showOpenDialog(this) ==      JFileChooser.APPROVE_OPTION)    {      System.out.println("File name is " + jFileChooser.getSelectedFile());      open(jFileChooser.getSelectedFile());    }  }  // Open file with the specified File instance  private void open(File file)  {    try    {      // Read from the specified file and store it in jta      BufferedInputStream in = new BufferedInputStream(        new FileInputStream(file));      byte[] b = new byte[in.available()];      in.read(b, 0, b.length);      jta.append(new String(b, 0, b.length));      in.close();      // Display the status of the Open file operation in jlblStatus      jlblStatus.setText(file.getName() + " Opened");    }    catch (IOException ex)    {      jlblStatus.setText("Error opening " + file.getName());    }  }  // Save file  private void save()  {    if (jFileChooser.showSaveDialog(this) ==      JFileChooser.APPROVE_OPTION)    {      save(jFileChooser.getSelectedFile());    }  }  // Save file with specified File instance  private void save(File file)  {    try    {      // Write the text in jta to the specified file      BufferedOutputStream out = new BufferedOutputStream(        new FileOutputStream(file));      byte[] b = (jta.getText()).getBytes();      out.write(b, 0, b.length);      out.close();      // Display the status of the save file operation in jlblStatus      jlblStatus.setText(file.getName()  + " Saved ");    }    catch (IOException ex)    {      jlblStatus.setText("Error saving " + file.getName());    }  }}

⌨️ 快捷键说明

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