public class TestTree { public static int num = 0; public static void main(String[] args) { File f = new File("c:\\test"); filelist(f, -1); } public static void filelist(File f, int id) { File[] fa = f.listFiles(); for (int i = 0; i < fa.length; i++) { if (fa[i].isDirectory()) { num++; System.out.print("fileName:" + fa[i].getAbsoluteFile() + "==="); System.out.print("id:" + num + "==="); System.out.println("pid:" + id + "===="); filelist(fa[i], num); } else { num++; System.out.print("fileName:" + fa[i].getAbsoluteFile() + "==="); System.out.print("id:" + num + "==="); System.out.println("pid:" + id + "===="); } } } }
把代码贴出来给你看看吧,强调一点,我这程序还有待加工
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;public class Exam4 extends JFrame {
private static final long serialVersionUID = 1L;
static String filename = null;
public Exam4(File file) {
final File fl = file;
Container container = this.getContentPane();
container.setLayout(new BorderLayout()); /**
* 取出文件库中文件
*/
File[] fileList = file.listFiles();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("试卷库");
DefaultMutableTreeNode child;
for (File f : fileList) {
child = new DefaultMutableTreeNode(f.getName());
root.add(child);
}
this.addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
}); final JTree tree = new JTree(root);
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION); JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1, 2));
JToolBar jtb = new JToolBar();
JButton jb = new JButton("编译"); final JTextField jtf = new JTextField();
final TextArea area= new TextArea();
//String s =null;
MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
TreePath path = tree.getSelectionPath();
String[] str2 = path.toString().split(",");
String str3 = str2[str2.length - 1].substring(1,
str2[str2.length - 1].length() - 1);
if (e.getClickCount() == 2) {
String string = fl.getAbsolutePath() + "\\" + str3;
filename = string;
try {
String s1 = "";
FileReader fr = new FileReader(string);
BufferedReader br = new BufferedReader(fr);
String s2 = br.readLine();
do {
s1 += s2 + "\n";
s2 = br.readLine();
} while (s2 != null);
br.close();
area.setText(s1); } catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
} }
}; tree.addMouseListener(ml); // 为每个文件添加双击打开事件临听器 jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//String buff1 = null;
//String[] buff2 = new String[1];
try {
//buff1 = area.getText();
//buff2[0] = filename;
//jtf.setText(buff1);
String command = "C:\\Program Files\\Java\\jdk1.6.0_05\\bin\\javac.exe" + " " + filename;
Process process = Runtime.getRuntime().exec(command);
//int value = process.exitValue();
InputStream is = process.getErrorStream();
BufferedReader br ;
br=new BufferedReader(new InputStreamReader(is));
//OutputStream ops = process.getOutputStream();
//BufferedReader br2 = new BufferedReader(new OutputStreamReader(ops));
String s;
String s2=null;
String s3;
String s4 = null;
while((s=br.readLine())!=null)
{
s2 += s + "\n";
}
if(s2!=null){
jtf.setText(s2);
}else{
// jtf.setText(s3);
}
} catch (Exception e2) {
e2.printStackTrace();
}
} }); area.setFont(new Font("宋体", Font.PLAIN, 15));
jtb.add(jb);
jtb.add(jtf);
p2.add(jtb);
container.add(new JScrollPane(tree), BorderLayout.WEST);
container.add(area, BorderLayout.CENTER);
container.add(p2, BorderLayout.SOUTH); setLocation(100, 100);
setSize(800, 600);
this.setTitle("***---阅卷系统");
setVisible(true); } public static void main(String[] args) {
File file = new File("D:\\exam");
new Exam3(file);
}
}
把代码贴出来给你看看吧,强调一点,我这程序还有待加工
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;public class Exam4 extends JFrame {
private static final long serialVersionUID = 1L;
static String filename = null;
public Exam4(File file) {
final File fl = file;
Container container = this.getContentPane();
container.setLayout(new BorderLayout()); /**
* 取出文件库中文件
*/
File[] fileList = file.listFiles();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("试卷库");
DefaultMutableTreeNode child;
for (File f : fileList) {
child = new DefaultMutableTreeNode(f.getName());
root.add(child);
}
this.addWindowListener(new WindowAdapter() {
public void WindowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
}); final JTree tree = new JTree(root);
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION); JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1, 2));
JToolBar jtb = new JToolBar();
JButton jb = new JButton("编译"); final JTextField jtf = new JTextField();
final TextArea area= new TextArea();
MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
TreePath path = tree.getSelectionPath();
String[] str2 = path.toString().split(",");
String str3 = str2[str2.length - 1].substring(1,
str2[str2.length - 1].length() - 1);
if (e.getClickCount() == 2) {
String string = fl.getAbsolutePath() + "\\" + str3;
filename = string;
try {
String s1 = "";
FileReader fr = new FileReader(string);
BufferedReader br = new BufferedReader(fr);
String s2 = br.readLine();
do {
s1 += s2 + "\n";
s2 = br.readLine();
} while (s2 != null);
br.close();
area.setText(s1); } catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
} }
}; tree.addMouseListener(ml); jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String command = "C:\\Program Files\\Java\\jdk1.6.0_05\\bin\\javac.exe" + " " + filename;
Process process = Runtime.getRuntime().exec(command);
InputStream is = process.getErrorStream();
BufferedReader br ;
br=new BufferedReader(new InputStreamReader(is));
String s;
String s2=null;
String s3;
String s4 = null;
while((s=br.readLine())!=null)
{
s2 += s + "\n";
}
if(s2!=null){
jtf.setText(s2);
}else{
// jtf.setText(s3);
}
} catch (Exception e2) {
e2.printStackTrace();
}
} }); area.setFont(new Font("宋体", Font.PLAIN, 15));
jtb.add(jb);
jtb.add(jtf);
p2.add(jtb);
container.add(new JScrollPane(tree), BorderLayout.WEST);
container.add(area, BorderLayout.CENTER);
container.add(p2, BorderLayout.SOUTH); setLocation(100, 100);
setSize(800, 600);
this.setTitle("***---阅卷系统");
setVisible(true); } public static void main(String[] args) {
File file = new File("D:\\exam");
new Exam3(file);
}
}
public class TestTree {
public static int num = 0; public static void main(String[] args) {
File f = new File("c:\\test");
filelist(f, -1);
} public static void filelist(File f, int id) { File[] fa = f.listFiles(); for (int i = 0; i < fa.length; i++) { if (fa[i].isDirectory()) {
num++;
System.out.print("fileName:" + fa[i].getAbsoluteFile() + "===");
System.out.print("id:" + num + "===");
System.out.println("pid:" + id + "====");
filelist(fa[i], num); } else {
num++;
System.out.print("fileName:" + fa[i].getAbsoluteFile() + "===");
System.out.print("id:" + num + "===");
System.out.println("pid:" + id + "====");
}
} }
}
filename = string;
放在if (e.getClickCount() == 2)上面就好了
public static int num = 0;改为public static int num = -1;