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

}



}

解决方案 »

  1.   

    最好的办法是在类A中定义一个get方法,把jpic返回给调用者;或者通过A的方法,调用B方法的时候传递过去。
      

  2.   

    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 }}