第五次Java作业

时间:2023-03-09 10:04:13
第五次Java作业

作业一:

文件显示列表框。

增加了下拉式组合框,text区域设置颜色为红色。

 import javax.swing.*;
import java.awt.*;
import java.io.File; public class FileUtil extends JFrame{ /**
* @param args
*/ public static String listDirectory(File dir) throws IllegalAccessException{
if(!dir.exists()){
throw new IllegalAccessException("目录"+dir+"不存在");
}
if(!dir.isDirectory()){//判断是不是目录
throw new IllegalArgumentException(dir+"不是目录");
}
String[] fileName = dir.list();
String name ="";
for(String a : fileName){
name=name+a+"\n";}
return name;
} public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
JPanel main_panel =new JPanel(new BorderLayout());
JLabel label = new JLabel("FileList");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLayout(null);
String[] itme =new String[]{"文件夹A","文件夹B"};
JComboBox box=new JComboBox();
for(int i=;i<itme.length;i++){
box.addItem(itme[i]);
}
box.setEnabled(true);
box.setEditable(true);
box.setMaximumRowCount();
box.setBounds(,,,); frame.setBounds(,,,);
frame.setVisible(true);
JTextArea main_text =new JTextArea();
main_text.setBackground(Color.red);
JScrollPane AA=new JScrollPane();
AA.setViewportView(main_text);
main_text.setEnabled(false);
main_panel.add(box,BorderLayout.NORTH);
main_panel.add(AA,BorderLayout.CENTER);
main_panel.add(label,BorderLayout.SOUTH);
frame.add(main_panel); try {
String str=FileUtil.listDirectory(new File("C:\\Users\\woshinibaba\\Desktop\\Java作业5\\soundPlayer"));
main_text.setText(str);
}
catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} frame.setBounds(,,,);
frame.setVisible(true); }
}

代码效果:

第五次Java作业第五次Java作业

作业2 :