说实话看不大懂你程序的结构
你的 frame类中怎么还有两个类啊?txt1是在frame类中定义的变量 怎么拿到myMouseListener中用啊定义域都不允许
你的 frame类中怎么还有两个类啊?txt1是在frame类中定义的变量 怎么拿到myMouseListener中用啊定义域都不允许
解决方案 »
- hibernate处理日志表的时间
- J2EE工程移植到linux问题
- hibernate 一对多级联更新,老问题重提
- Hibernate的Example queries运用疑问
- javamail的问题:不能用163的邮箱发送,大家帮我啊
- 急问svg高人:如何在网页中插入动态生成的svg图像
- 我想在同一台机器上,启动两个tomcat,请问除了改动访问端口外,还需要改动什么,谢谢,急
- javascript如何把参数传给后台
- spring前置判断
- spring data jpa 通过字符串字段查询,接口返回永远是空,而通过其他非字符串字段查询却可以返回结果
- tomcat 为什么会产生大量的.tmp文件
- -====我是加加,十分迷恋编程,有个问题请教大家,谢谢:) ===-
import java.awt.event.*;
public class zuoye4
{
public static void main(String args[])
{
frame z=new frame();
z.setSize(300,300);
z.setVisible(true);
}
}
class frame extends Frame
{
Panel p;
TextField txt1;
frame()
{
setTitle("显示鼠标事件:");
p=new Panel();
txt1=new TextField(15);
p.add(txt1);
txt1.setEditable(false);
myMouseListener n=new myMouseListener();
p.addMouseListener(n);
add(p);
addWindowListener(new myWindowAdapter());
}
private class myMouseListener implements MouseListener
{
public void mouseClicked(MouseEvent m)
{
txt1.setText("是:"+m.getX()+","+m.getY());
}
public void mouseEntered(MouseEvent m){}
public void mouseExited(MouseEvent m){}
public void mousePressed(MouseEvent m){}
public void mouseReleased(MouseEvent m){}
}
private class myWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent w)
{
System.exit(0);
}
}}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class MouseDemo extends MouseAdapter
{
JFrame f = null;
JButton b1 = null;
JLabel label = null;
public MouseDemo()
{
f = new JFrame("MouseDemo");
Container contentPane = f.getContentPane();
contentPane.setLayout(new GridLayout(2,1));
b1 = new JButton("按钮");
label = new JLabel("起始状态,还没鼠标事件",JLabel.CENTER);
b1.addMouseListener(this);
contentPane.add(label);
contentPane.add(b1);
f.pack();
f.show();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}});
} public void mousePressed(MouseEvent e) {
label.setText("目前鼠标坐标(X,Y)为:("+e.getX()+","+e.getY()+")");
} public void mouseClicked(MouseEvent e) {
if (e.getClickCount() ==2&&e.getButton()==1&&e.getModifiers()==17)
{
JFrame newF = new JFrame("新窗口");
newF.setSize(200,200);
newF.show();
}
}
public static void main(String args[])
{
new MouseDemo();
}
}
import java.awt.event.*;
import javax.swing.*;public class shubiaoClicked extends Frame
{
private shubiaoClicked Form1;
public int X;
public int Y;
public shubiao()
{
Form1=this;
this.setSize(300,300);
this.setVisible(true);
this.setBackground(Color.pink);
this.addMouseListener(new myMouseListener());
this.addWindowListener(new myWindowAdapter());
}
class myMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
if(e.getID==MouseEvent.MOUSE_CLICKED)
{
X=e.getX();
Y=e.getY();
Form1.setTitle("此时的鼠标的坐标是"+X+"."+Y);
}
else
{
System.out.println("sorry!");
}
}
}
class myWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main(String args[])
{
new shubiaoClicked();
}}
///////楼主看看我的程序吧!我的在你的基础上改的!
///////运行结果是,鼠标在窗体上每电击左键一次,在窗口的标题栏上就会出现坐标!
构造函数是shubiaoClicked() 而不是 shubiao()
请楼主记得修改!
下面的tet.setText("..是不是超出范围了????...");
关键在你的text对象不能在构造函数里面申明还有panel对象 要在它之前申明 否则你的事件监听不到
方法里面的对象 他是监听类的对象
public void mouseClicked(MouseEvent m)
{
txt1.setText("你的鼠标现在的位置是:"+m.getX()+","+m.getY());
}
import java.awt.event.*;
class zuoye4
{
public static void main(String args[])
{
frame z=new frame();
z.setSize(300,300);
z.setVisible(true);
}
}
class frame extends Frame
{
frame()
{
setTitle("显示鼠标事件:");
Panel p=new Panel();
final TextField txt1=new TextField(15);
p.add(txt1);
txt1.setEditable(false);
p.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent m)
{
txt1.setText(("你的鼠标现在的位置是:"+m.getX()+","+m.getY());
} public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
} public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
} public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
} public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
add(p);
}
}
import java.awt.event.*;
class zuoye4
{
public static void main(String args[])
{
frame z=new frame();
z.setSize(300,300);
z.setVisible(true);
}
}
class frame extends Frame
{
frame()
{
setTitle("显示鼠标事件:");
Panel p=new Panel();
final TextField txt1=new TextField(15);
p.add(txt1);
txt1.setEditable(false);
p.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent m)
{
txt1.setText(("你的鼠标现在的位置是:"+m.getX()+","+m.getY()));
} public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
} public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
} public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
} public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
add(p);
}
}
楼上的少了一个“)”,现在我帮加上,呵呵.......
好极了.......
inner class myMouseListener只能访问类中的元素,它看不见 txt1