簡體   English   中英

如何在JAVA swing中將html文件按鈕改為文件瀏覽器?

[英]How can I have somehing like the html file button to browser in file in JAVA swing?

我試圖通過Swing創建我的第一個UI頁面。 在此頁面中,我希望瀏覽一個文件。 有人可以幫助我實現這個目標嗎?

查看Sun的本教程頁面: http//java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html

基本實施涉及:

//Create a file chooser
final JFileChooser fc = new JFileChooser();
...
//In response to a button click:
int returnVal = fc.showOpenDialog(aComponent);

返回值為您提供有關用戶是單擊“確定”還是“取消”等信息,然后您可以查詢文件選擇器對象以找出所選文件。

你似乎想要一個JFileChooser

在此頁面上,您將找到CodeExample JFileChooser的工作原理。

// This action creates and shows a modal open-file dialog.
    public class OpenFileAction extends AbstractAction {
        JFrame frame;
        JFileChooser chooser;

    OpenFileAction(JFrame frame, JFileChooser chooser) {
        super("Open...");
        this.chooser = chooser;
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent evt) {
        // Show dialog; this method does not return until dialog is closed
        chooser.showOpenDialog(frame);

        // Get the selected file
        File file = chooser.getSelectedFile();
    }
};

// This action creates and shows a modal save-file dialog.
public class SaveFileAction extends AbstractAction {
    JFileChooser chooser;
    JFrame frame;

    SaveFileAction(JFrame frame, JFileChooser chooser) {
        super("Save As...");
        this.chooser = chooser;
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent evt) {
        // Show dialog; this method does not return until dialog is closed
        chooser.showSaveDialog(frame);

        // Get the selected file
        File file = chooser.getSelectedFile();
    }
};

在按鈕上添加actionListener以打開JFileChooser

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM