当前位置:编程学习 > JAVA >>

java如何实现下面的界面?

麻烦您源代码 写出来 谢谢啊!

答案:这个是我以前练习时写的,希望对你有帮助


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;

public class JTreeTest extends JFrame {
private Vector<Vector<String>> rowJTableData;
private Vector<String> columnJTableName;
private DefaultTreeModel treeModel;
private Hashtable<String,Object> jtreeData;
private JSplitPane splitPanel;
private JTable jtable;
private JScrollPane scrollTablePanel;
private JScrollPane scrollTreePanel;
private JPanel treePanel;
private JPanel tablePanel;
private JTree jtree;
public JTreeTest()
{
initComponent();
setComponentListener();
this.setMinimumSize(new Dimension(800,600));
this.setLocationRelativeTo(null);
}
private void initComponent()
{
Container content=this.getContentPane();
jtable=new JTable(initJTableRowData(),initJTableColumnName());
scrollTablePanel=new JScrollPane(jtable,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
tablePanel=new JPanel();
tablePanel.setLayout(new BorderLayout());
tablePanel.add(scrollTablePanel);

jtree=new JTree(testData());
treePanel=new JPanel();
treePanel.setLayout(new BorderLayout());
scrollTreePanel=new JScrollPane(jtree,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
treePanel.add(scrollTreePanel);
splitPanel=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT ,true,treePanel,tablePanel);
splitPanel.setDividerLocation(200);//设置分割比例
splitPanel.setOneTouchExpandable(true);//设置是否有展开或收缩箭头
splitPanel.setDividerSize(10);//设置分隔条大小

DefaultTreeCellRenderer applyTreeCellRenderer = new DefaultTreeCellRenderer() {
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded,
leaf, row, hasFocus);

DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
Object userObj = treeNode.getUserObject();

if (userObj instanceof Hashtable) {
this.setIcon(null);
this.setText((String) ((Hashtable) userObj).get("name"));
}
return this;
}
};
jtree.setCellRenderer(applyTreeCellRenderer);
content.add(splitPanel);
}
private void setComponentListener()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/*
private Hashtable<String,Object> initJTreeData()
{
String[] s1={"蔬菜食品","肉食品","副食品"};
String[] s2={"苹果","香蕉","西瓜"};
jtreeData=new Hashtable();
Hashtable ht1=new Hashtable();
Hashtable ht2=new Hashtable();
ht1.put("蔬菜食品", new String[]{"白菜","萝卜","土豆"});
ht1.put("肉食品", new String[]{"牛肉","猪肉","羊肉"});
ht1.put("副食品", ht2);
ht2.put("水果", s2);
jtreeData.put("食品", ht1);
return jtreeData;
}*/
private Vector<Vector<String>> initJTableRowData()
{
rowJTableData=new Vector<Vector<String>>();
Vector<String> columnJTableData=new Vector<String>();
columnJTableData.add("1");
columnJTableData.add("2");
columnJTableData.add("3");
rowJTableData.add(columnJTableData);
return rowJTableData;
}
private void addSubNode(DefaultMutableTreeNode paraent,Vector<Hashtable> nodelist,Vector<Hashtable> list)
{
Hashtable subTable=null;
for(int i=0;i<nodelist.size();i++)
{
subTable=nodelist.get(i);
DefaultMutableTreeNode childNode=new DefaultMutableTreeNode(subTable,true);
paraent.add(childNode);
Vector<Hashtable> subList=getSubNode(subTable,list);
//System.out.println(subList.size()+":"+list.size());
if(subList.size()<1)continue;
addSubNode(childNode,subList,list);
}
}
private DefaultMutableTreeNode initJTreeData(Vector<Hashtable> list)
{
Hashtable table=null;
DefaultMutableTreeNode root=null;
for(int i=0;i<list.size();i++)
{
table=list.get(i);
if(table.get("pid").equals("000"))
{
root=new DefaultMutableTreeNode(table,true);
Vector<Hashtable> subNode=getSubNode(table,list);
addSubNode(root,subNode,list);
}
}
return root;
}

private DefaultMutableTreeNode testData()
{
Vector<Hashtable> list=new Vector<Hashtable>();
Hashtable<String,String> item1=new Hashtable<String,String>();
item1.put("name", "食品");
item1.put("id","001");
item1.put("pid","000");
Hashtable<String,String> item2=new Hashtable<String,String>();
item2.put("name", "蔬菜食品");
item2.put("id", "002");
item2.put("pid", "001");
Hashtable<String,String> item3=new Hashtable<String,String>();
item3.put("name", "肉类食品");
item3.put("id", "003");
item3.put("pid", "001");
Hashtable<String,String> item4=new Hashtable<String,String>();
item4.put("name", "副类食品");
item4.put("id", "004");
item4.put("pid", "001");
Hashtable<String,String> item5=new Hashtable<String,String>();
item5.put("name", "水果");
item5.put("id", "005");
item5.put("pid","004");
Hashtable<String,String> item6=new Hashtable<String,String>();
item6.put("name", "苹果");
item6.put("id", "006");
item6.put("pid","005");
Hashtable<String,String> item7=new Hashtable<String,String>();
item7.put("name", "香蕉");
item7.put("id","007" );
item7.put("pid", "005");
list.add(item1);
list.add(item2);
list.add(item3);
list.add(item4);
list.add(item5);
list.add(item6);
list.add(item7);
return initJTreeData(list);
}

private Vector<Hashtable> getSubNode(Hashtable<String,String> table,Vector<Hashtable> list)
{
String id=table.get("id");
System.out.println(id);
Vector<Hashtable> subNode=new Vector<Hashtable>();
for(int i=0;i<list.size();i++)
{
Hashtable<String,String> temp=list.get(i);
String pid=temp.get("pid");
if(pid.equals(id))
{
System.out.println(temp.get("name"));
subNode.add(temp);
}
}
return subNode;
}

private Vector<String> initJTableColumnName()
{
columnJTableName=new Vector<String>();
columnJTableName.add("test1");
columnJTa

上一个:数组——JAVA程序设计
下一个:java 的关键字有哪些

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,