Java Fifth time job

Source: Internet
Author: User

Refer to the teacher to our program SoundPlayer, completed the following operations

1. Display all files in the specified directory to the list box (JList) component

2. Add a drop-down combo box (JComboBox) to filter the list of files displayed by file name extension

The code is as follows:

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 = "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 ("list display");
Setdefaultcloseoperation (Exit_on_close);

JPanel ContentPane = (JPanel) getcontentpane ();
Contentpane.setborder (New Emptyborder (6, 10, 10, 10));
BQ = new JLabel (VERSION);
BQ2 = new JLabel ("category lookup:");
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 = (JCo Mbobox) 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 ();

}

}

As follows:

Java Fifth time job

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.