zoukankan      html  css  js  c++  java
  • java第五次作业

    参考老师给我们的程序soundplayer,完成了以下操作

    1.将指定目录下的所有文件显示到列表框(JList)组件中

    2.增加下拉式组合框(JComboBox),实现对列表显示的文件按照文件扩展名进行筛选

    代码如下:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;

    import javax.swing.*;
    import javax.swing.border.EmptyBorder;

    public class JListTest extends JFrame {
    private static final String music = "音乐";
    private JFrame frame;
    private JList fileList;
    private JLabel bq, bq2;
    private static final String VERSION = "Version 3.1.0";

    public JListTest() {

    String[] FileNames = findFiles(music, null);
    makeFrame(FileNames);
    frame.pack();
    frame.setVisible(true);
    frame.setSize(280, 360);
    frame.setLocation(550, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private String[] findFiles(String dirName, String suffix) {
    File dir = new File(dirName);
    if (dir.isDirectory()) {
    String[] allFiles = dir.list();
    if (suffix == null) {
    return allFiles;
    } else {
    List<String> selected = new ArrayList<String>();
    for (String filename : allFiles) {
    if (filename.endsWith(suffix)) {
    selected.add(filename);
    }
    }
    return selected.toArray(new String[selected.size()]);
    }
    } else {
    System.out.println("Error: " + dirName + " must be a directory");
    return null;
    }
    }

    private void makeFrame(String[] audioFiles) {

    frame = new JFrame(" 列表显示");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel contentPane = (JPanel) getContentPane();
    contentPane.setBorder(new EmptyBorder(6, 10, 10, 10));
    bq = new JLabel(VERSION);
    bq2 = new JLabel(" 分类查找:");
    frame.add(bq2, BorderLayout.NORTH);
    frame.add(bq, BorderLayout.SOUTH);

    contentPane.setLayout(new BorderLayout(8, 8));

    JPanel leftPane = new JPanel();
    {
    leftPane.setLayout(new BorderLayout(8, 8));

    String[] formats = { "all formats", ".wav", ".au", ".aif",".txt",".fla",".doc" };

    JComboBox formatList = new JComboBox(formats);
    formatList.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    JComboBox cb = (JComboBox) e.getSource();
    String format = (String) cb.getSelectedItem();
    if (format.equals("all formats")) {
    format = null;
    }
    fileList.setListData(findFiles(music, format));

    }});
    leftPane.add(formatList, BorderLayout.NORTH);
    fileList = new JList(audioFiles);
    fileList.setForeground(new Color(140, 171, 226));
    fileList.setBackground(new Color(0, 0, 0));
    fileList.setSelectionBackground(new Color(87, 49, 134));
    fileList.setSelectionForeground(new Color(140, 171, 226));
    JScrollPane scrollPane = new JScrollPane(fileList);
    scrollPane.setColumnHeaderView(new JLabel("files list"));
    leftPane.add(scrollPane, BorderLayout.CENTER);
    }
    contentPane.add(leftPane, BorderLayout.CENTER);
    frame.add(contentPane);
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    new JListTest();

    }

    }

    效果图如下:

  • 相关阅读:
    OSCP Learning Notes Buffer Overflows(3)
    OSCP Learning Notes Buffer Overflows(5)
    OSCP Learning Notes Exploit(3)
    OSCP Learning Notes Exploit(4)
    OSCP Learning Notes Exploit(1)
    OSCP Learning Notes Netcat
    OSCP Learning Notes Buffer Overflows(4)
    OSCP Learning Notes Buffer Overflows(1)
    OSCP Learning Notes Exploit(2)
    C++格式化输出 Learner
  • 原文地址:https://www.cnblogs.com/shemai/p/5397181.html
Copyright © 2011-2022 走看看