Hi,
hab ein Problem mit folgendem Code... Eigentlich läuft ja alles so wie gewünscht, bis auf ne Kleinigkeit. Wenn der Button von einem Panel ins andere verschoben wird, "springt" er rüber. Ich möchte aber, dass er in dem einen Panel verschwindet und in dem anderen wieder auftaucht.
Komme einfach nicht weiter. Bitte helft mir!
Hier der Code:
Für jede Idee oder Hilfe bin ich dankbar.
Gruß Andi
hab ein Problem mit folgendem Code... Eigentlich läuft ja alles so wie gewünscht, bis auf ne Kleinigkeit. Wenn der Button von einem Panel ins andere verschoben wird, "springt" er rüber. Ich möchte aber, dass er in dem einen Panel verschwindet und in dem anderen wieder auftaucht.
Komme einfach nicht weiter. Bitte helft mir!
Hier der Code:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseDrag {
// main panel container as parent for the labels
JPanel mainP;
JPanel leftP;
JPanel rightP;
JButton but;
//click coordinates
int x = 0;
int y = 0;
//position coordinates
int lx = 0;
int ly = 0;
int cx = 0;
int cy = 0;
public MouseDrag() {
mainP = new JPanel();
leftP = new JPanel();
rightP = new JPanel();
mainP.setLayout(new GridLayout(0,2));
but = new JButton("Button");
but.setBounds(50, 50, 100, 30);
mainP.add(leftP);
mainP.add(rightP);
rightP.add(but);
leftP.setLayout(null);
rightP.setLayout(null);
setPanelColour();
// Enable mouse motion event handling by
// adding mouse motion listeners
mainP.addMouseMotionListener(new MyMouseMotionListener());
mainP.addMouseListener(new MyMouseMotionListener());
but.addMouseMotionListener(new MyMouseMotionListener());
but.addMouseListener(new MyMouseMotionListener());
}
public void setPanelColour(){
rightP.setBackground(Color.green);
}
private class MyMouseMotionListener implements MouseListener, MouseMotionListener {
// Methods of the MouseMotionListener interface
public void mousePressed(MouseEvent e) {
//save the x and y coord of the click
cx = e.getX();
cy = e.getY();
}
public void mouseDragged(MouseEvent e) {
Component move = (Component)e.getSource();
Dimension size = move.getSize();
lx = e.getX() + move.getX()- cx;
ly = e.getY() + move.getY()- cy;
move.setLocation(lx,ly);
if(lx > leftP.getWidth()){
leftP.remove(move);
rightP.add(move);
}
if(lx < 0){
rightP.remove(move);
leftP.add(move);
move.setLocation(lx + leftP.getWidth(),ly);
}
//do not move off the screen sides
lx = Math.max(lx,0);
lx = Math.min(lx, rightP.getWidth()-size.width);
//do not move off top or bottom
ly = Math.max(ly,0);
ly = Math.min(ly, mainP.getHeight()-size.height);
move.setLocation(lx,ly);
}
//method of the MouseMotionListener
public void mouseMoved(MouseEvent e) {}
//methods of the mouselistener
public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
// Main method
public static void main(String[] args) {
// Create a frame as main application window
// with the specified title
JFrame mainF = new JFrame("Drag the Mouse");
// Make the application exit when the window is closed.
mainF.addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
// Set width and height of the main window
mainF.setSize(300, 300);
// Create a MouseDrag object
MouseDrag drag = new MouseDrag();
// Add the main panel to the frame's content pane
mainF.getContentPane( ).add(drag.mainP);
mainF.setVisible(true);
}
}
Für jede Idee oder Hilfe bin ich dankbar.
Gruß Andi