import java.awt.Label; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; public class frame1 extends javax.swing.JFrame { int x,y; private int preX; private int preY; private boolean pressOut; /** Creates new form frame */ public frame1() { initComponents(); } class NvLabel extends javax.swing.JLabel implements MouseListener, MouseMotionListener { javax.swing.JLabel n = new javax.swing.JLabel(); public NvLabel(String image, String text){ n.setIcon(new javax.swing.ImageIcon(getClass().getResource(image))); n.setText(text); n.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); n.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setBackground(new java.awt.Color(102, 102, 102)); jPanel1.setBackground(new java.awt.Color(255, 255, 255)); jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(java.awt.event.MouseEvent evt) { jPanel1MouseDragged(evt); } public void mouseMoved(java.awt.event.MouseEvent evt) { jPanel1MouseMoved(evt); } }); jPanel1.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jPanel1MouseClicked(evt); } public void mousePressed(java.awt.event.MouseEvent evt) { jPanel1MousePressed(evt); } public void mouseReleased(java.awt.event.MouseEvent evt) { jPanel1MouseReleased(evt); } }); jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/testdragpanel/delete.png"))); // NOI18N jLabel1.setText("jLabel1"); jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .add(349, 349, 349) .add(jLabel1) .addContainerGap(219, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(jPanel1Layout.createSequentialGroup() .add(80, 80, 80) .add(jLabel1) .addContainerGap(277, Short.MAX_VALUE)) ); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap() .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .addContainerGap() .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); pack(); }// </editor-fold> private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) { this.x=evt.getX(); this.y=evt.getY(); NvLabel a = new NvLabel("/testdragpanel/delete.png", "delete"); this.add(a); a.setLocation(x, y); a.setVisible(true); jLabel1.setLocation(evt.getX(), evt.getY()); } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionMouseAdapter mouseAdapter=new MouseAdapter(){ @Override public final void mouseClicked(MouseEvent e){ if(SwingUtilities.isLeftMouseButton(e)) {int x=e.getX(),y=e.getY(); // stocke x et y quelque part ailleurs ou dans un attribut pour t'en servir après } } }; panel.addMouseMotionListener(mouseAdapter); panel.addMouseListener(mouseAdapter);
@Override protected void paintComponent(Graphics g){ super.paintComponent(g); g.drawImage(bufferedImage,0,0,this); }