java学习笔记(5)
2016-04-16 12:47
537 查看
上周,老师让我们仿照音乐播放器程序做一个小程序,要求能够将指定目录下的所有文件显示到列表框组件中,周二的时候,老师讲了要怎么显示找到那些文件并显示,让后让我们试着关联一下,可是我试了很多次,就是做不好,最后参考同学的方法,也只是做了一个能显示目录,文件还是显示不出来,我完了再看一看,做一做,如果实在不会再问老师吧。由于不太会,作业交迟了。老师,对不起。
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class FileUtils {
private static final String music = "音乐";
private static final String VERSION = "Version 3.1.0";
public static String listDirectory(File dir) throws IllegalAccessException{
if(!dir.exists()){
throw new IllegalAccessException("目录" + dir + "不存在。");
}
if(!dir.isDirectory()){
throw new IllegalAccessException(dir + "不是目录");
}
String[] files = dir.list();
String m ="";
for(String a : files){
m=m+a+"\n";}
return m;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
JPanel main_panel =new JPanel(new BorderLayout());//面板
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLayout(null);
//设置组合框
String[] itme = {".png","ico","wav",".doc"};
JComboBox frm=new JComboBox(itme);
frm.setEnabled(true);
frm.setEditable(true);
frm.setMaximumRowCount(5);
frm.setBounds(230,30,130,25);
frame.setBounds(400,200,600,400);
frame.setVisible(true);
JTextArea main_text =new JTextArea();
main_text.setBackground(Color.gray);
JScrollPane z=new JScrollPane();
z.setViewportView(main_text);
main_text.setEnabled(false);
main_panel.add(frm,BorderLayout.NORTH);
main_panel.add(z,BorderLayout.CENTER);
frame.add(main_panel);
try {
String LLO = FileUtils.listDirectory(new File("E:\soundPlayer\audio"));
main_text.setText(LLO);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class FileUtils {
private static final String music = "音乐";
private static final String VERSION = "Version 3.1.0";
public static String listDirectory(File dir) throws IllegalAccessException{
if(!dir.exists()){
throw new IllegalAccessException("目录" + dir + "不存在。");
}
if(!dir.isDirectory()){
throw new IllegalAccessException(dir + "不是目录");
}
String[] files = dir.list();
String m ="";
for(String a : files){
m=m+a+"\n";}
return m;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
JPanel main_panel =new JPanel(new BorderLayout());//面板
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLayout(null);
//设置组合框
String[] itme = {".png","ico","wav",".doc"};
JComboBox frm=new JComboBox(itme);
frm.setEnabled(true);
frm.setEditable(true);
frm.setMaximumRowCount(5);
frm.setBounds(230,30,130,25);
frame.setBounds(400,200,600,400);
frame.setVisible(true);
JTextArea main_text =new JTextArea();
main_text.setBackground(Color.gray);
JScrollPane z=new JScrollPane();
z.setViewportView(main_text);
main_text.setEnabled(false);
main_panel.add(frm,BorderLayout.NORTH);
main_panel.add(z,BorderLayout.CENTER);
frame.add(main_panel);
try {
String LLO = FileUtils.listDirectory(new File("E:\soundPlayer\audio"));
main_text.setText(LLO);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
相关文章推荐
- Eclipse安装PyDev
- java基础(五),接口
- JDK自带工具一览表
- java基础知识拾遗(二)
- java反序列化简介
- Java环境变量配置
- Java 使用 Stream API 筛选 List
- Struts中错误: No result defined for action
- Mac下Maven的安装和配置
- spring 监听器 IntrospectorCleanupListener简介
- MyBatis学习总结(八)——Mybatis3.x与Spring4.x整合
- Java学习-10天
- Java 线程池原理解析(一)
- JAVA修改组件
- spring mvc中的@PathVariable
- eclipse aidl.exe error while creating directories
- 一个Java音乐播放器
- 位运算符详细总结
- 一个求迷宫入口到出口最近距离的程序 JAVA版本
- Struts2 入门学习笔记(四)——输入校验