[求助]请教高手,下面程序为何组件不能自由移动,百思不得其解!
源程序如下:frame里有JPanel实例panel1,panel2. smallpanel 原来在pane11中,现在想实现移动,使smallpanel能自由移动到panel2中!执行后,不能移动,请高手指教! package lxc; import javax.swing.*; import java.awt.*; import java.awt.event.*; class twoPanelFrame extends JFrame implements MouseMotionListener ,MouseListener { JPanel smallpanel=new JPanel(); public twoPanelFrame() { setSize(400,300); JPanel panel1=new JPanel(); JPanel panel2=new JPanel(); smallpanel.setBackground(Color.black); smallpanel.setSize(100,40); panel1.add(smallpanel); smallpanel.addMouseListener(this); smallpanel.addMouseMotionListener(this); JSplitPane wholePane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, panel2); getContentPane().add(wholePane, "Center"); } public void mouseDragged(MouseEvent m) { int x = m.getX(); int y = m.getY(); smallpanel.setLocation(x, y); smallpanel.setBackground(Color.blue); validate(); } public void mouseMoved(MouseEvent m) { int x = m.getX(); int y = m.getY(); smallpanel.setLocation(x, y); smallpanel.setBackground(Color.blue); validate(); } public void mousePressed(MouseEvent m) { int x = m.getX(); int y = m.getY(); smallpanel.setLocation(x, y); smallpanel.setBackground(Color.blue); validate(); } public void mouseReleased(MouseEvent m) { int x = m.getX(); int y = m.getY(); smallpanel.setLocation(x, y); smallpanel.setBackground(Color.blue); validate(); } public void mouseEntered(MouseEvent m) {} 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(); } } |