Swing JFileChooser類

JFileChooser類是一個為用戶提供選擇檔的簡單機制的組件。

類聲明

以下是javax.swing.JFileChooser類的聲明 -

public class JFileChooser
   extends JComponent
      implements Accessible

類字段

以下是javax.swing.JFileChooser類的字段 -

類構造函數

以下是javax.swing.JFileChooser類的構造函數 -

編號 構造函數 描述
1 JFileChooser() 構造一個指向用戶默認目錄的JFileChooser實例。
2 JFileChooser(File currentDirectory) 使用給定的檔作為路徑構造JFileChooser實例。
3 JFileChooser(File currentDirectory, FileSystemView fsv) 使用給定的當前目錄和FileSystemView構造JFileChooser實例。
4 JFileChooser(FileSystemView fsv) 使用給定的FileSystemView構造一個JFileChooser實例。
5 JFileChooser(String currentDirectoryPath) 使用給定路徑構造JFileChooser實例。
6 JFileChooser(String currentDirectoryPath, FileSystemView fsv) 使用給定的當前目錄路徑和FileSystemView構造JFileChooser

類方法

以下是javax.swing.JFileChooser類的方法 -

方法繼承

該類繼承以下類中的方法 -

  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JFileChooser示例

使用編輯器創建以下Java程式:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JFileChooserExample {
    private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;

    public JFileChooserExample() {
        prepareGUI();
    }

    public static void main(String[] args) {
        JFileChooserExample swingControlDemo = new JFileChooserExample();
        swingControlDemo.showFileChooserDemo();
    }

    private void prepareGUI() {
        mainFrame = new JFrame("Java Swing JFileChooser示例(xuhuhu.com)");
        mainFrame.setSize(400, 400);
        mainFrame.setLayout(new GridLayout(3, 1));

        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        headerLabel = new JLabel("", JLabel.CENTER);
        statusLabel = new JLabel("", JLabel.CENTER);
        statusLabel.setSize(350, 100);

        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        mainFrame.add(headerLabel);
        mainFrame.add(controlPanel);
        mainFrame.add(statusLabel);
        mainFrame.setVisible(true);
    }

    private void showFileChooserDemo() {
        headerLabel.setText("Control in action: JFileChooser");
        final JFileChooser fileDialog = new JFileChooser();
        JButton showFileDialogButton = new JButton("選擇檔...");

        showFileDialogButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int returnVal = fileDialog.showOpenDialog(mainFrame);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    java.io.File file = fileDialog.getSelectedFile();
                    statusLabel.setText("選擇檔 :" + file.getName());
                } else {
                    statusLabel.setText("用戶取消打開檔");
                }
            }
        });
        controlPanel.add(showFileDialogButton);
        mainFrame.setVisible(true);
    }
}

執行上面示例代碼,得到以下結果:

JFileChooser示例


上一篇: Swing控件 下一篇: Swing事件處理