[JAVA100例]017、文件对话框

2016-02-19 15:38 1 1 收藏

下面图老师小编要向大家介绍下[JAVA100例]017、文件对话框,看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!

【 tulaoshi.com - 编程语言 】

import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;
/**
 * pTitle: 文件过滤器演示/p
 * pDescription: FileChooserDemo文件使用的文件过滤器/p
 * pCopyright: Copyright (c) 2003/p
 * pFilename: MyFilter.java/p
 * @version 1.0
 */


  public class MyFilter extends FileFilter {
  private String files;
  public boolean accept(File f) {
    if (f.isDirectory()) {
      return true;
    }


  String extension = getExtension(f);
    if (extension != null) {
      
      if (extension.equals("java")) {//定义过滤Java文件
          return true;
      } else {
        return false;
      }


  }


  return false;
  }


  //过滤器描述
  public String getDescription() {
    return "Java";
  }
/**
 *br方法说明:获取文件扩展名
 *br输入参数:
 *br返回类型:
 */
  public static String getExtension(File f) {
    String ext = null;
    String s = f.getName();
    int i = s.lastIndexOf(´.´);

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  if (i 0 && i s.length() - 1) {
      ext = s.substring(i+1).toLowerCase();
    }
    return ext;
  }
}


  import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
/**
 * pTitle: 文件对话框演示/p
 * pDescription: 演示打开文件对话框和保存文件对话框,使用了文件过滤。/p
 * pCopyright: Copyright (c) 2003/p
 * pFilename: FileChooserDemo.java/p
 * @version 1.0
 */


  public class FileChooserDemo extends JPanel
               implements ActionListener {
  static private final String newline = "n";
  JButton openButton, saveButton;
  JTextArea log;
  JFileChooser fc;


  public FileChooserDemo() {
    super(new BorderLayout());

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  log = new JTextArea(15,40);
    log.setMargin(new Insets(10,10,10,10));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);


  //创建一个文件过滤,使用当前目录
    fc = new JFileChooser(".");
    //过滤条件在MyFilter类中定义
    fc.addChoosableFileFilter(new MyFilter());


  openButton = new JButton("打开文件",
                 createImageIcon("images/Open16.gif"));
    openButton.addActionListener(this);


  saveButton = new JButton("保存文件",
                 createImageIcon("images/Save16.gif"));
    saveButton.addActionListener(this);


  //构建一个面板,添加打开文件和保存文件
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(openButton);
    buttonPanel.add(saveButton);


  add(buttonPanel, BorderLayout.PAGE_START);
    add(logScrollPane, BorderLayout.CENTER);
  }
/**
 *br方法说明:事件处理
 *br输入参数:
 *br返回类型:
 */
  public void actionPerformed(ActionEvent e) {


  //当点击打开文件按钮
    if (e.getSource() == openButton) {
      int returnVal = fc.showOpenDialog(FileChooserDemo.this);


  if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        //在这里添加一些对文件的处理
        log.append("打开文件: " + file.getName() + newline);
      } else {
        log.append("打开文件被用户取消!" + newline);
      }


  //点击保存文件按钮
    } else if (e.getSource() == saveButton) {
      int returnVal = fc.showSaveDialog(FileChooserDemo.this);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        //在这里添加一些对文件的处理
        log.append("保存文件: " + file.getName() + newline);
      } else {
        log.append("保存文件被用户取消!" + newline);
      }
    }
  }
/**
 *br方法说明:获取图像对象
 *br输入参数:String path 图片路径
 *br返回类型:ImageIcon 图片对象
 */
  protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = FileChooserDemo.class.getResource(path);
    if (imgURL != null) {
      return new ImageIcon(imgURL);
    } else {
      System.err.println("Couldn´t find file: " + path);
      return null;
    }
  }


  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);


  //创建窗体
    JFrame frame = new JFrame("FileChooserDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


  //创建一个面板
    JComponent newContentPane = new FileChooserDemo();
    newContentPane.setOpaque(true);
    frame.setContentPane(newContentPane);


  //显示窗体
    frame.pack();
    frame.setVisible(true);
  }
}

来源:https://www.tulaoshi.com/n/20160219/1610066.html

延伸阅读
import java.awt.*; import java.awt.event.*; import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.ImageIcon; /**  * pTitle: 按钮演示/p  * pDescription: 提供一个按钮的演示。如何实现按钮和是一个按钮失效/p  * pCopyright:...
/**  * pTitle: 实现Runnable接口,获得线程。/p  * pDescription: 通过实现Runnable接口来获得自己的线程(t2)。/p  * pCopyright: Copyright (c) 2003/p  * pFilename: twothread.java/p  * @version 1.0  */ public class twothread implements Runnable { /**  *br方法说明:构造器。实际线程,并启动...
public class flowDemo{ public static void main(String[] arges){ int iPara1,iPara2,iEnd; if(arges.length!=3) { System.out.println("USE :java flowDome parameter1 parameter2 circle"); System.out.println("parameter1 : 比较条件1,数字类型"); System.out.println...
/**  * pTitle: 线程组群/p  * pDescription: 通过线程组管理下面的多个线程。/p  * pCopyright: Copyright (c) 2003/p  * pFilename: myThreadgroup.java/p  * @version 1.0  */ public class myThreadgroup extends Thread { public static int flag=1; ThreadGroup tgA; ThreadGroup tgB; /**  *b...
/**  * pTitle: 目录操作/p  * pDescription: 演示列目录下的文件,和移动一个目录/p  * pCopyright: Copyright (c) 2003/p  * pFilename: Dir.java/p  * @version 1.0  */ import java.io.*; public class Dir{  /**  *br方法说明:实现目录列表  *br输入参数:  *br返回类型:  ...

经验教程

32

收藏

33
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部