简体   繁体   中英

Jittering when trying to move undercorated JFrame

I have made a custome JFrame called mainWindow that is undecorated. I have added a JLabel named dragBar at the top of it and gave it desired dimensions (as shown below). When I click on the label I make the window move according to my mouse by using two listeners; one MouseListener and one MouseMotionListener .
The problem is that whenever I click on the label the window does move according to my mouse's location but it spazzes all over my screen until I stop moving the mouse or let go of the click button.
Is my method wrong? What is causing this issue? Here is my code:

    //what i use to make the dragBar
    private JLabel dragBar = new JLabel();
    private Point initialClick; //the initial point where I click on the label
    //my mainWindow JFrame
    private JFrame mainWindow = new JFrame();
    private Dimension mainWindowSize = new Dimension(680,410);

    //the code I use to set up my mainWindow JFrame
    mainWindow.setUndecorated(true);
    mainWindow.setShape(new RoundRectangle2D.Double(0, 0, 670, 400, 5, 5));
    mainWindow.setSize(mainWindowSize);
    mainWindow.setMinimumSize(mainWindowSize);
    mainWindow.setResizable(false);
    mainWindow.setLocation((screen_size.width/2)- mainWindow.getWidth()/2, (screen_size.height/2)- mainWindow.getHeight()/2);
    mainWindow.getContentPane().setBackground(new Color(46, 48, 50, 255));

    //the code I use to set up my dragBar label
    topContainer.add(dragBar,3); //a Jlayeredpane that contains the dragBar label and is added to the mainWindow
    dragBar.setSize(topContainer.getSize());
    dragBar.setLocation(0,0);
    dragBar.addMouseListener(new MouseListener() {
        @Override public void mouseClicked(MouseEvent e) {}
        @Override
        public void mousePressed(MouseEvent e) {
            initialClick = e.getPoint();
        }
        @Override public void mouseReleased(MouseEvent e) {}
        @Override public void mouseEntered(MouseEvent e) {}
        @Override public void mouseExited(MouseEvent e) {}
    });
    dragBar.addMouseMotionListener(new MouseMotionListener() {
        @Override
        public void mouseDragged(MouseEvent e) {
            int changeX = e.getX()-initialClick.x;
            int changeY = e.getY()-initialClick.y;

            mainWindow.setLocation(mainWindow.getX()+changeX, mainWindow.getY()+changeY);
        }
        @Override public void mouseMoved(MouseEvent e) {}
    });

a Jlayeredpane that contains the dragBar label

Don't think I would use a JLayeredPane for this. Just add a component to the BorderLayout.PAGE_START of the frame.

The basic logic for dragging a component is something like:

public class DragListener extends MouseInputAdapter
{
    Point location;
    MouseEvent pressed;

    public void mousePressed(MouseEvent me)
    {
        pressed = me;
    }

    public void mouseDragged(MouseEvent me)
    {
        Component component = me.getComponent();
        location = component.getLocation(location);
        int x = location.x - pressed.getX() + me.getX();
        int y = location.y - pressed.getY() + me.getY();
        component.setLocation(x, y);
     }
}

However in your case you don't want to drag the label, but instead drag the window, you your logic needs to forward the events to the window.

Check out Moving Windows for a more complex implementation of the above code that also adds additional features that easily allow you to move a window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM