Drag and drop d'un JPanel à un autre
Bonjour,
Je suis en train de travailler sur un projet qui va permettre de dessiner des réseaux électriques et je bloque sur le drag and drop.
J'ai une frame qui contient 2 JPanel, un JPanel de composant et un autre pour les dessins. L'objectif ici est de porter un JLabel du JPanel de composant pour le déposer dans le JPanel de dessin et gérer les enlèvements liés au JLable dépose.
Je débute en Java...
Quelqu'un aurait-il un idée sur comment procéder ?
Voici mon code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
public class MonProjet extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel_Composant = null;
private JPanel jPanel_Dessin = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JLabel jLabel3 = null;
/**
* This method initializes jPanel_Composant
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel_Composant() {
if (jPanel_Composant == null) {
jLabel3 = new JLabel();
jLabel3.setText("JLabel");
jLabel3.setName("jLabel3");
jLabel2 = new JLabel();
jLabel2.setText("JLabel");
jLabel2.setName("jLabel2");
jLabel1 = new JLabel();
jLabel1.setText("JLabel");
jLabel1.setName("jLabel1");
jLabel = new JLabel();
jLabel.setText("JLabel");
jLabel.setName("jLabel");
jPanel_Composant = new JPanel();
jPanel_Composant.setLayout(new BoxLayout(getJPanel_Composant(), BoxLayout.Y_AXIS));
jPanel_Composant.setBounds(new Rectangle(10, 16, 44, 134));
jPanel_Composant.add(jLabel, null);
jPanel_Composant.add(jLabel1, null);
jPanel_Composant.add(jLabel2, null);
jPanel_Composant.add(jLabel3, null);
}
return jPanel_Composant;
}
/**
* This method initializes jPanel_Dessin
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel_Dessin() {
if (jPanel_Dessin == null) {
jPanel_Dessin = new JPanel();
jPanel_Dessin.setLayout(new GridBagLayout());
jPanel_Dessin.setBounds(new Rectangle(68, 15, 205, 135));
jPanel_Dessin.setForeground(Color.white);
jPanel_Dessin.setBackground(Color.white);
jPanel_Dessin.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
}
return jPanel_Dessin;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MonProjet thisClass = new MonProjet();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public MonProjet() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJPanel_Composant(), null);
jContentPane.add(getJPanel_Dessin(), null);
}
return jContentPane;
}
} |
Merci d'avance pour votre aide.