Swing程序题:用JTree创建一个节点树,当鼠标点节点时会弹出一个框来显示当前子节点的信息。
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
/**
*
* @author 逝水
*
*/
public class JTreeDemo extends JFrame implements TreeSelectionListener{
private static final long serialVersionUID = -6209666185329417356L;
//树组件
protected JTree jTree ;
public JTreeDemo(){
init();
}
//初始化
private void init() {
jTree = new JTree();
//树节点的相关数据
DefaultMutableTreeNode root = new DefaultMutableTreeNode("三国名将");
DefaultMutableTreeNode shu = new DefaultMutableTreeNode("蜀国");
DefaultMutableTreeNode wei = new DefaultMutableTreeNode("魏国");
DefaultMutableTreeNode wu = new DefaultMutableTreeNode("吴国");
shu.add(new DefaultMutableTreeNode("关羽"));
shu.add(new DefaultMutableTreeNode("张飞"));
shu.add(new DefaultMutableTreeNode("赵云"));
shu.add(new DefaultMutableTreeNode("马超"));
shu.add(new DefaultMutableTreeNode("黄忠"));
wei.add(new DefaultMutableTreeNode("张辽"));
wei.add(new DefaultMutableTreeNode("典韦"));
wei.add(new DefaultMutableTreeNode("夏侯惇"));
wei.add(new DefaultMutableTreeNode("许褚"));
wei.add(new DefaultMutableTreeNode("徐晃"));
wu.add(new DefaultMutableTreeNode("太史慈"));
wu.add(new DefaultMutableTreeNode("甘宁"));
wu.add(new DefaultMutableTreeNode("孙策"));
wu.add(new DefaultMutableTreeNode("周瑜"));
wu.add(new DefaultMutableTreeNode("陆逊"));
root.add(shu);
root.add(wei);
root.add(wu);
//树的数据模型
DefaultTreeModel model = new DefaultTreeModel(root);
//设置数据模型
jTree.setModel(model);
// 展开所有树
for (int i = 0; i < jTree.getRowCount(); i++)
jTree.expandRow(i);
//添加事件
jTree.addTreeSelectionListener(this);
//滚动面板
JScrollPane jScrollPane = new JScrollPane(jTree,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//添加树到滚动面板
jScrollPane.getViewport().add(jTree);
//添加滚动面板到窗口中
this.getContentPane().add(jScrollPane);
this.setTitle("JTree的事件例子");
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
//Windows风格
//String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
//Windows Classic风格
//String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
//系统当前风格
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(lookAndFeel);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
JTreeDemo demo = new JTreeDemo();
demo.setVisible(true);
}
@Override
public void valueChanged(TreeSelectionEvent e) {
//获取选择的节点
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree
.getLastSelectedPathComponent();
if(node.getLevel() == 0){
//显示提示信息
JOptionPane.showMessageDialog(null,
node.getUserObject()+": 共"+node.getChildCount()+"个国家");
}else if(node.getLevel() == 1){
//显示提示信息
JOptionPane.showMessageDialog(null,
node.getUserObject()+": 共"+node.getChildCount()+"名名将");
}else if(node.getLevel() == 2){
//显示提示信息
JOptionPane.showMessageDialog(null, node.getParent()+"名将: "+
node.getUserObject());
}
}
}
觉得第二个更接近提问者想要的,如果能变点节点选择后,再出对话框就更好了。
import java.awt.GridLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
public class TreeDemo1 extends JFrame implements TreeSelectionListener {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTree jTree = null;
private JEditorPane jEditorPane = null;
private JScrollPane jScrollPane = null;
public JScrollPane getjScrollPane() {
if(jScrollPane==null){
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTree());
}
return jScrollPane;
}
DefaultMutableTreeNode top = null; // @jve:decl-index=0:
/**
* This is the default constructor
*/
public TreeDemo1() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(438, 309);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(1);
gridLayout.setColumns(2);
jContentPane = new JPanel();
jContentPane.setLayout(gridLayout);
jContentPane.add(getjScrollPane(), null);
jContentPane.add(getJEditorPane(), null);
}
return jContentPane;
}
/**
* This method initializes jTree
*
* @return javax.swing.JTree
*/
private JTree getJTree() {
if (jTree == null) {
top = new DefaultMutableTreeNode("黄山");
createNodes(top);
jTree = new JTree(top);
jTree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
jTree.addTreeSelectionListener(this);
}
return jTree;
}
private void createNodes(DefaultMutableTreeNode top) {
DefaultMutableTreeNode jingdian = null, jiaotong = null, zhusu = null, binguan = null, luying = null, jiesu = null;
jingdian = new DefaultMutableTreeNode("景点");
jingdian.add(new DefaultMutableTreeNode("景点1"));
jingdian.add(new DefaultMutableTreeNode("景点2"));
jiaotong = new DefaultMutableTreeNode("交通");
jiaotong.add(new DefaultMutableTreeNode("路线1"));
jiaotong.add(new DefaultMutableTreeNode("路线2"));
jiaotong.add(new DefaultMutableTreeNode("路线3"));
zhusu = new DefaultMutableTreeNode("住宿");
zhusu.add(new DefaultMutableTreeNode("A住宿"));
zhusu.add(new DefaultMutableTreeNode("B住宿"));
zhusu.add(new DefaultMutableTreeNode("C住宿"));
binguan = new DefaultMutableTreeNode("宾馆");
binguan.add(new DefaultMutableTreeNode("X宾馆"));
binguan.add(new DefaultMutableTreeNode("Y宾馆"));
binguan.add(new DefaultMutableTreeNode("Z宾馆"));
binguan.add(new DefaultMutableTreeNode("XYZ宾馆"));
luying = new DefaultMutableTreeNode("露营");
luying.add(new DefaultMutableTreeNode("方式1"));
luying.add(new DefaultMutableTreeNode("方式2"));
jiesu = new DefaultMutableTreeNode("借宿");
jiesu.add(new DefaultMutableTreeNode("借宿1"));
jiesu.add(new DefaultMutableTreeNode("借宿2"));
jiesu.add(new DefaultMutableTreeNode("借宿3"));
top.add(jingdian);
top.add(jiaotong);
top.add(zhusu);
top.add(binguan);
top.add(luying);
top.add(jiesu);
}
/**
* This method initializes jEditorPane
*
* @return javax.swing.JEditorPane
*/
private JEditorPane getJEditorPane() {
if (jEditorPane == null) {
jEditorPane = new JEditorPane();
}
return jEditorPane;
}
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree
.getLastSelectedPathComponent();
if (node == null)
// Nothing is selected.
return;
if(node.isRoot()){
jEditorPane.setText("黄山的简介");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
e.printStackTrace();
}
new TreeDemo1().setVisible(true);
}
});
}
}