以下是程序段,我实在找不出哪里出了错,编译老是提示找不到符号,
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)
解决方案 »
- 请教调用webservice接口出现的问题
- 使用FileInputStream和文件建立连接是怎么一个概念?
- 请问高手。。这个程序为什么没办法实现。。
- 现有两个vbscript的函数,要翻译成java的,帮帮忙!
- jdk\lib, jdk\jre\lib 和 jdk\jre\lib\ext 这三个文件夹有什么区别呀?
- 关于公式解析器的问题,求教
- 初来乍到,遵守诺言,Kosling请进来领分!
- 问个弱问题,JAVA里有没有四舍五入的方法?
- 有知道理想信息(深圳分公司)的么,顺便散纷
- 12月24号偶生日为答谢各位朋友对偶的照顾特此散分!!(100)
- 弱弱提问 《thinging in Java》的程序有点不明白?
- 如何显示选中的字符串,大虾指点下
还有你的Point p=button.getLocationOnScreen(); 这句button根本就是空的,所以你这句肯定会报null pointer错误。
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、欢迎楼下的补充!
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);
}
注意Point是java.awt.Point
包没引错,一开始我是直接导入整个awt包,编译不过,我又改为引入程序中用到的类,还是不行
以下是程序段,我实在找不出哪里出了错,编译老是提示找不到符号,
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);
}
}楼主根据我标的序号按顺序参考
那建议你把error message发上来
或者把你出错的代码附近的代码贴上来一般这种错误,可能有以下几种原因
1. source里面包含全角字符
2. 前面有编译不过的
3. 包引用不对
4. 多了或者少了括号啊分号之类的
5. PS:你上面贴的代码,在你那边可以编译过么?
/*图像采集发送线程*/
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);
}
}
你写的测试程序也是可以跑的
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 错误
再不行就编译的时候把库加上
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)版本信息如上,是最新的,没有有问题啊!
与java.awt.Point冲突了