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

    public class wj extends JFrame
    implements ChangeListener, ActionListener
    {
    private static final String VERSION = "Version 1.0";
    private static final String WENJ_DIR = "文件";

    private JList fileList;
    private SoundEngine player;

    public static void main(String[] args)
    {
    wj gui = new wj();
    }

    public wj()
    {
    super("文件");
    player = new SoundEngine();
    String[] FileNames = findFiles(WENJ_DIR, null);
    makeFrame(FileNames);
    }

    private void quit()
    {
    System.exit(0);
    }

    private void showAbout()
    {
    JOptionPane.showMessageDialog(this,
    "文件 " + VERSION,
    "About 文件",
    JOptionPane.INFORMATION_MESSAGE);
    }

    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 selected = new ArrayList();
    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;
    }
    }

    public void stateChanged(ChangeEvent evt)
    {

    }

    //添加监听器
    public void actionPerformed(ActionEvent evt)
    {
    JComboBox cb = (JComboBox)evt.getSource();
    String format = (String)cb.getSelectedItem();
    if(format.equals("all")) {
    format = null;
    }
    fileList.setListData(findFiles(WENJ_DIR, format));
    }

    private void makeFrame(String[] Files)
    {

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    JPanel contentPane = (JPanel)getContentPane();
    contentPane.setBorder(new EmptyBorder(6, 10, 10, 10));
    
    makeMenuBar();
    
    
    contentPane.setLayout(new BorderLayout(8, 8));
    
    
    JPanel leftPane = new JPanel();
    {
        leftPane.setLayout(new BorderLayout(8, 8));
    
        String[] formats = { "all", ".doc", ".png", ".mp3" };
    
      
        JComboBox formatList = new JComboBox(formats);
        formatList.addActionListener(this);
        leftPane.add(formatList, BorderLayout.NORTH);
    
    
        fileList = new JList(Files);
        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("wenj files"));
        leftPane.add(scrollPane, BorderLayout.CENTER);
    }
    contentPane.add(leftPane, BorderLayout.CENTER);
    
    pack();
    
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation(d.width/2 - getWidth()/2, d.height/2 - getHeight()/2);
    setVisible(true);
    

    }
    //设置菜单项
    private void makeMenuBar()
    {
    final int SHORTCUT_MASK =
    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

    JMenuBar menubar = new JMenuBar();
    setJMenuBar(menubar);
    
    JMenu menu;
    JMenuItem item;
    
    
    menu = new JMenu("File");
    menubar.add(menu);
    
    item = new JMenuItem("Quit");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
        item.addActionListener(new ActionListener() {
                           public void actionPerformed(ActionEvent e) { quit(); }
                       });
    menu.add(item);
    
    
    menu = new JMenu("Help");
    menubar.add(menu);
    
    item = new JMenuItem("About ...");
        item.addActionListener(new ActionListener() {
                           public void actionPerformed(ActionEvent e) { showAbout(); }
                       });
    menu.add(item);
    

    }
    }

  • 相关阅读:
    epoll示例
    realloc的使用误区
    pyCharm最新激活码(2018激活码)
    python程序打包成.exe
    VS2017下载安装
    C# Cache缓存读取设置
    WPF中展示HTML
    Aspose Word模板使用总结
    js alert(“”)弹框 自定义样式
    利用反射将Model转化为sql
  • 原文地址:https://www.cnblogs.com/jxfcc/p/5403501.html
Copyright © 2011-2022 走看看