java中文件选择对话框
生活随笔
收集整理的這篇文章主要介紹了
java中文件选择对话框
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
java中打開文件話框我們可以,調(diào)用j操作系統(tǒng)的文件對(duì)話框:
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);}} }運(yùn)行會(huì)顯示如下的對(duì)話框:注意:FileDialog(Frame f,String s,int mode):構(gòu)造方法,f為所依賴的窗口對(duì)象,s是對(duì)話框的名字,mode取值為FileDialog.LOAD或FileDialog.SAVE;默認(rèn)模式為L(zhǎng)OAd模式。
二、調(diào)用java中內(nèi)置的文件對(duì)話框:
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);} }顯示的文件對(duì)話框效果:
總結(jié)
以上是生活随笔為你收集整理的java中文件选择对话框的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中写入文件的方法
- 下一篇: Win7下共享文件(以及凭据管理简单介绍