http://www.csdn.net/expert/topic/85/85391.shtm

解决方案 »

  1.   

    老大
    你的构造子写错了
    public maincanvas(){
    }
    没有返回值
    还有,类名最好大写
    所以
    public class Maincanvas extends Canvas{
     public Maincanvas(){
     }
    }
      

  2.   

    老兄!你的路径设置对了吗?包com.stock.core.graphics必须在搜索路径或当前路径下!
      

  3.   

    写了也不行
    在定义包的时候先为空包。com.stock.core.graphics为后加的 ,加完后,编译有警告,不加没有
      

  4.   

    com下定义了另一个包来应用此类,装载时能看见包import 下有包,但是包的下面没类;
      

  5.   

    这是定义的类,先测试一下
    package com.stock.core.graphics;import java.awt.*;
    import java.io.*;public class MainCanvas extends InterfacePoint{
      Canvas canvas;
      Rectangle rectangle;
      public MainCanvas(){       // Canvas canvas
        this.canvas=canvas;
      }  public void paint(Graphics g){
        if(rectangle == null){
          rectangle = new Rectangle();
        }
        drawFace(g);
        drawK(17,32,40,8,40,g);
      }
      public void drawK(int fdcost,int edcost,int hcost,int lcost,int time,
                          Graphics g){
        int _fdpoint = switchCostPoint(fdcost);
        int _edpoint = switchCostPoint(edcost);
        int _hpoint  = switchCostPoint(hcost);
        int _lpoint  = switchCostPoint(lcost);
        int _time    = switchTimePoint(time);
        g.setColor(Color.red);
        if(rectangle == null){
          rectangle = new Rectangle();
        }
        if(fdcost > edcost){
          g.setColor(Color.red);
          g.fillRect(_time-1,_fdpoint,4,_edpoint-_fdpoint);
        }
        else{
          g.drawRect(_time-1,_edpoint,4,_fdpoint-_edpoint);
        }    if(fdcost > edcost){
          g.setColor(Color.red);
          g.drawLine(_time+1,_hpoint,_time+1,_fdpoint);
          g.drawLine(_time+1,_lpoint,_time+1,_edpoint);
        }
        else{
          g.setColor(Color.red);
          g.drawLine(_time+1,_hpoint,_time+1,_edpoint);
          g.drawLine(_time+1,_lpoint,_time+1,_fdpoint);
        }
      }
      /*
       *switch the cost value to point value.
       *@param cost value.
       *@param return the point value in the interface.
       */
      public int switchCostPoint(int cost){    int _maxHPoint = getMaxHPoint()[1];
        int _maxCost = 100;       //getMaxCost();
        int _minCost = 0;       //getMinCost();
        int _base = getCostBasePoint()[1];
        int _temp;
        int _lastPoint;    _temp = (_base-_maxHPoint) *cost/(_maxCost-_minCost);
        _lastPoint = _base-_temp;
        return _lastPoint;
      }
      /*
       *switch the time value to point value.
       *@param time value.
       *@param return the point value in the interface.
       */
      public int switchTimePoint(int time){
        int _lastPoint;
        int _maxWPoint = getMaxWPoint()[0];//size().width;
        int _minWPoint = getTranQBasePoint()[0];
        int _maxTime = 300;      //getMaxTime();
        int _minTime = 0;        //getMinTime();    _lastPoint = (_maxWPoint-_minWPoint)*(time-_minTime)/(_maxTime-_minTime);    return _lastPoint;
      }
      /*
       *draw the interface, give the sign for ing the K line,translation
       *qitity,ave line.
       */
      public void drawFace(Graphics g){    //int _maxTime = getMaxTime();
        //int _minTime = getMintime();    int _maxHPoint = 300;//size().height;
        int _maxWPoint = 400;//size().width;    int[] tranQBasePoint = {_maxWPoint/10,(_maxHPoint-_maxHPoint/10)};
        int[] maxHPoint = {_maxWPoint/10,_maxHPoint/15};
        int[] maxWPoint = {_maxWPoint-_maxWPoint/15,(_maxHPoint-_maxHPoint/10)};
        int[] costBasePoint = {_maxWPoint/10,(tranQBasePoint[1]-(tranQBasePoint[1]-maxHPoint[1])/4)};    setTranQBasePoint(tranQBasePoint);
        setCostBasePoint(costBasePoint);
        setMaxHPoint(maxHPoint);
        setMaxWPoint(maxWPoint);
        // draw the Graphics Frame
        g.drawLine(maxHPoint[0],maxHPoint[1],tranQBasePoint[0],tranQBasePoint[1]);
        g.drawLine(maxWPoint[0],maxWPoint[1],tranQBasePoint[0],tranQBasePoint[1]);
        g.drawLine(costBasePoint[0],costBasePoint[1],maxWPoint[0],costBasePoint[1]);
        /*
         *draw the transfation Qty unit,transfation cost unit, transfation time unit.
         */
        /*
        int _maxCost = getMaxCost();
        int _minCost = getMinCost();
        int _tempCost = (_maxCost-_minCost)/5
        for(int i=1;i<6;i++){
          g.drawLine(tranQBasePoint[0],costBasePoint-i*_tempCost,maxWPoint[0],costBasePoint-i*_tempCost);
          write the transfation cost
        }
        int _maxTranQty = getMaxTranQty();
        int _minTranQty = getMinTranQty();
        int (_tempTranQty = _maxTranQty-_minTranQty)/5;
        for(int i=1;i<6;i++){
          g.drawLine(tranQBasePoint[0],costBasePoint-i*_tempTranQty,maxWPoint[0],costBasePoint-i*_tempTranQty);
          write the transfation Qty
        }
        */
      }
    }
    /*
     *define the interface point,applying at drawing the K line,ave line .
     */
    class InterfacePoint extends Canvas{
      int[] tranQBasePoint;
      int[] maxHPoint;
      int[] maxWPoint;
      int[] costBasePoint;  public void setTranQBasePoint(int[] point){
        tranQBasePoint = point;
      }
      public void setMaxHPoint(int[] point){
        maxHPoint = point;
      }
      public void setMaxWPoint(int[] point){
        maxWPoint = point;
      }
      public void setCostBasePoint(int[] point){
        costBasePoint = point;
      }  public int[] getTranQBasePoint(){
        return tranQBasePoint;
      }
      public int[] getMaxHPoint(){
        return maxHPoint;
      }
      public int[] getMaxWPoint(){
        return maxWPoint;
      }
      public int[] getCostBasePoint(){
        return costBasePoint;
      }
    }这是引用的类
    package SplitPane;import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.text.*;
    import javax.swing.table.*;
    import java.io.*;//import com.stock.core.graphics.*;
    public class SplitPane
    {  public static void main(String[] args)
       {  JFrame frame = new SplitPaneFrame();
          try{
          frame.show(); }catch(Exception ex) {System.out.println(ex);}
       }
    }class SplitPaneFrame extends JFrame
      implements ListSelectionListener,ActionListener{//,ChangeListener{
      public SplitPaneFrame(){
        setTitle("SplitPaneTest");
        setSize(600, 400);
        addWindowListener(new WindowAdapter(){
          public void windowClosing(WindowEvent e){
            System.exit(0);
          }
        } );    // set up components for planet names, images, descriptions    planetList = new JList(planets);
        planetList.addListSelectionListener(this);    planetImage = new JLabel();      //image
        description = new JTextArea();
        canvas      = new Canvas();
        displayNum = new TextField(20);
        table = new JTable(cells,columnNames);    //invest table
        JScrollPane scrollPane = new JScrollPane(table);
        ListSelectionModel selectionModel = table.getSelectionModel();
        selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        selectionModel.addListSelectionListener(new ListSelectionListener(){
                public void valueChanged(ListSelectionEvent e){
                  int[] selectRow = table.getSelectedRows();
                  String selectData = null;              for(int i=0;i<selectRow.length;i++){
                    selectData = String.valueOf(table.getValueAt(selectRow[i],0));
                  }
                  displayNum.setText(selectData);
                  drawk.MainCanvas(canvas);   // drawk.
               }
        });
        // invest module
        JSplitPane innerPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                                        scrollPane,planetImage);      
        JSplitPane unionPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                                        innerPane,canvas);     //description
        innerPane.setContinuousLayout(true);
        innerPane.setOneTouchExpandable(true);
        unionPane.setContinuousLayout(true);
        unionPane.setOneTouchExpandable(true);
        JSplitPane outerPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                                     planetList,unionPane);
        outerPane.setContinuousLayout(true);    getContentPane().add(outerPane, "Center");    getContentPane().add(displayNum,"North");    JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);    JMenu selectionMenu = new JMenu("selection");
        menuBar.add(selectionMenu);    rowsItem = new JCheckBoxMenuItem("Rows");
        rowsItem.setSelected(table.getRowSelectionAllowed());
        rowsItem.addActionListener(this);
        selectionMenu.add(rowsItem);    columnsItem = new JCheckBoxMenuItem("Columns");
        columnsItem.setSelected(table.getColumnSelectionAllowed());
        columnsItem.addActionListener(this);
        selectionMenu.add(columnsItem);    cellsItem = new JCheckBoxMenuItem("Cells");
        cellsItem.setSelected(table.getCellSelectionEnabled());
        cellsItem.addActionListener(this);
        selectionMenu.add(cellsItem);    JMenu tableMenu = new JMenu("edit");
        menuBar.add(tableMenu);    showColumnsItem = new JMenuItem("show columns");
        showColumnsItem.addActionListener(this);
        tableMenu.add(showColumnsItem);    hideColumnsItem = new JMenuItem("hide columns");
        hideColumnsItem.addActionListener(this);
        tableMenu.add(hideColumnsItem);    addRowItem = new JMenuItem("add row");
        addRowItem.addActionListener(this);
        tableMenu.add(addRowItem);    removeRowsItem = new JMenuItem("remove row");
        removeRowsItem.addActionListener(this);
        tableMenu.add(removeRowsItem);    clearCellsItem = new JMenuItem("clear cells");
        clearCellsItem.addActionListener(this);
        tableMenu.add(clearCellsItem);  }  public void valueChanged(ListSelectionEvent event){
        JList source = (JList)event.getSource();
        Planet value = (Planet)source.getSelectedValue();
            // update image and description
        planetImage.setIcon(value.getImage());
        description.setText(value.getDescription());
      }   //add action listener     -------------------------------------------------------
      public void actionPerformed(ActionEvent event){
        Object source = event.getSource();
        if(source == rowsItem){
          table.setRowSelectionAllowed(rowsItem.isSelected());
          table.clearSelection();
        }
        else if(source == columnsItem){
          table.setColumnSelectionAllowed(columnsItem.isSelected());
          table.clearSelection();
        }
        else if(source == hideColumnsItem){
          int[] selected = table.getSelectedColumns();
          TableColumnModel columnModel = table.getColumnModel();
           //rremove the columns
          for(int i=selected.length - 1;i>=0;i--){
            TableColumn column= columnModel.getColumn(selected[i]);
            table.removeColumn(column);
            removedColumns.add(column);
          }
        }
        else if(source == showColumnsItem){
          for(int i=0;i<removedColumns.size();i++)
            table.addColumn((TableColumn)removedColumns.get(i));
          removedColumns.clear();
        }
        else if(source == removeRowsItem){
          int[] selected = table.getSelectedRows();
          for(int i= selected.length-1;i>=0;i--)
             //remove row
            model.removeRow(selected[i]);
        }
        else if(source == addRowItem){
          Integer[] newCells = new Integer[model.getColumnCount()];
          for(int i=0;i<newCells.length;i++)
            newCells[i]= new Integer((i+1)*(model.getRowCount()+1));
           //add row
          model.addRow(newCells);
        }
        else if(source == clearCellsItem){
          for(int i=0;i<table.getRowCount();i++)
            for(int j=0;j<table.getColumnCount();j++)
              if(table.isCellSelected(i,j))
                table.setValueAt(new Integer(0),i,j);
        }
      }
              //MainCanvas
      private MainCanvas drawk;
      private Canvas canvas;
      private JTable table;  //
      private JList planetList;
      private JLabel planetImage;
      private JTextArea description;
      private TextField displayNum;
      private DefaultTableModel model; //  private JMenuItem showColumnsItem;
      private JMenuItem hideColumnsItem;
      private JMenuItem addRowItem;
      private JMenuItem removeRowsItem;
      private JMenuItem clearCellsItem;  private JCheckBoxMenuItem rowsItem;
      private JCheckBoxMenuItem columnsItem;
      private JCheckBoxMenuItem cellsItem;  private ArrayList removedColumns = new ArrayList();  private Planet[] planets = {
             new Planet("Mercury", 2440, 0),
             new Planet("Venus", 6052, 0),
             new Planet("Earth", 6378, 1),
             new Planet("Mars", 3397, 2),
             new Planet("Jupiter", 71492, 16),
             new Planet("Saturn", 60268, 18),
             new Planet("Uranus", 25559, 17),
             new Planet("Neptune", 24766, 8),
             new Planet("Pluto", 1137, 1),
      };
      private Object[][] cells = {      //table data
        {"Mercury",new Double(2440),new Integer(0),Boolean.FALSE,new String("yellow")},
        {"Venus",new Double(6052),new Integer(0),Boolean.FALSE,new String("yellow")},
        {"Earth",new Double(6378),new Integer(1),Boolean.FALSE,new String("blue")},
        {"Mars",new Double(3397),new Integer(2),Boolean.FALSE,new String("red")},
        {"Jupiter",new Double(71492),new Integer(16),Boolean.TRUE,new String("orange")},
        {"Saturn",new Double(60268),new Integer(18),Boolean.TRUE,new String("orange")},
        {"Uranus",new Double(25559),new Integer(17),Boolean.TRUE,new String("blue")},
        {"Neptune",new Double(24766),new Integer(8),Boolean.TRUE,new String("blue")},
        {"Pluto",new Double(1137),new Integer(1),Boolean.FALSE,new String("blue")},
      };
      //table header
      private String[] columnNames = {"Planet","Radius","Moon","Caseous","Color"};
    }class Planet
    {  public Planet(String n, double r, int m)
       {  name = n;
          radius = r;
          moons = m;
          image = new ImageIcon(name + ".gif");
       }   public String toString()
       {  return name;
       }   public String getDescription()
       {  return "Radius: " + radius + "\nMoons: " + moons + "\n";
       }   public ImageIcon getImage()
       {  return image;
       }   private String name;
       private double radius;
       private int moons;
       private ImageIcon image;
    }
      

  6.   

    你帮测试一下。我的email;[email protected]
      

  7.   

    com.stock.core.graphics中的com必须用大写COM.-----,你在试试,别的地方我认为没有问题.
      

  8.   

    首先,上楼的老兄的意见是绝对不正确的。从Java2开始已经建议包名全部用小写了,你说的用大写已经是deprecated了。:)其次,我怀疑你对CLASSPATH的理解还是有问题,比如你定义的包com.stock.core.graphics,如果你在CLASSPATH中定义如下: set classpath=c:\jdk1.3\lib\dt.jar;c:\jdk1.3\lib\tools.jar;c:\myclasses;.
    那么你的包中的.class应该放在这样的目录结构下: c:\myclasses\com\stock\core\graphics\或
    .\com\stock\core\graphics\,这样才能找到你的包中的类,
    还有一种方法就是把你的.class通通打包到.jar文件中去,然后设CLASSPATH如下:
    set classpath=%CLASSPATH%;c:\myclasses\mypack.jar
    这样也可以找到你的类,不过要特别注意:在.jar文件中也必须是/com/stock/core/graphics/目录下才行!也就是说,你生成mypack.jar文件时必须用 -d 的选项把这个目录结构一起打包进去才行。