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

📄 filechooserdemo.java

📁 初期JAVA学习非常有用的资料。帮助深入了解API。特别是Applet。
💻 JAVA
字号:
import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.filechooser.*;public class FileChooserDemo extends JFrame {    static private final String newline = "\n";    public FileChooserDemo() {        super("FileChooserDemo");        //Create the log first, because the action listeners        //need to refer to it.        final JTextArea log = new JTextArea(5,20);        log.setMargin(new Insets(5,5,5,5));        log.setEditable(false);        JScrollPane logScrollPane = new JScrollPane(log);        //Create a file chooser        final JFileChooser fc = new JFileChooser();        //Create the open button        ImageIcon openIcon = new ImageIcon("images/open.gif");        JButton openButton = new JButton("Open a File...", openIcon);        openButton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                int returnVal = fc.showOpenDialog(FileChooserDemo.this);                if (returnVal == JFileChooser.APPROVE_OPTION) {                    File file = fc.getSelectedFile();                    //this is where a real application would open the file.                    log.append("Opening: " + file.getName() + "." + newline);                } else {                    log.append("Open command cancelled by user." + newline);                }            }        });        //Create the save button        ImageIcon saveIcon = new ImageIcon("images/save.gif");        JButton saveButton = new JButton("Save a File...", saveIcon);        saveButton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                int returnVal = fc.showSaveDialog(FileChooserDemo.this);                if (returnVal == JFileChooser.APPROVE_OPTION) {                    File file = fc.getSelectedFile();                    //this is where a real application would save the file.                    log.append("Saving: " + file.getName() + "." + newline);                } else {                    log.append("Save command cancelled by user." + newline);                }            }        });        //For layout purposes, put the buttons in a separate panel        JPanel buttonPanel = new JPanel();        buttonPanel.add(openButton);        buttonPanel.add(saveButton);        //Explicitly set the focus sequence.        openButton.setNextFocusableComponent(saveButton);        saveButton.setNextFocusableComponent(openButton);        //Add the buttons and the log to the frame        Container contentPane = getContentPane();        contentPane.add(buttonPanel, BorderLayout.NORTH);        contentPane.add(logScrollPane, BorderLayout.CENTER);    }    public static void main(String[] args) {        JFrame frame = new FileChooserDemo();        frame.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });        frame.pack();        frame.setVisible(true);    }}

⌨️ 快捷键说明

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