当前位置: 代码迷 >> Java相关 >> [求助]请教高手,下面程序为何组件不能自由移动,百思不得其解!
  详细解决方案

[求助]请教高手,下面程序为何组件不能自由移动,百思不得其解!

热度:434   发布时间:2004-12-19 09:28:00.0
[求助]请教高手,下面程序为何组件不能自由移动,百思不得其解!
源程序如下: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(); } }
搜索更多相关的解决方案: 百思不得其解  组件  

----------------解决方案--------------------------------------------------------

可能不合你的原意 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(); } }


----------------解决方案--------------------------------------------------------
我认为:最好将窗口的布局设为空布局。如果将要移动的的组件添加到窗口便可以在窗口范围内移动。
----------------解决方案--------------------------------------------------------
  相关解决方案