题目:定义一个Point类,该类有两个float类型的属性x,y,,定义两个构造法,一个无参数,将x,y初始为0;另一个一坐标值为参数,设计x,y为给定坐标值,用该类的show方法输出该点的坐标值。测试:使用窗体,根据鼠标动态在窗体上显示坐标值。下面是我自己做的,但不会动态显示坐标值,请交高手指正错误~谢谢import java.awt.*;
import java.awt.event.*;
public class Point extends Frame implements MouseMotionListener
{
Label L1;
float x,y;
Panel p;
int k=1; public void Escent()
{
x=0;
y=0;
}
public void PMark(float x1,float y1)
{
x=x1;
y=y1;
}
public void show()
{
L1.setText("Moved at (" + x + "," + y + ")");
repaint();
} public void mouseDragged(MouseEvent e)
{
this.PMark(e.getX(),e.getY());
}
public void mouseMoved(MouseEvent e)
{
this.PMark(e.getX(),e.getY());
} public Point()
{
super("求坐标");
setBackground(Color.lightGray);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
setSize(200,100);
setVisible(true);
L1=new Label();
L1.setText("");
p=new Panel();
add(p);p.add(L1);
addMouseMotionListener(this);
this.show();
} public static void main(String[] args)
{
Point m1;
m1=new Point();
m1.Escent();
}
}
import java.awt.event.*;
public class Point extends Frame implements MouseMotionListener
{
Label L1;
float x,y;
Panel p;
int k=1; public void Escent()
{
x=0;
y=0;
}
public void PMark(float x1,float y1)
{
x=x1;
y=y1;
}
public void show()
{
L1.setText("Moved at (" + x + "," + y + ")");
repaint();
} public void mouseDragged(MouseEvent e)
{
this.PMark(e.getX(),e.getY());
}
public void mouseMoved(MouseEvent e)
{
this.PMark(e.getX(),e.getY());
} public Point()
{
super("求坐标");
setBackground(Color.lightGray);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
setSize(200,100);
setVisible(true);
L1=new Label();
L1.setText("");
p=new Panel();
add(p);p.add(L1);
addMouseMotionListener(this);
this.show();
} public static void main(String[] args)
{
Point m1;
m1=new Point();
m1.Escent();
}
}
show();
如下
public void PMark(float x1,float y1)
{
x=x1;
y=y1;
show();
}
import java.awt.event.*;
public class Point extends Frame implements MouseMotionListener
{
Label L1;
float x,y;
Panel p;
int k=1; public void Escent()
{
x=0;
y=0;
}
public void PMark(float x1,float y1)
{
x=x1;
y=y1;
showPoint();
}
public void showPoint()
{
L1.setText("Moved at (" + x + "," + y + ")");
repaint();
} public void mouseDragged(MouseEvent e)
{
this.PMark(e.getX(),e.getY());
}
public void mouseMoved(MouseEvent e)
{
this.PMark(e.getX(),e.getY());
} public Point()
{
super("求坐标");
setBackground(Color.lightGray);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
setSize(400,100);
setVisible(true);
L1=new Label();
L1.setText("");
//由于你的Lable初始是没有文字的,所以要加上下面一句,保证它被显示出来
L1.setPreferredSize(new Dimension(200,30));
p=new Panel();
//下面一句,把panel添加到一个指定的地方,否则他默认在中间,占据了大多空间,
//影响frame接收鼠标事件
add(p,BorderLayout.NORTH);
p.add(L1);
addMouseMotionListener(this);
this.setVisible(true);
//this.show();
//不使用show()方法,使用setVisible();
} public static void main(String[] args)
{
Point m1;
m1=new Point();
m1.Escent();
}
}
Point.java:53: cannot resolve symbol
symbol : method setPreferredSize (java.awt.Dimension)
location: class java.awt.Label
L1.setPreferredSize(new Dimension(200,30));
^
1 error不过还是顶你啦~高手o(∩_∩)o...哈哈
那个方法是在1.5版本中才有的
o(∩_∩)o...哈哈这样的话,我知道啦,非常感谢!