当前位置: 代码迷 >> J2SE >> 【J2SE】简易笔记簿
  详细解决方案

【J2SE】简易笔记簿

热度:77   发布时间:2016-04-23 20:42:44.0
【J2SE】简易笔记本

一个简单的笔记本小程序,熟悉了JTree和DefaultMutableTreeNode类的使用。

程序运行结果图:



代码:

import java.awt.BorderLayout;import java.awt.Container;import java.awt.TextArea;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTree;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.tree.DefaultMutableTreeNode;public class Diary extends JFrame implements ActionListener,		TreeSelectionListener {	public static void main(String[] args) {		try {			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");			//SwingUtilities.updateComponentTreeUI(this);		} catch (ClassNotFoundException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} catch (InstantiationException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} catch (IllegalAccessException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} catch (UnsupportedLookAndFeelException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		new Diary();	}	JTree tree = null;	DefaultMutableTreeNode root = null;	TextArea text = new TextArea(20, 20);	JButton b_save = new JButton("保存日志");	JButton b_delete = new JButton("删除日志");	JPanel p = new JPanel();	JPanel p1 = new JPanel();	JLabel l = new JLabel("  ");	JScrollPane sp;	Container c = this.getContentPane();	DefaultMutableTreeNode[] month = new DefaultMutableTreeNode[13];	Diary() {								root = new DefaultMutableTreeNode("日历记事本");		for (int i = 1; i <= 12; i++) {			month[i] = new DefaultMutableTreeNode(i + "月");			root.add(month[i]);		}		for (int 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 <= 28; j++) {					month[i].add(new DefaultMutableTreeNode(j + "日"));				}			}		}		tree = new JTree(root);		p.setLayout(new BorderLayout());		sp = new JScrollPane(tree);		tree.addTreeSelectionListener(this);		p1.add(b_save);		p1.add(b_delete);		b_save.addActionListener(this);		b_delete.addActionListener(this);		p.add(p1, BorderLayout.NORTH);		p.add(text, BorderLayout.CENTER);		p.add(l, BorderLayout.SOUTH);		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,				true, sp, p);		this.add(splitPane);		this.setTitle("Diary");		this.setBounds(400, 300, 460, 320);		this.setVisible(true);		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	}	@Override	public void valueChanged(TreeSelectionEvent e) {		// TODO Auto-generated method stub		text.setText(null);		if (e.getSource() == tree) {			DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree					.getLastSelectedPathComponent();			try {				if (node.isLeaf()) {					String str = node.toString();					for (int i = 1; i <= 12; i++) {						if (node.getParent() == month[i]) {							String temp = null;							File f = new File(node.getParent().toString() + str									+ ".txt");							try {								FileReader fr = new FileReader(f);								BufferedReader br = new BufferedReader(fr);								while ((temp = br.readLine()) != null) {									text.append(temp + '\n');								}								br.close();								fr.close();							} catch (FileNotFoundException e1) {								} catch (Exception e1) {								e1.printStackTrace();							}						}					}				}			} catch (NullPointerException e1) {							}		}	}	@Override	public void actionPerformed(ActionEvent e) {		// TODO Auto-generated method stub		DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree				.getLastSelectedPathComponent();		if (e.getSource() == b_save) {			try {				if (node.isLeaf()) {					String str = node.toString();					File f = new File(node.getParent().toString() + str + ".txt");					try {						FileWriter fw = new FileWriter(f);						BufferedWriter bw = new BufferedWriter(fw);						bw.write(text.getText(), 0, text.getText().length());						bw.flush();						bw.close();						fw.close();					} catch (IOException e1) {						e1.printStackTrace();					}					l.setText("日志已保存");					new Thread(new Disappear()).start();				}			} catch (NullPointerException e1) {				l.setText("请选择日期");				new Thread(new Disappear()).start();			}		} else if (e.getSource() == b_delete) {			try {				if (node.isLeaf()) {					String str = node.toString();					File f = new File(node.getParent().toString() + str + ".txt");					f.delete();					text.setText(null);					l.setText("日志已删除");					new Thread(new Disappear()).start();				}			} catch (NullPointerException e1) {				l.setText("请选择日志");				new Thread(new Disappear()).start();			}		}	}		class Disappear implements Runnable {		@Override		public void run() {			// TODO Auto-generated method stub			try {				Thread.sleep(1000);			} catch (InterruptedException e) {				e.printStackTrace();			}			l.setText(" ");		}			}}