java中文件选择对话框
生活随笔
收集整理的這篇文章主要介紹了
java中文件选择对话框
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java中打開文件話框我們可以,調用j操作系統的文件對話框:
public class ChooseFile extends MouseAdapter{private JTextField filePathFild;private JFrame frame;private FileDialog fileDialog;private String filePath;private String fileName;public ChooseFile(JTextField filePathFild,JFrame frame) {this.filePathFild = filePathFild;this.frame = frame;}@Overridepublic void mouseClicked(MouseEvent e) {super.mouseClicked(e);fileDialog = new FileDialog(frame);fileDialog.show();filePath = fileDialog.getDirectory(); fileName = fileDialog.getFile(); if(filePath == null || fileName == null){ }else{filePathFild.setText(filePath + fileName);}} }運行會顯示如下的對話框:注意:FileDialog(Frame f,String s,int mode):構造方法,f為所依賴的窗口對象,s是對話框的名字,mode取值為FileDialog.LOAD或FileDialog.SAVE;默認模式為LOAd模式。
二、調用java中內置的文件對話框:
public class DialogTest {public static void main(String[] args) {JFrame frame = new JFrame();JButton button = new JButton("button");button.addMouseListener(new ShowDialogLintener(frame));frame.add(button,BorderLayout.CENTER);frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.pack();} } class ShowDialogLintener extends MouseAdapter{JFrame frame;public ShowDialogLintener(JFrame frame) {this.frame = frame;}@Overridepublic void mouseClicked(MouseEvent arg0) {super.mouseClicked(arg0);JFileChooser chooser = new JFileChooser(".");chooser.showOpenDialog(frame);String filePath = chooser.getSelectedFile().getAbsolutePath();System.out.println(filePath);} }顯示的文件對話框效果:
總結
以上是生活随笔為你收集整理的java中文件选择对话框的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中写入文件的方法
- 下一篇: Win7下共享文件(以及凭据管理简单介绍