当前位置: 代码迷 >> Java相关 >> 树组 输出问题 ,求大侠帮助啊
  详细解决方案

树组 输出问题 ,求大侠帮助啊

热度:166   发布时间:2012-11-24 14:17:04.0
树组 输出问题 ,求大侠帮助啊
。。。。数组 名字和得分输不出来,不知道哪里错了 ,请求指点
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import javax.swing.event.*;

public class Tree {
    public static void main(String args[]){
        new TreeWin();
        }
    }
class Student{
    String name;
    double score;
    //doube MajorSpeciaty;
    Student(String name,double score){
    this.name=name;
    this.score=score;
    }
    public String toString(){
    return name;
    }   
   
abstract class TreeWin extends JFrame implements TreeSelectionListener{
    JTree tree;
    JTextArea showText;
    TreeWin(){
        DefaultMutableTreeNode root=new DefaultMutableTreeNode("华北科技学院");
        DefaultMutableTreeNode node=new DefaultMutableTreeNode("信息与计算科学");
        DefaultMutableTreeNode nodeson1=new DefaultMutableTreeNode(new Student("三",98));
        DefaultMutableTreeNode nodeson2=new DefaultMutableTreeNode(new Student("四",88));
        DefaultMutableTreeNode nodeson3=new DefaultMutableTreeNode(new Student("五",78));
        root.add(node);
        node.add(nodeson1);
        node.add(nodeson2);
        node.add(nodeson3);
        tree=new JTree(root);
        tree.addTreeSelectionListener(this);
        showText=new JTextArea();
        setLayout(new GridLayout(1,2));
        add(new JScrollPane(tree));
        add(new JScrollPane(showText));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setBounds(350,400,300,300);
        validate();
        }
        public void valueChaged(TreeSelectionEvent e){
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
            if(node.isLeaf()){
                Student s=(Student)node.getUserObject();
                showText.append(s.name+","+s.score+"."+"\n");
            }
            else {
            showText.setText(null);
            }
        }
    }
}
搜索更多相关的解决方案: double  abstract  public  return  

----------------解决方案--------------------------------------------------------
我就不知道你的程序是怎么跑起来的。。。。。。。。。。。。
new 一个抽象的类,肯定是要报错的..
我刚刚修改了一下,
package gfgfg;

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import javax.swing.event.*;

class Student{
    String name;
    double score;
    //doube MajorSpeciaty;
    Student(String name,double score){
    this.name=name;
    this.score=score;
    }
    public String toString(){
    return name;
    }   
}
public  class TreeWin extends JFrame implements TreeSelectionListener{
    JTree tree;
    JTextArea showText;
    TreeWin(){
        DefaultMutableTreeNode root=new DefaultMutableTreeNode("华北科技学院");
        DefaultMutableTreeNode node=new DefaultMutableTreeNode("信息与计算科学");
        DefaultMutableTreeNode nodeson1=new DefaultMutableTreeNode(new Student("三",98));
        DefaultMutableTreeNode nodeson2=new DefaultMutableTreeNode(new Student("四",88));
        DefaultMutableTreeNode nodeson3=new DefaultMutableTreeNode(new Student("五",78));
        root.add(node);
        node.add(nodeson1);
        node.add(nodeson2);
        node.add(nodeson3);
        tree=new JTree(root);
        tree.addTreeSelectionListener(this);
        showText=new JTextArea();
        setLayout(new GridLayout(1,2));
        add(new JScrollPane(tree));
        add(new JScrollPane(showText));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setBounds(350,400,300,300);
        validate();
        }
        @Override
        public void valueChanged(TreeSelectionEvent e) {
             DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
                if(node.isLeaf()){
                    Student s=(Student)node.getUserObject();
                    showText.append(s.name+","+s.score+"."+"\n");
                }
                else {
                showText.setText(null);
                }
            
        }
   
    public static void main(String[]args){
            new TreeWin();
        }
    }



----------------解决方案--------------------------------------------------------
谢谢你 ,终与可以执行了,我头都想爆了了 呵呵

----------------解决方案--------------------------------------------------------
谢谢你!要不是你我不知道还有多久才能弄出来
我还有些不明白 :你再帮我看一下好不?  在下感激不进
刚开始老报这个错误:
E:JAVAtry>javac TreeWin.java
TreeWin.java:19: TreeWin 不是抽象的,并且未覆盖 javax.swing.event.TreeSelectionL
istener 中的抽象方法 valueChanged(javax.swing.event.TreeSelectionEvent)
public class TreeWin extends JFrame implements TreeSelectionListener{
后来无赖我就在class TreeWin 前加了个抽象类关键字。

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import javax.swing.event.*;

class Student{
    String name;
    double score;
    Student(String name,double score){
    this.name=name;
    this.score=score;
    }
    public String toString(){
    return name;
    }   
}

public class TreeWin extends JFrame implements TreeSelectionListener{
    JTree tree=null;
    JTextArea showText;
    TreeWin(){
        DefaultMutableTreeNode root=new DefaultMutableTreeNode("华北科技学院");
        DefaultMutableTreeNode node=new DefaultMutableTreeNode("信息与计算科学");
        DefaultMutableTreeNode nodeson1=new DefaultMutableTreeNode(new Student("三",98));
        DefaultMutableTreeNode nodeson2=new DefaultMutableTreeNode(new Student("四",88));
        DefaultMutableTreeNode nodeson3=new DefaultMutableTreeNode(new Student("五",78));
        root.add(node);
        node.add(nodeson1);
        node.add(nodeson2);
        node.add(nodeson3);
        tree=new JTree(root);
        tree.addTreeSelectionListener(this);
        showText=new JTextArea();
        setLayout(new GridLayout(1,2));
        add(new JScrollPane(tree));
        add(new JScrollPane(showText));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setBounds(350,400,300,300);
        validate();
        }
   
        public void valueChaged(TreeSelectionEvent e){
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
            if(node.isLeaf()){
                Student s=(Student)node.getUserObject();
                showText.append(s.name+","+s.score+"."+"n");
            }
            else {
            showText.setText(null);
            }
        }
   

    public static void main(String args[]){
        new TreeWin();
        }
}

----------------解决方案--------------------------------------------------------
你用一个类A继承另一个类B,A必须实现B中的抽象方法。。。不然的话,A也必须为抽象的.......所以你会报那个错。
你在main方法中,必须new一个非抽象类,new一个抽象类是没有意义的。。。
一般情况就是new一个抽象类的子类。。。
----------------解决方案--------------------------------------------------------
好像明白一些了
谢谢你了!
----------------解决方案--------------------------------------------------------
  相关解决方案