可能不合你的原意
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class twoPanelFrame extends JFrame implements MouseListener
{
JPanel smallpanel=new JPanel();
Container contentpane;
int x=0;
int y=0;
public twoPanelFrame() {
contentpane=this.getContentPane();
contentpane.setLayout(null);
setSize(400,300);
JPanel panel1=new JPanel();
panel1.setLayout(null);
JPanel panel2=new JPanel();
smallpanel.setBackground(Color.black);
contentpane.add(smallpanel);
smallpanel.addMouseListener(this);
JSplitPane wholePane= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
panel1, panel2);
smallpanel.setBounds(0,0,50,50);
contentpane.add(wholePane);
wholePane.setBounds(0,0,400,300);
contentpane.setVisible(true);
contentpane.validate();
this.setVisible(true);
}
public void mousePressed(MouseEvent m)
{
x = m.getX();
y = m.getY();
smallpanel.setLocation(x,y);
smallpanel.setVisible(false);
smallpanel.setBackground(Color.blue);
validate();
}
public void mouseReleased(MouseEvent m)
{
x = m.getX();
y = m.getY();
smallpanel.setLocation(x, y);
smallpanel.setVisible(true);
smallpanel.setBackground(Color.yellow);
validate();
}
public void mouseEntered(MouseEvent m) {
smallpanel.setVisible(true);
validate();
}
public void mouseExited(MouseEvent m) {}
public void mouseClicked(MouseEvent m) {}
}
public class twoPanel
{
public static void main(String[] args)
{
twoPanelFrame frame = new twoPanelFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}