在Java中,我想使用JFileChooser
以自己的格式加載一個文件[無論它是什麼格式]。意思是我不想閱讀和顯示我的JFrame
內的內容。相反,我希望它們可以像在Windows照片查看器/ Irfan查看器中打開的圖像一樣打開/加載,並在Adobe Reader中打開PDF。通過單擊按鈕。如何使用JFileChooser加載文件?
我已經搜查了很多。但是,我閱讀的所有教程都通過單擊JButton
來說明如何打印「打開此文件/選擇此文件」行。實際上沒有人按鈕點擊打開/加載文件。可能是我沒有正確地得到他們說的,因爲我是Java的新手。我希望我的問題是明確的,請幫助...
這裏是我從一個教程頁面得到的代碼:
public class JFileChooserTest {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("JComboBox Test");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Select File");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getName());
}
}
});
frame.add(button);
frame.pack();
frame.setVisible(true);
}
}
這是我想與Java做的。這裏是有窗戶的例子:
瀏覽按鈕即可打開該窗口
當我選擇XLS文件並單擊打開按鈕,XLS文件將打開。我想和Java完全一樣。希望現在更清楚。
你想要什麼?你想看文件內容嗎? – Adem 2014-12-04 07:42:22
不,我想加載/導入我通過單擊按鈕選擇的文件。就像我們在Windows文件打開資源管理器中打開文件。 – Sam 2014-12-04 07:43:47
你能解釋一下「load/import」是什麼意思? – Adem 2014-12-04 07:48:20