当前位置: 代码迷 >> J2SE >> 怎么实现java磁性窗体
  详细解决方案

怎么实现java磁性窗体

热度:58   发布时间:2016-04-24 00:22:21.0
如何实现java磁性窗体
就是让窗体之间有自动吸附的功能,和拖动qq到桌面旁边一样的效果

------解决方案--------------------
Java code
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的下边        }    });    }}
  相关解决方案