Use component JList to complete the display of specified file contents with a list component
Import java.awt.*;
Import Java.io.File;
Import java.util.ArrayList;
Import java.util.List;
Import javax.swing.*;
public class Liebiao extends JFrame {
Private static final String music = "File";
Private JFrame frame;
Private JList fileList;
Public Liebiao () {
string[] FileNames = findfiles (music, NULL);
Makeframe (FileNames);
Frame.pack ();
Frame.setvisible (TRUE);
Frame.setsize (250, 380);
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.setlayout (New BorderLayout (8, 8));
JPanel Leftpane = new JPanel ();
{
Leftpane.setlayout (New BorderLayout (8, 8));
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 Liebiao ();
}
}
Java Fifth time job