第五次作业

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,
"文件\n" + 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);

}
}

posted @ 2016-04-18 10:35  14软件G1靳晓芳  阅读(115)  评论(0编辑  收藏  举报