你好,请问做了一个界面,没有标题栏
他不能用鼠标拖动,有啥函数可以实现?

解决方案 »

  1.   

    虽然没有做过,但感觉应该可以实现的,从前做c++的开发时也做过类似的程序.下面把思路说一下:
    介绍一种易于实现并且效果更好的方法,这种方法被很多的专业软件所使用,其中包括大名鼎鼎的Winamp。其主要原理是当窗口用鼠标进行拖动时,鼠标的位置相对于窗口是始终不变的。所以如果能够在鼠标移动过程中,通过改变窗口在桌面上的坐标,始终保持鼠标的相对坐标不变,即可实现鼠标的拖动效果。在具体的程序设计中,先在OnMouseDown事件中记录鼠标位置,而在OnMouseMove事件中根据鼠标的移动距离,实时修改窗体Form的Top和Left值,即可实现窗口的鼠标拖动操作。在JAVA中原理应该是差不多的,都是在鼠标Down,Move,Up三个函数中写代码.
    首先设置一个boolean的变量isDraging表示是否开始拖动,初值为false,
    按下时:
    isDraging设置为true,同时记录鼠标位置.
    拖动时:
    如果isDraging为true,表明正在拖动,根据鼠标当前位置实时改变窗体的位置,关键c++代码如下:
            if(isDraging)
            {
               Form1->Left+=X-xx;
               Form1->Top+=Y-yy;
            }
    弹起时:
    设置isDraging为false.
      

  2.   

    package mousedrag;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;public class MouseDrag extends JFrame {
        private boolean isDraging = false;
        private int xx, yy;
        /** Creates a new instance of MouseDrag */
        public MouseDrag() {
            super("Mouse Drag");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setBounds(200, 200, 200, 200);
            this.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e)
                {
                    isDraging=true;
                    xx=e.getX();
                    yy=e.getY();
                }
                public void mouseReleased(MouseEvent e)
                {
                    isDraging = false;
                }
            });
            this.addMouseMotionListener(new MouseMotionAdapter(){
             public void mouseDragged(MouseEvent e)
                {
                            if(isDraging)
                            {
    //                           Form1->Left+=X-xx;
    //                           Form1->Top+=Y-yy;
                                int left = getLocation().x;
                                int top = getLocation().y;
                                setLocation( left+ e.getX() - xx, 
                                                   top + e.getY() - yy);
                            }            }
            });
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            new MouseDrag().setVisible(true);
        }
        
    }
    刚刚自己做了一下,已经实现了,由于完全是照c++的代码扒下来的,所以可能写的不是很精练,也可能在java中有更好的解决办法,懒得优化了.
      

  3.   

    // 这是一个没有标题也不能拖拽的窗口
    import java.awt.BorderLayout;
    import javax.swing.JLabel;
    import javax.swing.JWindow;public class WindowTest extends JWindow
    {
    public WindowTest()
    {
    super();
    this.setBounds(100, 100, 400, 225);
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(new JLabel("这是一个没有标题也不能拖拽的窗口"), BorderLayout.CENTER);
    } /**
     * @param args
     */
    public static void main(String[] args)
    {
    WindowTest mainWin = new WindowTest();
    mainWin.setVisible(true);
    }
    }