以下是程序段,我实在找不出哪里出了错,编译老是提示找不到符号,
import java.awt.*;
import javax.swing.*;public class  testpoint 
{
  static JButton button;
  testpoint()
  {
    JFrame jframe=new JFrame();
    button=new JButton();
    jframe.getContentPane().add(button);
    jframe.setSize(200,200);
    button.setSize(50,50);
    jframe.setVisible(true);
  }
public static void main(String[] args) 
{
           Point p=button.getLocationOnScreen();
                  p=new Point(p.x+1,p.y+1);
                  Dimension d=new Dimension(50,50);
Rectangle rec=new Rectangle(p,d);
System.out.println(rec.x+","+rec.y+","+rec.height+","+rec.width);
}
}
提示
不兼容的类型 Point p=button.getLocationOnScreen();
找不到符号!构造函数Point(int x,int y)构造函数Rectangle(Point,java,awt,Dimension)

解决方案 »

  1.   

    getLocationOnScreen 这个函数是jdk 1.5的,你的compiler应该是1.4的。
    还有你的Point p=button.getLocationOnScreen(); 这句button根本就是空的,所以你这句肯定会报null pointer错误。
      

  2.   

    button是空 的吗?我定义了的,主函数里漏了一句testpoint test=new testpoint();另外,我的JDK是最新1.6的!
      

  3.   

    1、1楼和上面所有的答案全不负责。2、
        static JButton button;将button生命为静态的,因此,在main中直接使用没问题。但是,
    public static void main(String[] args) {
    Point p = button.getLocationOnScreen();
    p = new Point(p.x + 1, p.y + 1);
    Dimension d = new Dimension(50, 50);
    Rectangle rec = new Rectangle(p, d);
    System.out.println(rec.x + "," + rec.y + "," + rec.height + ","
    + rec.width);
    }这个main方法中,你根本没使用到你的这个类。也就是说你的这个类的构造函数没被使用,等于说button没被初始化。因此,出现nullpoint在意料之中。3、代码不贴了。4、欢迎楼下的补充!
      

  4.   

    支持楼上,此构造函数完全没有被调用,所有的配置都无效,
     public static void main(String[] args) 
        {         testpoint  tp=new testpoint();  //类名以后别小写了,怪怪的
                  Point p=tp.button.getLocationOnScreen();
                      p=new Point(p.x+1,p.y+1);
                      Dimension d=new Dimension(50,50);
            Rectangle rec=new Rectangle(p,d);
            System.out.println(rec.x+","+rec.y+","+rec.height+","+rec.width);
        }
      

  5.   

    从错误提示来看,八成是你的import的package不对,全删了,然后重新import
    注意Point是java.awt.Point
      

  6.   

    注意我在四楼的补充,构造函数已经实例化了的!而且,编译没报NULLPOintException如果说button还没在屏幕上显示出来,系统无法获取其坐标,倒是有可能。另外,我这段函数只是从我的另外代码中抄过来的,是一段截取应用程序的图形界面,然后实时传输的代码,在图形界面显示后才会调用这个模块,但是一直提示找不到符号······
      

  7.   

    迪亚,我认识你!上次是你给我解决问题的是不?!
    包没引错,一开始我是直接导入整个awt包,编译不过,我又改为引入程序中用到的类,还是不行
      

  8.   


    以下是程序段,我实在找不出哪里出了错,编译老是提示找不到符号,
    import java.awt.*;
    import javax.swing.*;public class  testpoint 
    {
      static JButton button;
      testpoint()
      {
        JFrame jframe=new JFrame();
        button=new JButton();  //2、这里你根本还没有调用
        jframe.getContentPane().add(button);
        jframe.setSize(200,200);
        button.setSize(50,50);
        jframe.setVisible(true);
      }
        public static void main(String[] args) 
        {
                  //3、所以只要你是构造器调用就OK,怎么调用呢?构造器调用就是new这个对象的时候,所以加下面这句:
    testpoint  point = new testpoint();//问题就解决了
                  Point p=button.getLocationOnScreen();//1、这里的button你虽然在属性里面定义了,但是还没来得及初始化,因为你根本还没有调用构造器重的方法对button进行实例化
                      p=new Point(p.x+1,p.y+1);
                      Dimension d=new Dimension(50,50);
            Rectangle rec=new Rectangle(p,d);
            System.out.println(rec.x+","+rec.y+","+rec.height+","+rec.width);
        }
    }楼主根据我标的序号按顺序参考
      

  9.   

    呵呵
    那建议你把error message发上来
    或者把你出错的代码附近的代码贴上来一般这种错误,可能有以下几种原因
    1. source里面包含全角字符
    2. 前面有编译不过的
    3. 包引用不对
    4. 多了或者少了括号啊分号之类的
    5. PS:你上面贴的代码,在你那边可以编译过么?
      

  10.   

    这是出问题的程序段
    /*图像采集发送线程*/
    class imagecapture extends Thread
    {
      BufferedImage image;
      Rectangle rec;
      Dimension dimension;
      Robot robot=new Robot(); 
      static byte imagedata[];
      DatagramPacket datapack;
      Point p;
      DatagramSocket senddatapack;
      ByteArrayOutputStream capturestream;
      InetAddress address;
      /*构造方法*/
      imagecapture()
      {
        dimension=new Dimension(300,500);
    address=InetAddress.getByName(addnetuserpanel.IPtextfield.getText());
    capturestream=new ByteArrayOutputStream();
      }
      public void run()
      {
    while(gamecontrol.gamestate!=0)
    {
        p=jframe.gamecanvas1.getLocationOnScreen();
        p=new Point(p.x+1,p.y+1);
    rec=new Rectangle(p,dimension);
      try{
        image=robot.createScreenCapture(rec);
        ImageIO.write(image,"gif",capturestream);
    imagedata=capturestream.toByteArray();
        datapack=new DatagramPacket(imagedata,imagedata.length,address,1988);
            senddatapack=new DatagramSocket(1988);
            senddatapack.send(datapack);
     }catch(IOException e){}
     try{
     sleep(gamecontrol.time);
     }catch(InterruptedException e){}
     imagedata=null;
     }
      }
       
     
    }下面的代码,也就是上面贴的代码是我为了找出错误,单独写的测试程序。
    import java.awt.*;
    import javax.swing.*;
    public class  testpoint 
    {
      static JButton button;  testpoint()
      {
        JFrame jframe=new JFrame();
        button=new JButton();
        jframe.getContentPane().add(button);
        jframe.setSize(200,200);
        button.setSize(50,50);
        jframe.setVisible(true);
      }
    public static void main(String[] args) 
    {
        testpoint test=new testpoint();
        Point p=new Point();
    p=button.getLocationOnScreen();
    p=new Point(p.x+1,p.y+1);
            Dimension d=new Dimension(50,50);
    Rectangle rec=new Rectangle(p,d);
    System.out.println(rec.x+","+rec.y+","+rec.height+","+rec.width);
    }
    }
      

  11.   

    没有nullpointexception问题!编译提示三个错误,不兼容的类型!找不到符号!找不到符号!
      

  12.   

    我这边除了找不到addnetuserpanel和gamecontrol的定义,就没别的错了啊
    你写的测试程序也是可以跑的
      

  13.   

    是吗?我这边测试程序不能跑啊!总是提示找不到符号!我的JDK是1.6的,不应该是API过旧的问题,那会是上面问题呢?
      

  14.   

    D:\javapro>javac testpoint.java
    testpoint.java:19: 不兼容的类型
    找到: java.awt.Point
    需要: Point
                    p=button.getLocationOnScreen();
                                                ^
    testpoint.java:20: 找不到符号
    符号: 构造函数 Point(int,int)
    位置: 类 Point
                    p=new Point(p.x+1,p.y+1);
                      ^
    testpoint.java:22: 找不到符号
    符号: 构造函数 Rectangle(Point,java.awt.Dimension)
    位置: 类 java.awt.Rectangle
                    Rectangle rec=new Rectangle(p,d);
                                  ^
    3 错误
      

  15.   

    楼主用的jdk哪个版本啊?你输入下java -version看,吧结果贴出来
      

  16.   

    D:\workspace\Test4Swing\src\to\shin\sai>c:\Java\jdk1.6.0_16\bin\javac.exe testpoint.javaD:\workspace\Test4Swing\src\to\shin\sai>我这边还是没出错,看看你的java版本吧,或许是环境问题
    再不行就编译的时候把库加上
      

  17.   

    D:\javapro\java -vesion
    java vesion "1.6.0_17"
    java(TM) SE Runtime Environment (build 1.6.0_17-b04)
    java HostSpot(TM) Client VM(build 14.3-b01,mixed mode,sharing)版本信息如上,是最新的,没有有问题啊!
      

  18.   

    是不是楼主自己定义了一个Point类?
    与java.awt.Point冲突了
      

  19.   

    不明白,不兼容类型,楼主去看下button.getLocationOnScreen();返回类型!