ich möchte ein JLabel (name=letter) von einem JPanel(name = startPanel) in ein anderes JPanel (name = endPanel) verschieben...
Geht das ?
wenn ja wie gehts weiter oder wo denke ich völlig verkehrt.. bzw. welche Transferklassen/-interfaces muss ich nutzen?
Klassen:
Jframe-Klasse (hintergrund)
package View;
import java.awt.Color;
import java.awt.dnd.DropTarget;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.TransferHandler;
public class MyFrame extends JFrame {
private JPanel startPanel, endPanel;
private Letter_Panel letter;
public MyFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Color back = new Color(191,233,129);
this.getContentPane().setBackground(back);
this.setTitle("Buchstabenrätsel");
this.setBounds(200, 200, 400, 400);
this.setLayout(null);
startPanel = new JPanel();
startPanel.setBounds(30, 50, 40, 60);
startPanel.setBackground(back);
startPanel.setBorder(BorderFactory.createEtchedBorder());
endPanel = new JPanel();
endPanel.setBounds(30, 150, 40, 60);
endPanel.setBackground(back);
endPanel.setBorder(BorderFactory.createEtchedBorder());
endPanel.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e) {
TransferHandler th2 = letter.getTransferHandler();
?
}
});
letter = new Letter_Panel();
startPanel.add(letter);
this.add(startPanel);
this.add(endPanel);
}
}
JLabel-Klasse:
package View;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.TransferHandler;
public class Letter_Panel extends JLabel implements MouseListener {
private Font myFont;
public Letter_Panel() {
myFont = new Font("arial",1,38);
Color back = new Color(255,0,0);
this.setBackground(back);
this.setFont(myFont);
this.setText("S");
this.setCursor(null);
this.setBorder(BorderFactory.createEtchedBorder());
this.setBounds(2, 2, 35, 55);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
TransferHandler th = this.getTransferHandler();
th.exportAsDrag(this, e, TransferHandler.MOVE);
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
Geht das ?
wenn ja wie gehts weiter oder wo denke ich völlig verkehrt.. bzw. welche Transferklassen/-interfaces muss ich nutzen?
Klassen:
Jframe-Klasse (hintergrund)
package View;
import java.awt.Color;
import java.awt.dnd.DropTarget;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.TransferHandler;
public class MyFrame extends JFrame {
private JPanel startPanel, endPanel;
private Letter_Panel letter;
public MyFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Color back = new Color(191,233,129);
this.getContentPane().setBackground(back);
this.setTitle("Buchstabenrätsel");
this.setBounds(200, 200, 400, 400);
this.setLayout(null);
startPanel = new JPanel();
startPanel.setBounds(30, 50, 40, 60);
startPanel.setBackground(back);
startPanel.setBorder(BorderFactory.createEtchedBorder());
endPanel = new JPanel();
endPanel.setBounds(30, 150, 40, 60);
endPanel.setBackground(back);
endPanel.setBorder(BorderFactory.createEtchedBorder());
endPanel.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e) {
TransferHandler th2 = letter.getTransferHandler();
?
}
});
letter = new Letter_Panel();
startPanel.add(letter);
this.add(startPanel);
this.add(endPanel);
}
}
JLabel-Klasse:
package View;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.TransferHandler;
public class Letter_Panel extends JLabel implements MouseListener {
private Font myFont;
public Letter_Panel() {
myFont = new Font("arial",1,38);
Color back = new Color(255,0,0);
this.setBackground(back);
this.setFont(myFont);
this.setText("S");
this.setCursor(null);
this.setBorder(BorderFactory.createEtchedBorder());
this.setBounds(2, 2, 35, 55);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
TransferHandler th = this.getTransferHandler();
th.exportAsDrag(this, e, TransferHandler.MOVE);
}
@Override
public void mouseReleased(MouseEvent e) {
}
}