【问题标题】:Moving undecorated window by clicking on JPanel通过单击 JPanel 移动未装饰的窗口
【发布时间】:2012-06-02 04:12:24
【问题描述】:

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

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

【问题讨论】:

  • 如果是标签,标题中不需要包含Java。

标签: java swing border panel


【解决方案1】:

我的项目有一个简单的解决方案。这是我未装饰的 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();
}
}

【讨论】:

  • 我不知道如何,但这个在我的 HiDPI 屏幕上效果更好
【解决方案2】:

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

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 来概括此示例。

【讨论】:

  • 谢谢,这正是我想要的!
【解决方案3】:
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);

【讨论】:

    【解决方案4】:

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

    我认为@camickr 的ComponetMover 适合你

    【讨论】:

      【解决方案5】:

      是的,很有可能。您需要一个 MouseListener 来监听鼠标事件。您开始在 mousedown 上移动并在 mouseup 上停止移动。然后,您只需将窗口位置平移鼠标在该阶段平移的相同量(计算旧鼠标位置和新鼠标位置之间的增量并将其添加到框架位置)。你应该可以很容易地用鼠标监听器做到这一点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-29
        • 2015-12-05
        • 1970-01-01
        • 2012-10-23
        • 2015-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多