就是让窗体之间有自动吸附的功能,和拖动qq到桌面旁边一样的效果

解决方案 »

  1.   

    addComponentListener(new ComponentAdapter(){
        @Override public void componentMoved(ComponentEvent e){
            ... // 设置窗口位置。
        }
    });
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class PrimaryFrame extends JFrame {
        private SecondFrame secondFrame;//假设SecondFrame一直跟在PrimaryFrame的下边    public PrimaryFrame() {
    init();
        }    private void init() {
    setSize(340, 170);
    setTitle("PrimaryFrame");
    setDefaultCloseOperation(3);
    secondFrame = new SecondFrame(this);
    setVisible(true);
    addComponentListener(new ComponentAdapter(){
        public void componentMoved(ComponentEvent e){
            secondFrame.setLocation(getX(),getY()+getHeight());//假设SecondFrame一直跟在PrimaryFrame的下边
        }
        public void componentResized(ComponentEvent e) {
    secondFrame.setSize(getWidth(),secondFrame.getHeight());
        }
    });     }    public static void main(String[] args) {
    new PrimaryFrame();
        }}class SecondFrame extends JFrame {//假设它一直跟在PrimaryFrame的下边
        PrimaryFrame primaryFrame ;
        public SecondFrame(PrimaryFrame primaryFrame){
    this.primaryFrame = primaryFrame;
    init();

        }
        private void init(){
    setTitle("SecondFrame");
    setSize(340, 170);
    setLocation(0, 170);
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    setVisible(true);
    addComponentListener(new ComponentAdapter(){
        public void componentResized(ComponentEvent e) {
    primaryFrame.setSize(getWidth(),primaryFrame.getHeight());

        }
        @Override public void componentMoved(ComponentEvent e){
            primaryFrame.setLocation(getX(),getY()-primaryFrame.getHeight());//假设SecondFrame一直跟在PrimaryFrame的下边
        }
    });
        }
    }
      

  3.   

    public void componentMoved(ComponentEvent e){
                secondFrame.setLocation(getX(),getY()+getHeight());//假设SecondFrame一直跟在PrimaryFrame的下边
            }
            public void componentResized(ComponentEvent e) {
            secondFrame.setSize(getWidth(),secondFrame.getHeight());        
            }
      

  4.   


    ls这位仁兄看来很喜欢copy别人的回复啊。那个帖子都能看见你的copy。
      

  5.   

    if(frame.x > (screen.x-frame.width))
    {
    frame.x = scree.x - 10;//要露出来一点
    }然后鼠标移上去的话,就再设定frame的位置