Moving undecorated window by clicking on JPanel

前端 未结 6 805
鱼传尺愫
鱼传尺愫 2021-02-08 15:43

Is there a possibility to move window by clicking on one of the panels in the window when that window is undecorated?

I have a main panel with matte border 40 pixels siz

相关标签:
6条回答
  • 2021-02-08 16:26

    I have a simple solution from my project. Here is my undecorated JDialog class.

    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();
    }
    }
    
    0 讨论(0)
  • 2021-02-08 16:30
    int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
    

    thisX + -thisX = 0

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

    What i'm using.

    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) {
    
    }
    

    }

    and in your Frame constructor

    MouseLiestenerX IMove = new MouseLiestenerX(this);      
    addMouseListener(IMove);
    addMouseMotionListener(IMove);
    
    0 讨论(0)
  • 2021-02-08 16:34

    Yes, it is very possible. You need a MouseListener to listen on mouse events. you start moving on mousedown and stop moving on mouseup. Then you simply translate the window position by the same amount the mouse translates during that phase (calculate the delta bewteen old mouse position and new mouse position and add that to the frames position). You should be able to do this with a mouse listener fairly easily.

    0 讨论(0)
  • 2021-02-08 16:36

    You can place another panel over the panel with the border, leaving the border visible.Use the following code to move your window.

    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);
            }
        });
        }
    }
    

    I've been working with this code for a while now to make a custom titlebar for undecorated windows. P.S.:You can generalize this example by extending JComponent instead of JPanel.

    0 讨论(0)
  • 2021-02-08 16:39

    I have a main panel with matte border 40 pixels size, and few panels with controls inside, and I would like to move the window when clicking on that border

    I think that ComponetMover by @camickr is right class for you

    0 讨论(0)
  • 2021-02-08 16:50

    This code works very well with single monitor. It uses a simple mouselistener and mouse motion listener, which do some basic algebra do move the frame.

    class Frame extends JFrame{
    
        private int framePositionX, framePositionY, mousePositionX, mousePositionY, newMousePositionX, newMousePositionY;
    
        Frame(){
    
            addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent mouseEvent) {
                    framePositionX = mouseEvent.getComponent().getX();
                    framePositionY = mouseEvent.getComponent().getY();
                    mousePositionX = mouseEvent.getX();
                    mousePositionY = mouseEvent.getY();
                }
                @Override
                public void mousePressed(MouseEvent mouseEvent) {
                    framePositionX = mouseEvent.getComponent().getX();
                    framePositionY = mouseEvent.getComponent().getY();
                    mousePositionX = mouseEvent.getX();
                    mousePositionY = mouseEvent.getY();
                }
                @Override public void mouseReleased(MouseEvent mouseEvent) { }
                @Override public void mouseEntered(MouseEvent mouseEvent) { }
                @Override public void mouseExited(MouseEvent mouseEvent) { }
            });
    
            addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    newMousePositionX = e.getX();
                    newMousePositionY = e.getY();
                    int newFramePositionX = (newMousePositionX - mousePositionX)+ framePositionX;
                    int newFramePositionY = (newMousePositionY - mousePositionY)+ framePositionY;
                    e.getComponent().setLocation(newFramePositionX, newFramePositionY);
                }
            });
    
            setUndecorated(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
    }
    
    0 讨论(0)
提交回复
热议问题