我想问一下为什么我在jframe中点击一下了之后,图片并没有出现呢?还有就是如果我想实现鼠标拖动jlabel该怎么实现呢?
package action10;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Point;
/**
*
* @author 11417
*/
public class Action10 extends JFrame implements MouseListener{
JLabel jb=new JLabel();
JPanel jp=new JPanel();
public Action10(){
this.addMouseListener(this);
this.setLayout(null);
this.setSize(600,600);
this.setVisible(true);
jp.addMouseListener(this);
}
public void mouseClicked(MouseEvent e){
ImageIcon img=new ImageIcon("D:/Procedure/Java/Action10/cmcc.png");
jb.setIcon(img);
int i=e.getX();
int j=e.getY();
jp.add(jb);
this.add(jp);
jp.setSize(100,100);
jp.setLocation(i-50,j-50);
}
public void mouseDragged(MouseEvent e){
if(e.getSource()==jb){
Point point = jb.getLocation();
jb.setLocation(e.getPoint().x + point.x, e.getPoint().y + point.y);
}
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){
ImageIcon img=new ImageIcon("D:/Procedure/Java/Action10/cmcc.png");
jb.setIcon(img);
int i=e.getX();
int j=e.getY();
jp.add(jb);
this.add(jp);
jp.setSize(100,100);
jp.setLocation(i-50,j-50);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Action10();
}
}