树组件首先要new一个JTree,再加结点,然后添加到 JScrollPane
JTree tree1=new JTree(); //.......添加节点 add(new ScrollPane(tree1)
添加节点
DefaultMutableTreeNode root=new DefaultMutableTreeNode("dongxi")//后面的是备注 DefaultMutableTreeNode root=new DefaultMutableTreeNode(new Good("aa",11));//还可以作为叶子new 对象
添加监视器(由树来添加
addTreeSelectionListener(TreeSelectionListener listener)
TreeSelectionListener接口的方法
public void valueChanged(TreeSelectionEvent e)
在上面的接口方法中,返回树的结点,再返回结点中的对象
DefaultMutableTreeNode node1=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();//只有树Tree才能用的方法 if(node.isLeaf())//判断是否叶子节点 Obeject a=(Obeject)node.getUserObject()//返回叶子里面的对象
完整的测试代码
public class test{ public static void main(String args[]){ MyWin window1=new MyWin(); window1.setBounds(12,12,400,300); } } class MyWin extends JFrame implements TreeSelectionListener{ JTextField text1; JButton button1,button2; JTextArea textArea1,textArea2; JLabel label1; JTree tree1; MyWin(){ init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init(){ DefaultMutableTreeNode root=new DefaultMutableTreeNode("Goods"); DefaultMutableTreeNode nodeTV=new DefaultMutableTreeNode("TV"); DefaultMutableTreeNode nodePhone=new DefaultMutableTreeNode("phone"); DefaultMutableTreeNode tv1=new DefaultMutableTreeNode(new Goods("huangTV",11)); DefaultMutableTreeNode tv2=new DefaultMutableTreeNode(new Goods("yuTV",21)); DefaultMutableTreeNode phone1=new DefaultMutableTreeNode(new Goods("ga",31)); DefaultMutableTreeNode phone2=new DefaultMutableTreeNode(new Goods("bing",11)); root.add(nodeTV); root.add(nodePhone); nodeTV.add(tv1); nodeTV.add(tv2); nodePhone.add(phone1); nodePhone.add(phone2); tree1=new JTree(root); tree1.addTreeSelectionListener(this); setLayout(new GridLayout(1,2)); add(new JScrollPane(tree1)); textArea2=new JTextArea(15,15); add(new JScrollPane(textArea2)); } public void valueChanged(TreeSelectionEvent e){ DefaultMutableTreeNode a=(DefaultMutableTreeNode)tree1.getLastSelectedPathComponent(); if(a.isLeaf()){//是叶子节点? Goods b=(Goods)a.getUserObject(); textArea2.append(b.name+": "+b.price+" "); } else textArea2.setText(null); } } class Goods{ String name; double price; Goods(String name,double price){ this.name=name; this.price=price; } }
表格组件
Jtable默认的构造方法有7个,这里说常用的3个
JTable() JTable(int a,int b)//几行几列 JTable(Object data[][],Object columnName[])//data为数据数组,columnName为列名数组
如果表格data[][]是 数字类型,可以用 Double.parseDouble 或者Integer.parseInt(s);
调用repaint来刷新表格
计算成绩代码测试
class MyWin extends JFrame implements ActionListener{ JTable table1; JButton button1; Object a[][]; Object name[]={"姓名","英语成绩","数学成绩","总成绩"}; MyWin(){ init(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } void init(){ a=new Object[8][4]; for(int i=0;i<8;i++){ for(int j=0;j<4;j++){ if(j!=0) a[i][j]=0; else a[i][j]="姓名"; } } button1=new JButton("click"); table1=new JTable(a,name); Container con1=this.getContentPane();//获得一个容器,不用add getContentPane().add(new JScrollPane(table1), BorderLayout.CENTER); con1.add(new JLabel("update the data and click the button"),BorderLayout.NORTH); con1.add(button1,BorderLayout.SOUTH); button1.addActionListener(this); // validate();//验证组件 } public void actionPerformed(ActionEvent e) { double sum=0; boolean boo=true; for(int i=0;i<8;i++){ sum=0; for(int j=1;j<3;j++){ try{sum+=Double.parseDouble(a[i][j].toString()); } catch(Exception ee){ boo=false; table1.repaint(); } } if(boo==true){ a[i][3]=sum+"";//把他改为String table1.repaint(); } } } }