单击JPanel移动未修饰的窗口

时间:2012-05-27 11:40:52

标签: java swing border panel

当该窗口未修饰时,是否可以通过单击窗口中的一个面板来移动窗口?

我有一个带有哑光边框40像素大小的主面板,里面有几个带控件的面板,我想在点击边框时移动窗口。这可能吗?

6 个答案:

答案 0 :(得分:30)

您可以使用边框在面板上放置另一个面板,使边框可见。使用以下代码移动窗口。

public class MotionPanel extends JPanel{
    private Point initialClick;
    private JFrame parent;

    public MotionPanel(final JFrame parent){
    this.parent = parent;

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            initialClick = e.getPoint();
            getComponentAt(initialClick);
        }
    });

    addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {

            // get location of Window
            int thisX = parent.getLocation().x;
            int thisY = parent.getLocation().y;

            // Determine how much the mouse moved since the initial click
            int xMoved = e.getX() - initialClick.x;
            int yMoved = e.getY() - initialClick.y;

            // Move window to this position
            int X = thisX + xMoved;
            int Y = thisY + yMoved;
            parent.setLocation(X, Y);
        }
    });
    }
}

我已经使用这段代码了一段时间,为未修饰的窗口制作自定义标题栏。 P.S .:你可以通过扩展JComponent而不是JPanel来概括这个例子。

答案 1 :(得分:4)

  

我有一个带有哑光边框40像素大小的主面板,里面有几个带控件的面板,我想在点击边框时移动窗口

我认为@camickr ComponetMover对你来说是正确的课程

答案 2 :(得分:1)

是的,很有可能。您需要一个MouseListener来监听鼠标事件。你开始移动mousedown并停止继续前进。然后,您只需将窗口位置转换为鼠标在该阶段转换的相同数量(计算旧鼠标位置和新鼠标位置的增量并将其添加到帧位置)。你应该可以很容易地使用鼠标监听器。

答案 3 :(得分:0)

我从我的项目中得到了一个简单的解决方案。这是我未经修饰的JDialog类。

public class TimerDialog extends JDialog {
// some fields here
private Point mouseClickPoint; // Will reference to the last pressing (not clicking) position

private TimerDialog() {
    initComponents();
    addEventsForDragging();
}

private void addEventsForDragging() {
    // Here is the code does moving
    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            mouseClickPoint = e.getPoint(); // update the position
        }

    });
    addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            Point newPoint = event.getLocationOnScreen();
            newPoint.translate(-mouseClickPoint.x, -mouseClickPoint.y); // Moves the point by given values from its location
            setLocation(newPoint); // set the new location
        }
    });
}

private void initComponents() {
    setLayout(new FlowLayout());
    // adding components
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setAlwaysOnTop(true);
    setUndecorated(true);
    setResizable(false);
    pack();
}
}

答案 4 :(得分:0)

此代码与单个监视器配合使用非常好。它使用简单的mouselistener和mouse motion侦听器,它们执行一些基本的代数运算来移动框架。

entries

答案 5 :(得分:-1)

int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);

thisX + -thisX = 0

int xMoved = e.getX()-initialClick.x;

我在用什么。

public class MouseLiestenerX implements MouseListener,MouseMotionListener{

private theFrame;

public MouseLiestenerX(Frame theFrame){
this.theFrame = theFrame;

}

private Point startClick;

public void mouseDragged(MouseEvent e) {
    int deltaX = e.getX()-startClick.x;
    int deltaY = e.getY()-startClick.y;

    Core.getSp().setLocation(theFrame.getLocation().x+deltaX, theFrame.getLocation().y+deltaY);
}

public void mousePressed(MouseEvent e) {
    startClick = e.getPoint();
}

public void mouseMoved(MouseEvent e){

}
@Override
public void mouseClicked(MouseEvent e) {


}
@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent e) {

}

}

并在你的Frame构造函数

MouseLiestenerX IMove = new MouseLiestenerX(this);      
addMouseListener(IMove);
addMouseMotionListener(IMove);