我正在尝试JLabel
在带有 的容器中实现一个用鼠标指针移动的mouseMotionListener
,但JLabel
没有出现在屏幕上。有什么建议么?
public class Ships extends JFrame implements ActionListener{
private JPanel contentPane;
int x=418,p=75,l=10;
public static void main(String[] args) {
Ships m = new Ships();
}
JLabel lblNewLabel = new JLabel();
JLabel l5 = new JLabel();
Container container;
/**
* Create the frame.
*/
public Ships() {
Container container= getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, 0, 1363, 730);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
setVisible(true);
l5.setIcon(new ImageIcon("C:\\Users\\The Wimpster\\Desktop\\images22.png"));
container.add(l5);
l5.setBounds(0, 10, 75, 50);//this label is supposed to move with mouse pointer
container.addMouseMotionListener(new MouseAdapter(){
public void mouseMoved(MouseEvent e){
p = e.getX();
l = e.getY();
l5.setBounds(p,l,150,50);
}
});
}
}