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
}
}
我听别人说太多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
}
}
解决方案 »
- 用正则表达式如何表示合法的括号序列?
- inputStream数据输入流数据有几万个btye,直接read()会丢失数据怎么办??求各位大侠指导下
- JDBC的问题!!!!!
- textpad 运行java程序的问题
- 关于windows的问题.(JAVA如何关闭一个windows进程,如何修改windows设置)
- 有朋友过这个网站吗?http://www.ttjj.com/index.php 我发现它的技术还真是不错!!
- 救急!!请赐教!!!!
- 那里有java源码网址
- java 怎么调用VB做的DLL,
- JAVA运行错误问题!急!!!
- 能不能简单的debug一下这段简单的代码
- About OutOfMemoryError in java
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 }}