急!日记本编到一半,又出问题了,真令人沮丧!!! 是不是代码太长了?其实是很简单的东西,大部分的代码是用来定义GUI的,关键部分,也就是public void valueChanged(TreeSelectionEvent e)的地方很短。大家粘贴回去,调试一下,应该很方便的。我想了很久,没有找到原因。非常郁闷!能不能帮帮忙啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我有一个日志的例子,送给你:import javax.swing.*;import javax.swing.tree.*;import java.awt.*;import java.awt.event.*;import javax.swing.event.*;import java.io.*;class Remember extends JFrame implements TreeSelectionListener,ActionListener{ JTree tree=null; JTextArea text=new JTextArea("",20,20); int i=0; DefaultMutableTreeNode root; JButton b_save=new JButton("保存日志"),b_del=new JButton("删除日志"); DefaultMutableTreeNode month[]=new DefaultMutableTreeNode[13]; Remember() { Container con=getContentPane(); root=new DefaultMutableTreeNode("日历记事本"); for ( i=1;i<=12 ;i++ ) { month[i]=new DefaultMutableTreeNode(""+i+"月"); root.add(month[i]); } for (i=1; i<=12;i++ ) { if (i==1||i==3||i==5||i==7||i==8||i==10||i==12) { for (int j=1;j<=31 ;j++ ) { month[i].add(new DefaultMutableTreeNode(j+"日")); } } else if (i==4||i==6||i==9||i==11) { for (int j=1;j<=30 ;j++ ) { month[i].add(new DefaultMutableTreeNode(j+"日")); } } else { for (int j=1;j<=30 ;j++ ) { month[i].add(new DefaultMutableTreeNode(j+"日")); } } } b_save.addActionListener(this); b_del.addActionListener(this); tree=new JTree(root); JPanel p=new JPanel(); p.setLayout(new BorderLayout()); JScrollPane scrollpane1=new JScrollPane(text); p.add(scrollpane1,BorderLayout.CENTER); JPanel p1=new JPanel(); p1.add(b_save); p1.add(b_del); p.add(p1,BorderLayout.NORTH); JScrollPane scrollpane2=new JScrollPane(tree); JSplitPane splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollpane2,p); tree.addTreeSelectionListener(this); con.add(splitpane); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}}); setVisible(true); setBounds(70,80,200,300); } public void valueChanged(TreeSelectionEvent e) //throws IOException { text.setText(" "); if (e.getSource()==tree) { DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); if (node.isLeaf()) { String str=node.toString(); for (int i=0;i<=12 ;i++ ) { if (node.getParent()==month[i]) { try{ String temp=null; File f=new File(node.getParent().toString()+str+".txt"); FileReader file=new FileReader(f); BufferedReader in=new BufferedReader(file); while ((temp=in.readLine())!=null) { text.append(temp+'\n'); } file.close() /*throws IOException*/; in.close() /*throws IOException*/; } catch(FileNotFoundException ee){} catch(IOException ioe){} } } } } } public void actionPerformed(ActionEvent e) { if (e.getSource()==b_save) { DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); String str=node.toString(); if (node.isLeaf()) { try{ File f=new File(node.getParent().toString()+str+".txt"); FileWriter tofile=new FileWriter(f); BufferedWriter out=new BufferedWriter(tofile); out.write(text.getText(),0,(text.getText()).length()); out.flush(); tofile.close(); out.close(); } catch(FileNotFoundException fe){} catch(IOException ioe){} } } else if (e.getSource()==b_del) { DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); String str=node.toString(); if (node.isLeaf()) { try{ File f=new File(node.getParent().toString()+str+".txt"); FileWriter tofile=new FileWriter("gxy.bat"); BufferedWriter out=new BufferedWriter(tofile); String temp1="del "+f.getAbsoluteFile().toString(); out.write(temp1,0,temp1.length()); out.newLine(); out.flush(); tofile.close(); out.close(); } catch(IOException e1){} } del(); } } void del() { try{ Runtime ce=Runtime.getRuntime(); ce.exec("gxy.bat"); } catch(IOException e1){} }}public class Example25_35{ public static void main(String[] args) { Remember win=new Remember(); win.pack(); }} 楼主的错误确实在循环里,这是你原先的代码://*****************************************temp=in.readLine();while( temp!=null&&(temp.startsWith("=="))){ if ((temp=in.readLine())!=null&&(temp.startsWith(node.toString()))) { while ((temp=in.readLine())!=null&&!(temp.startsWith("end"))) text.append(temp+'\n'); } else { temp=in.readLine(); continue; }}*************************************************//你想,当要读到18号的第一行时,temp.startsWith("=="),就不是“==”了,所以就退出循环直接关闭文件了。以下是修改好的代码,我运行过,应该没什么问题了:import javax.swing.*;import javax.swing.event.*;import javax.swing.tree.*;//import javax.accessibility.*;import java.awt.*;import java.awt.event.*;//import java.beans.*;import java.util.*;import java.io.*;//import java.applet.*;import java.net.*;public class diaryPtest extends JFrame implements TreeSelectionListener,ActionListener{ JTree tree =null; JTextArea text=new JTextArea(); DefaultMutableTreeNode root; JButton saveB=new JButton("save diary"),delB=new JButton("del diary"); File indexF=new File("data/diaryIndex"); String yearD,monthD,dayD; //construct diaryPtest() { String timeD=(new Date()).toString(); yearD =timeD.substring((timeD.length()-4),(timeD.length())); monthD=timeD.substring(4,7); dayD=timeD.substring(8,10); DefaultMutableTreeNode root = new DefaultMutableTreeNode("My diarys"); DefaultMutableTreeNode year = null; DefaultMutableTreeNode month = null; DefaultMutableTreeNode day = null; // open tree data URL url = getClass().getResource("Index.txt"); try { // convert url to buffered string InputStream is = url.openStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader reader = new BufferedReader(isr); // read one line at a time, put into tree String line = reader.readLine(); while(line != null) { char linetype = line.charAt(0); switch(linetype) { case 'y': year = new DefaultMutableTreeNode(line.substring(2)); root.add(year); break; case 'm': if(year != null) { year.add(month = new DefaultMutableTreeNode(line.substring(2))); } break; case 'd': if(month != null) { month.add(day= new DefaultMutableTreeNode(line.substring(2))); } break; default: break; } line = reader.readLine();//read next } } catch (IOException e) {} tree =new JTree(root); tree.addTreeSelectionListener(this); //set panels JPanel diaryP=new JPanel();//panel to hold the editor panel and the text panel diaryP.setLayout(new BorderLayout()); JScrollPane textP=new JScrollPane(text); diaryP.add(textP,BorderLayout.CENTER); JScrollPane treeP=new JScrollPane(tree); JPanel editP=new JPanel();//panel to hold the edit utilitis editP.add(saveB); editP.add(delB); diaryP.add(editP,BorderLayout.NORTH); //add tree panel and text panel to the diary panel JSplitPane p=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,diaryP,treeP); p.setSize(450,350); p.setDividerLocation(400); p.setDividerSize(4); getContentPane().add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(150,50,600,400); setVisible(true); } //-------------------------------------------------------------------------- //add tree selection method public void valueChanged(TreeSelectionEvent e) { text.setText("");//clear the area before read if (e.getSource()==tree) { DefaultMutableTreeNode node =(DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); if (node.isLeaf()) { try { String temp=null;//used to store the lines File f=new File(node.getParent().toString()+".txt");//open the month file:Mar.txt FileReader file =new FileReader(f); BufferedReader in=new BufferedReader(file); //input the file using readline temp=in.readLine(); while(temp!=null) { if(temp.startsWith("==")) { temp=in.readLine(); if (temp!=null&&(temp.startsWith(node.toString()))) { while ((temp=in.readLine())!=null&&!(temp.startsWith("end"))) text.append(temp+'\n'); } } temp=in.readLine(); } file.close(); in.close(); }catch(FileNotFoundException el){} catch (IOException ee){} } } } public void actionPerformed(ActionEvent e){} public static void main(String[] args) { new diaryPtest(); }} 原来如此,真是感激涕零。本质说,是我对while的用法没有深刻的认识。必须苦练基本功!!! 问各位一个问题 关于TableModel的问题 如何将jTable选中行的其中几列显示到另一个jTable中 JList Swing下如何获得按下回车的事件? 怎么样把一个数组写到一个jTable中啊 Java初学者容易混淆的几个问题 关于反射 一个弱智问题 [help] where can i download this book named<core JAVA2> ? 请教:这个小程序该如何写? 我的一个经典问题?
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;class Remember extends JFrame implements TreeSelectionListener,ActionListener
{
JTree tree=null;
JTextArea text=new JTextArea("",20,20);
int i=0;
DefaultMutableTreeNode root;
JButton b_save=new JButton("保存日志"),b_del=new JButton("删除日志");
DefaultMutableTreeNode month[]=new DefaultMutableTreeNode[13];
Remember()
{
Container con=getContentPane();
root=new DefaultMutableTreeNode("日历记事本");
for ( i=1;i<=12 ;i++ )
{
month[i]=new DefaultMutableTreeNode(""+i+"月");
root.add(month[i]);
}
for (i=1; i<=12;i++ )
{
if (i==1||i==3||i==5||i==7||i==8||i==10||i==12)
{
for (int j=1;j<=31 ;j++ )
{
month[i].add(new DefaultMutableTreeNode(j+"日"));
}
}
else if (i==4||i==6||i==9||i==11)
{
for (int j=1;j<=30 ;j++ )
{
month[i].add(new DefaultMutableTreeNode(j+"日"));
}
}
else
{
for (int j=1;j<=30 ;j++ )
{
month[i].add(new DefaultMutableTreeNode(j+"日"));
}
}
}
b_save.addActionListener(this);
b_del.addActionListener(this);
tree=new JTree(root);
JPanel p=new JPanel();
p.setLayout(new BorderLayout());
JScrollPane scrollpane1=new JScrollPane(text);
p.add(scrollpane1,BorderLayout.CENTER);
JPanel p1=new JPanel();
p1.add(b_save);
p1.add(b_del);
p.add(p1,BorderLayout.NORTH);
JScrollPane scrollpane2=new JScrollPane(tree);
JSplitPane splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollpane2,p);
tree.addTreeSelectionListener(this);
con.add(splitpane);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}});
setVisible(true);
setBounds(70,80,200,300);
}
public void valueChanged(TreeSelectionEvent e) //throws IOException
{
text.setText(" ");
if (e.getSource()==tree)
{
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if (node.isLeaf())
{
String str=node.toString();
for (int i=0;i<=12 ;i++ )
{
if (node.getParent()==month[i])
{
try{
String temp=null;
File f=new File(node.getParent().toString()+str+".txt");
FileReader file=new FileReader(f);
BufferedReader in=new BufferedReader(file);
while ((temp=in.readLine())!=null)
{
text.append(temp+'\n');
}
file.close() /*throws IOException*/;
in.close() /*throws IOException*/;
}
catch(FileNotFoundException ee){}
catch(IOException ioe){}
}
}
}
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b_save)
{
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
String str=node.toString();
if (node.isLeaf())
{
try{
File f=new File(node.getParent().toString()+str+".txt");
FileWriter tofile=new FileWriter(f);
BufferedWriter out=new BufferedWriter(tofile);
out.write(text.getText(),0,(text.getText()).length());
out.flush();
tofile.close();
out.close();
}
catch(FileNotFoundException fe){}
catch(IOException ioe){}
}
}
else if (e.getSource()==b_del)
{
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
String str=node.toString();
if (node.isLeaf())
{
try{
File f=new File(node.getParent().toString()+str+".txt");
FileWriter tofile=new FileWriter("gxy.bat");
BufferedWriter out=new BufferedWriter(tofile);
String temp1="del "+f.getAbsoluteFile().toString();
out.write(temp1,0,temp1.length());
out.newLine();
out.flush();
tofile.close();
out.close();
}
catch(IOException e1){}
}
del();
}
}
void del()
{
try{
Runtime ce=Runtime.getRuntime();
ce.exec("gxy.bat");
}
catch(IOException e1){}
}
}
public class Example25_35
{
public static void main(String[] args)
{
Remember win=new Remember();
win.pack();
}
}
//*****************************************
temp=in.readLine();
while( temp!=null&&(temp.startsWith("==")))
{
if ((temp=in.readLine())!=null&&(temp.startsWith(node.toString())))
{
while ((temp=in.readLine())!=null&&!(temp.startsWith("end")))
text.append(temp+'\n');
}
else
{
temp=in.readLine();
continue;
}
}
*************************************************//
你想,当要读到18号的第一行时,temp.startsWith("=="),
就不是“==”了,所以就退出循环直接关闭文件了。
以下是修改好的代码,我运行过,应该没什么问题了:import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
//import javax.accessibility.*;import java.awt.*;
import java.awt.event.*;
//import java.beans.*;
import java.util.*;
import java.io.*;
//import java.applet.*;
import java.net.*;public class diaryPtest extends JFrame implements TreeSelectionListener,ActionListener
{
JTree tree =null;
JTextArea text=new JTextArea();
DefaultMutableTreeNode root;
JButton saveB=new JButton("save diary"),delB=new JButton("del diary");
File indexF=new File("data/diaryIndex");
String yearD,monthD,dayD; //construct
diaryPtest()
{
String timeD=(new Date()).toString();
yearD =timeD.substring((timeD.length()-4),(timeD.length()));
monthD=timeD.substring(4,7);
dayD=timeD.substring(8,10); DefaultMutableTreeNode root = new DefaultMutableTreeNode("My diarys");
DefaultMutableTreeNode year = null;
DefaultMutableTreeNode month = null;
DefaultMutableTreeNode day = null; // open tree data
URL url = getClass().getResource("Index.txt");
try
{
// convert url to buffered string
InputStream is = url.openStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr); // read one line at a time, put into tree
String line = reader.readLine();
while(line != null) {
char linetype = line.charAt(0);
switch(linetype)
{
case 'y':
year = new DefaultMutableTreeNode(line.substring(2));
root.add(year);
break;
case 'm':
if(year != null) {
year.add(month = new DefaultMutableTreeNode(line.substring(2)));
}
break;
case 'd':
if(month != null) {
month.add(day= new DefaultMutableTreeNode(line.substring(2)));
}
break;
default:
break;
}
line = reader.readLine();//read next
}
} catch (IOException e) {} tree =new JTree(root);
tree.addTreeSelectionListener(this);
//set panels
JPanel diaryP=new JPanel();//panel to hold the editor panel and the text panel
diaryP.setLayout(new BorderLayout());
JScrollPane textP=new JScrollPane(text);
diaryP.add(textP,BorderLayout.CENTER); JScrollPane treeP=new JScrollPane(tree);
JPanel editP=new JPanel();//panel to hold the edit utilitis
editP.add(saveB);
editP.add(delB);
diaryP.add(editP,BorderLayout.NORTH);
//add tree panel and text panel to the diary panel
JSplitPane p=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,diaryP,treeP); p.setSize(450,350);
p.setDividerLocation(400);
p.setDividerSize(4); getContentPane().add(p);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(150,50,600,400);
setVisible(true); }
//--------------------------------------------------------------------------
//add tree selection method
public void valueChanged(TreeSelectionEvent e)
{
text.setText("");//clear the area before read
if (e.getSource()==tree)
{
DefaultMutableTreeNode node =(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if (node.isLeaf())
{
try
{
String temp=null;//used to store the lines
File f=new File(node.getParent().toString()+".txt");//open the month file:Mar.txt
FileReader file =new FileReader(f);
BufferedReader in=new BufferedReader(file);
//input the file using readline
temp=in.readLine();
while(temp!=null)
{
if(temp.startsWith("=="))
{
temp=in.readLine();
if (temp!=null&&(temp.startsWith(node.toString())))
{
while ((temp=in.readLine())!=null&&!(temp.startsWith("end")))
text.append(temp+'\n');
}
}
temp=in.readLine();
}
file.close();
in.close();
}catch(FileNotFoundException el){}
catch (IOException ee){}
}
}
}
public void actionPerformed(ActionEvent e){} public static void main(String[] args)
{
new diaryPtest();
}
}