文件选配演示2 : 文件选择 « 图形用户界面 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 图形用户界面 » 文件选择屏幕截图 
文件选配演示2
文件选配演示2
  
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.filechooser.FileView;

public class FileSample {

  public static void main(String args[]) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JLabel directoryLabel = new JLabel();
    contentPane.add(directoryLabel, BorderLayout.NORTH);

    final JLabel filenameLabel = new JLabel();
    contentPane.add(filenameLabel, BorderLayout.SOUTH);

    final JButton button = new JButton("Open FileChooser");
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        Component parent = (ComponentactionEvent.getSource();
        JFileChooser fileChooser = new JFileChooser(".");
        fileChooser.setAccessory(new LabelAccessory(fileChooser));
        FileView view = new JavaFileView();
        fileChooser.setFileView(view);
        int status = fileChooser.showOpenDialog(parent);
        if (status == JFileChooser.APPROVE_OPTION) {
          File selectedFile = fileChooser.getSelectedFile();
          directoryLabel.setText(selectedFile.getParent());
          filenameLabel.setText(selectedFile.getName());
        else if (status == JFileChooser.CANCEL_OPTION) {
          directoryLabel.setText(" ");
          filenameLabel.setText(" ");
        }
      }
    };
    button.addActionListener(actionListener);
    contentPane.add(button, BorderLayout.CENTER);

    frame.setSize(300200);
    frame.setVisible(true);
  }
}

class LabelAccessory extends JLabel implements PropertyChangeListener {
  private static final int PREFERRED_WIDTH = 125;

  private static final int PREFERRED_HEIGHT = 100;

  public LabelAccessory(JFileChooser chooser) {
    setVerticalAlignment(JLabel.CENTER);
    setHorizontalAlignment(JLabel.CENTER);
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
  }

  public void propertyChange(PropertyChangeEvent changeEvent) {
    String changeName = changeEvent.getPropertyName();
    if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
      File file = (FilechangeEvent.getNewValue();
      if (file != null) {
        ImageIcon icon = new ImageIcon(file.getPath());
        if (icon.getIconWidth() > PREFERRED_WIDTH) {
          icon = new ImageIcon(icon.getImage().getScaledInstance(
              PREFERRED_WIDTH, -1, Image.SCALE_DEFAULT));
          if (icon.getIconHeight() > PREFERRED_HEIGHT) {
            icon = new ImageIcon(icon.getImage().getScaledInstance(
                -1, PREFERRED_HEIGHT, Image.SCALE_DEFAULT));
          }
        }
        setIcon(icon);
      }
    }
  }
}

class JavaFileView extends FileView {
  Icon javaIcon = new DiamondIcon(Color.blue);

  Icon classIcon = new DiamondIcon(Color.green);

  Icon htmlIcon = new DiamondIcon(Color.red);

  Icon jarIcon = new DiamondIcon(Color.pink);

  public String getName(File file) {
    String filename = file.getName();
    if (filename.endsWith(".java")) {
      String name = filename + " : " + file.length();
      return name;
    }
    return null;
  }

  public String getTypeDescription(File file) {
    String typeDescription = null;
    String filename = file.getName().toLowerCase();

    if (filename.endsWith(".java")) {
      typeDescription = "Java Source";
    else if (filename.endsWith(".class")) {
      typeDescription = "Java Class File";
    else if (filename.endsWith(".jar")) {
      typeDescription = "Java Archive";
    else if (filename.endsWith(".html"|| filename.endsWith(".htm")) {
      typeDescription = "Applet Loader";
    }
    return typeDescription;
  }

  public Icon getIcon(File file) {
    if (file.isDirectory()) {
      return null;
    }
    Icon icon = null;
    String filename = file.getName().toLowerCase();
    if (filename.endsWith(".java")) {
      icon = javaIcon;
    else if (filename.endsWith(".class")) {
      icon = classIcon;
    else if (filename.endsWith(".jar")) {
      icon = jarIcon;
    else if (filename.endsWith(".html"|| filename.endsWith(".htm")) {
      icon = htmlIcon;
    }
    return icon;
  }
}

class DiamondIcon implements Icon {
  private Color color;

  private boolean selected;

  private int width;

  private int height;

  private Polygon poly;

  private static final int DEFAULT_WIDTH = 10;

  private static final int DEFAULT_HEIGHT = 10;

  public DiamondIcon(Color color) {
    this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected) {
    this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  }

  public DiamondIcon(Color color, boolean selected, int width, int height) {
    this.color = color;
    this.selected = selected;
    this.width = width;
    this.height = height;
    initPolygon();
  }

  private void initPolygon() {
    poly = new Polygon();
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    poly.addPoint(0, halfHeight);
    poly.addPoint(halfWidth, 0);
    poly.addPoint(width, halfHeight);
    poly.addPoint(halfWidth, height);
  }

  public int getIconHeight() {
    return height;
  }

  public int getIconWidth() {
    return width;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(color);
    g.translate(x, y);
    if (selected) {
      g.fillPolygon(poly);
    else {
      g.drawPolygon(poly);
    }
    g.translate(-x, -y);
  }
}



           
         
    
  
Related examples in the same category
1. JFileChooser是一个标准的对话框,从文件系统选择一个文件。
2. 文件选择文件选择
3. 演示文件对话框演示文件对话框
4. JFileChooser类的事件JFileChooser类的事件
5. 一个简单的文件选择一个简单的文件选择
6. 对话框过滤器对话框过滤器
7. 查看图形文件的缩略图查看图形文件的缩略图
8. 使用FileChooser文件过滤器定制filechooser使用FileChooser文件过滤器定制filechooser
9. 选择文件选择文件
10. 自定义JFileChooser自定义JFileChooser
11. 一个简单的FileFilter类,文件名扩展
12. 界面文件选配演示界面文件选配演示
13. FileChooser演示FileChooser演示
14. 文件选配演示4文件选配演示4
15. 获取文件类型的图标
16. 让大文件类型的图标
17. 在JFileChooser对话框监听批准和取消事件
18. String javax.swing.JFileChooser.APPROVE_SELECTION
19. void javax.swing.JFileChooser.addActionListener(ActionListener l)
20. JDialog javax.swing.JFileChooser.createDialog(Component parent)
21. void javax.swing.JFileChooser.setDialogType(int dialogType)
22. 在JFileChooser对话框改变按钮的字符
23. 确定某个文件是否隐藏
24. 在JFileChooser对话框显示隐藏文件
25. 在JFileChooser对话框启用多项选择
26. 在JFileChooser对话框倾听改变到所选文件
27. 获取文件类型名称
28. Getting and Setting the Selected File of a JFileChooser Dialog
29. Getting and Setting the Current Directory of a JFileChooser Dialog
30. Determining If the Approve or Cancel Button Was Clicked in a JFileChooser Dialog
31. 添加筛选器到一个文件选择对话框
32. 对话框只显示目录中的文件
33. 建立一个JFileChooser对话框
34. 本地化JFileChooser
35. 选择一个目录选择JFileChooser
36. 禁用JFileChooser的“新建文件夹”按钮
37. 从JFileChooser验证文件名
38. 继承FileView创建自定义的文件选择
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.