zoukankan      html  css  js  c++  java
  • 第五篇学习笔记

    将指定目录下的所有文件显示到列表框(JList)中。
    代码如下:

    1
    import java.awt*; 2 import javax.swing*; 3 import java.util.*; 4 import java.io.*; 5 6 public class FileUtils extends JFrame 7 implements ChangeListener, ActionListener 8 { 9 private static final String VERSION = "Version 1.0"; 10 private static final String WENJ_DIR = "文件"; 11 12 private JList fileList; 13 private SoundEngine player; 14 15 16 public static void main(String[] args) 17 { 18 FileUtils gui = new FileUtils(); 19 } 20 21 22 public FileUtils() 23 { 24 super("文件"); 25 player = new SoundEngine(); 26 String[] FileNames = findFiles(WENJ_DIR, null); 27 makeFrame(FileNames); 28 } 29 30 31 32 private void quit() 33 { 34 System.exit(0); 35 } 36 37 38 39 private void showAbout() 40 { 41 JOptionPane.showMessageDialog(this, 42 "文件 " + VERSION, 43 "About 文件", 44 JOptionPane.INFORMATION_MESSAGE); 45 } 46 47 private String[] findFiles(String dirName, String suffix) 48 { 49 File dir = new File(dirName); 50 if(dir.isDirectory()) { 51 String[] allFiles = dir.list(); 52 if(suffix == null) { 53 return allFiles; 54 } 55 else { 56 List<String> selected = new ArrayList<String>(); 57 for(String filename : allFiles) { 58 if(filename.endsWith(suffix)) { 59 selected.add(filename); 60 } 61 } 62 return selected.toArray(new String[selected.size()]); 63 } 64 } 65 else { 66 System.out.println("Error: " + dirName + " must be a directory"); 67 return null; 68 } 69 } 70 71 72 public void stateChanged(ChangeEvent evt) 73 { 74 75 } 76 77 //添加监听器 78 public void actionPerformed(ActionEvent evt) 79 { 80 JComboBox cb = (JComboBox)evt.getSource(); 81 String format = (String)cb.getSelectedItem(); 82 if(format.equals("all")) { 83 format = null; 84 } 85 fileList.setListData(findFiles(WENJ_DIR, format)); 86 } 87 88 89 private void makeFrame(String[] Files) 90 { 91 92 setDefaultCloseOperation(EXIT_ON_CLOSE); 93 94 JPanel contentPane = (JPanel)getContentPane(); 95 contentPane.setBorder(new EmptyBorder(6, 10, 10, 10)); 96 97 makeMenuBar(); 98 99 100 contentPane.setLayout(new BorderLayout(8, 8)); 101 102 103 JPanel leftPane = new JPanel(); 104 { 105 leftPane.setLayout(new BorderLayout(8, 8)); 106 107 String[] formats = { "all", ".doc", ".png", ".mp3" }; 108 109 110 JComboBox formatList = new JComboBox(formats); 111 formatList.addActionListener(this); 112 leftPane.add(formatList, BorderLayout.NORTH); 113 114 115 fileList = new JList(Files); 116 fileList.setForeground(new Color(140,171,226)); 117 fileList.setBackground(new Color(0,0,0)); 118 fileList.setSelectionBackground(new Color(87,49,134)); 119 fileList.setSelectionForeground(new Color(140,171,226)); 120 JScrollPane scrollPane = new JScrollPane(fileList); 121 scrollPane.setColumnHeaderView(new JLabel("wenj files")); 122 leftPane.add(scrollPane, BorderLayout.CENTER); 123 } 124 contentPane.add(leftPane, BorderLayout.CENTER); 125 126 pack(); 127 128 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 129 setLocation(d.width/2 - getWidth()/2, d.height/2 - getHeight()/2); 130 setVisible(true); 131 } 132 //设置菜单项 133 private void makeMenuBar() 134 { 135 final int SHORTCUT_MASK = 136 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); 137 138 JMenuBar menubar = new JMenuBar(); 139 setJMenuBar(menubar); 140 141 JMenu menu; 142 JMenuItem item; 143 144 145 menu = new JMenu("File"); 146 menubar.add(menu); 147 148 item = new JMenuItem("Quit"); 149 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK)); 150 item.addActionListener(new ActionListener() { 151 public void actionPerformed(ActionEvent e) { quit(); } 152 }); 153 menu.add(item); 154 155 156 menu = new JMenu("Help"); 157 menubar.add(menu); 158 159 item = new JMenuItem("About ..."); 160 item.addActionListener(new ActionListener() { 161 public void actionPerformed(ActionEvent e) { showAbout(); } 162 }); 163 menu.add(item); 164 } 165 }
  • 相关阅读:
    图像的纹理分析
    图像的小波变换
    图像的哈尔变换
    图像的K-L变换
    图像的斜变换
    图像的波尔什-哈达玛变换
    今日心得:给自己写信
    今日心得:人的幸福感取决于什么?
    今日心得:人生就像一杯茶,不会苦一辈子但会苦一阵子
    今日心得:纪念徐志摩117周年
  • 原文地址:https://www.cnblogs.com/lvbloges/p/5401528.html
Copyright © 2011-2022 走看看