| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 894 人关注过本帖
标题:[求助]请教高手,下面程序为何组件不能自由移动,百思不得其解!
只看楼主 加入收藏
yayali
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2004-12-19
收藏
 问题点数:0 回复次数:2 
[求助]请教高手,下面程序为何组件不能自由移动,百思不得其解!
源程序如下: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(); } }
搜索更多相关主题的帖子: 百思不得其解 组件 
2004-12-19 09:28
freedxw
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2005-3-31
收藏
得分:0 

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


天不知道,地知道, 你不知道,我知道!
2005-04-06 18:48
freedxw
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2005-3-31
收藏
得分:0 
我认为:最好将窗口的布局设为空布局。如果将要移动的的组件添加到窗口便可以在窗口范围内移动。

天不知道,地知道, 你不知道,我知道!
2005-04-07 08:51
快速回复:[求助]请教高手,下面程序为何组件不能自由移动,百思不得其解!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017735 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved