myfilterchooser.java

来自「java swing 开发代码」· Java 代码 · 共 54 行

JAVA
54
字号
// MyFilterChooser.java// Just a simple example to see what it takes to make one of these filters work.//package	jswing.ch12;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MyFilterChooser extends JFrame {  public MyFilterChooser() {    super("Filter Test Frame");    setSize(350, 200);    setDefaultCloseOperation(EXIT_ON_CLOSE);    Container c = getContentPane();    c.setLayout(new FlowLayout());        JButton openButton = new JButton("Open");    final JLabel statusbar =                  new JLabel("Output of your selection will go here");    openButton.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent ae) {        String[] pics = new String[] {"gif", "jpg", "tif"};        String[] audios = new String[] {"au", "aiff", "wav"};        JFileChooser chooser = new JFileChooser();        chooser.addChoosableFileFilter(new SimpleFileFilter(pics,                                        "Images (*.gif, *.jpg, *.tif)"));        chooser.addChoosableFileFilter(new SimpleFileFilter(".MOV"));        chooser.addChoosableFileFilter(new SimpleFileFilter(audios,                                        "Sounds (*.aiff, *.au, *.wav)"));        int option = chooser.showOpenDialog(MyFilterChooser.this);        if (option == JFileChooser.APPROVE_OPTION) {          if (chooser.getSelectedFile()!=null)             statusbar.setText("You chose " +                             chooser.getSelectedFile().getName());        }        else {          statusbar.setText("You canceled.");        }      }    });    c.add(openButton);    c.add(statusbar);    setVisible(true);  }  public static void main(String args[]) {    MyFilterChooser mfc = new MyFilterChooser();  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?