⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listfiledemo.java

📁 我学习JAVA时的一些练习,可以供初学者使用
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class ListFileDemo extends JFrame{
	JTextField jtfPath;
	JTextArea jtfShow;
	
	public ListFileDemo(){
		super("列出目录下的文件");
		Container container=getContentPane();
		jtfPath=new JTextField(16);
		JButton jbGo=new JButton("转到");
		jtfShow=new JTextArea();
		jtfPath.addActionListener(new ShowDirListener());
		jbGo.addActionListener(new ShowDirListener());
		JPanel panel=new JPanel();
		panel.add(jtfPath);
		panel.add(jbGo);
		container.add(panel,BorderLayout.NORTH);
		JScrollPane jsp=new JScrollPane(jtfShow);
		jsp.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		container.add(jsp,BorderLayout.CENTER);
		setSize(300,200);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	
	class ShowDirListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			showDirContent(jtfPath.getText());
		}
	}
	
	
	public void showDirContent(String path){
		File file=new File(path);
		File[] files=file.listFiles();
		StringBuffer message=new StringBuffer();
		message.append(path);
		message.append("内容如下: \n");
		for (int i=0;i<files.length;i++){
			if (files[i].isDirectory()){
				message.append("<dir>\t");
			}
			else{
				message.append("\t");
			}
			message.append(files[i].getName());
			message.append("\n");
		}
		
		jtfShow.setText(new String(message));
		
		
	}
	public static void main(String[] args){
		new ListFileDemo();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -