如何在Listener中使用另外一个类的变量?(绝对菜!)

cart55free99 2010-02-12 08:29:59
A类要用到B类中的东西我就不会了,每次都这样。

恩,我就是想在 监听器B中使用A中的变量,怎么弄呢?难道是A中吧此变量声明为static?
我听别人说太多static不好,那怎么弄呢?

&&这里A是弄界面的,我总不能把初始化A放在B里面吧!

下面是代码,就是想在DragListener中用jPic这个东东。(除了声明为static没有更好的方法?)

import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;


public class DragME extends JFrame {

static JLabel jPic;

public DragME() {

jPic = new JLabel(new ImageIcon(this.getClass().getResource("1.jpg")));

add(jPic);


DragListener listener = new DragListener();
jPic.addMouseListener(listener); // 增加标签的事件处理
jPic.addMouseMotionListener(listener);

}

public JLabel getJPic(){

return jPic;
}

public static void main(String args[]) {

DragME drag=new DragME();


drag.setBounds(300, 300, 200, 200);
drag.setVisible(true);
}


}

/*
* DragListener
*/

class DragListener implements MouseInputListener{

Point p=new Point(0,0);


public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}

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) {

p = SwingUtilities.convertPoint(DragME.jPic, e.getPoint(), DragME.jPic
.getParent()); // 得到当前坐标点
}

public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseDragged(MouseEvent e) {

/*
* convertPoint(Component source, int x, int y, Component destination)
*将 source 坐标系统中的点 (x,y) 转换到 destination 坐标系统。*/

Point newP = SwingUtilities.convertPoint(DragME.jPic, e.getPoint(),
DragME.jPic.getParent()); // 转换坐标系统


DragME.jPic.setLocation(DragME.jPic.getX() + (newP.x - p.x), DragME.jPic.getY()
+ (newP.y - p.y)); // 设置标签的新位置
p = newP; // 更改坐标点

}

public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub

}



}

...全文
152 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
stamp80 2010-02-13
  • 打赏
  • 举报
回复
最好的办法是在类A中定义一个get方法,把jpic返回给调用者;或者通过A的方法,调用B方法的时候传递过去。
yueguangkai001 2010-02-13
  • 打赏
  • 举报
回复
无非是内部类调用外面的对象而已,在外面定义对象的引用就OK了,或者用外面的方法得到对象
铑枪--突廆孒 2010-02-13
  • 打赏
  • 举报
回复
仔细比对下我改的和你的代码可以看出区别
铑枪--突廆孒 2010-02-13
  • 打赏
  • 举报
回复
LZ的写法有问题阿。jPic应该申明为属性,而不是静态变量。。。你对这个变量初始化的是在构造里边搞的,,,如果没有调用构造引用这个值为出错。。。

import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;

public class DragME extends JFrame {

private final JLabel jPic;

public DragME() {

jPic = new JLabel(new ImageIcon(this.getClass().getResource("1.jpg")));

add(jPic);

DragListener listener = new DragListener(this);
jPic.addMouseListener(listener); // 增加标签的事件处理
jPic.addMouseMotionListener(listener);

}

public JLabel getJPic() {

return jPic;
}

public static void main(String args[]) {

DragME drag = new DragME();

drag.setBounds(300, 300, 200, 200);
drag.setVisible(true);
}

}

/*
* DragListener
*/

class DragListener implements MouseInputListener {

private final DragME dragME;

DragListener(DragME d) {
dragME = d;
}

Point p = new Point(0, 0);

public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}

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) {

p = SwingUtilities.convertPoint(dragME.getJPic(), e.getPoint(), dragME
.getJPic().getParent()); // 得到当前坐标点
}

public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

public void mouseDragged(MouseEvent e) {

/*
* convertPoint(Component source, int x, int y, Component destination)将
* source 坐标系统中的点 (x,y) 转换到 destination 坐标系统。
*/

Point newP = SwingUtilities.convertPoint(dragME.getJPic(),
e.getPoint(), dragME.getJPic().getParent()); // 转换坐标系统

dragME.getJPic().setLocation(dragME.getJPic().getX() + (newP.x - p.x),
dragME.getJPic().getY() + (newP.y - p.y)); // 设置标签的新位置
p = newP; // 更改坐标点

}

public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub

}

}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧