怎么用java实现茎叶图的编写 5
展开全部
你看看下面这个代码,希望对你有帮助
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TreeSelectionDemo implements TreeSelectionListener {
JFrame frame;
JPanel panel;
JLabel label;
JTextArea textArea;
JScrollPane scrollPane1,scrollPane2;
public TreeSelectionDemo(){
frame=new JFrame("TreeSelectionDemo");
Container c=frame.getContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("Java 技术文档");
DefaultMutableTreeNode branch1=new DefaultMutableTreeNode("J2SE");
DefaultMutableTreeNode branch2=new DefaultMutableTreeNode("J2EE");
DefaultMutableTreeNode branch3=new DefaultMutableTreeNode("J2ME");
root.add(branch1);
root.add(branch2);
root.add(branch3);
branch1.add(new DefaultMutableTreeNode("面向对象基础知识"));
branch1.add(new DefaultMutableTreeNode("图形用户界面"));
branch1.add(new DefaultMutableTreeNode("网络与通信"));
branch1.add(new DefaultMutableTreeNode("文件系统"));
branch1.add(new DefaultMutableTreeNode("数据库应用"));
branch2.add(new DefaultMutableTreeNode("面向对象基础知识"));
branch2.add(new DefaultMutableTreeNode("图形用户界面"));
branch2.add(new DefaultMutableTreeNode("网络与通信"));
branch2.add(new DefaultMutableTreeNode("文件系统"));
branch2.add(new DefaultMutableTreeNode("数据库应用"));
branch3.add(new DefaultMutableTreeNode("面向对象基础知识"));
branch3.add(new DefaultMutableTreeNode("图形用户界面"));
branch3.add(new DefaultMutableTreeNode("网络与通信"));
branch3.add(new DefaultMutableTreeNode("文件系统"));
branch3.add(new DefaultMutableTreeNode("数据库应用"));
JTree tree=new JTree(root);
tree.addTreeSelectionListener(this);//为tree对象注册监听器
scrollPane1=new JScrollPane(tree);
label=new JLabel("你选择了:");
textArea=new JTextArea();//创建一个文本域对象
textArea.setLineWrap(true);//设置允许换行
scrollPane2=new JScrollPane(textArea);
panel=new JPanel();//创建一个面板容器对象
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));//将panel对象的布局管理器设置为BoxLayout,并以Y轴为轴方向
panel.add(label);
panel.add(scrollPane2);
c.add(scrollPane1,BorderLayout.WEST);
c.add(panel,BorderLayout.CENTER);
frame.setSize(500,400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){//增加匿名事件适配器类
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void valueChanged(TreeSelectionEvent e) {//处理来自树节点状态改变的事件
// TODO Auto-generated method stub
JTree tree=(JTree)e.getSource();//获得事件源所在对象
TreePath treepath=tree.getSelectionPath();//获得当前选择节点的路径
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();//获得当前选取的节点的对象
String nodePath=treepath.toString();
String nodeName=selectionNode.toString();
if(selectionNode.isLeaf())//判断是否为树叶节点,若是则给予说明,否则仅给出路径
{
textArea.setText(nodePath+"\n其中"+nodeName+"是叶子节点");
}
else textArea.setText(nodePath);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TreeSelectionDemo();
}
}
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TreeSelectionDemo implements TreeSelectionListener {
JFrame frame;
JPanel panel;
JLabel label;
JTextArea textArea;
JScrollPane scrollPane1,scrollPane2;
public TreeSelectionDemo(){
frame=new JFrame("TreeSelectionDemo");
Container c=frame.getContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode("Java 技术文档");
DefaultMutableTreeNode branch1=new DefaultMutableTreeNode("J2SE");
DefaultMutableTreeNode branch2=new DefaultMutableTreeNode("J2EE");
DefaultMutableTreeNode branch3=new DefaultMutableTreeNode("J2ME");
root.add(branch1);
root.add(branch2);
root.add(branch3);
branch1.add(new DefaultMutableTreeNode("面向对象基础知识"));
branch1.add(new DefaultMutableTreeNode("图形用户界面"));
branch1.add(new DefaultMutableTreeNode("网络与通信"));
branch1.add(new DefaultMutableTreeNode("文件系统"));
branch1.add(new DefaultMutableTreeNode("数据库应用"));
branch2.add(new DefaultMutableTreeNode("面向对象基础知识"));
branch2.add(new DefaultMutableTreeNode("图形用户界面"));
branch2.add(new DefaultMutableTreeNode("网络与通信"));
branch2.add(new DefaultMutableTreeNode("文件系统"));
branch2.add(new DefaultMutableTreeNode("数据库应用"));
branch3.add(new DefaultMutableTreeNode("面向对象基础知识"));
branch3.add(new DefaultMutableTreeNode("图形用户界面"));
branch3.add(new DefaultMutableTreeNode("网络与通信"));
branch3.add(new DefaultMutableTreeNode("文件系统"));
branch3.add(new DefaultMutableTreeNode("数据库应用"));
JTree tree=new JTree(root);
tree.addTreeSelectionListener(this);//为tree对象注册监听器
scrollPane1=new JScrollPane(tree);
label=new JLabel("你选择了:");
textArea=new JTextArea();//创建一个文本域对象
textArea.setLineWrap(true);//设置允许换行
scrollPane2=new JScrollPane(textArea);
panel=new JPanel();//创建一个面板容器对象
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));//将panel对象的布局管理器设置为BoxLayout,并以Y轴为轴方向
panel.add(label);
panel.add(scrollPane2);
c.add(scrollPane1,BorderLayout.WEST);
c.add(panel,BorderLayout.CENTER);
frame.setSize(500,400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){//增加匿名事件适配器类
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void valueChanged(TreeSelectionEvent e) {//处理来自树节点状态改变的事件
// TODO Auto-generated method stub
JTree tree=(JTree)e.getSource();//获得事件源所在对象
TreePath treepath=tree.getSelectionPath();//获得当前选择节点的路径
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();//获得当前选取的节点的对象
String nodePath=treepath.toString();
String nodeName=selectionNode.toString();
if(selectionNode.isLeaf())//判断是否为树叶节点,若是则给予说明,否则仅给出路径
{
textArea.setText(nodePath+"\n其中"+nodeName+"是叶子节点");
}
else textArea.setText(nodePath);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TreeSelectionDemo();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询