简体   繁体   English

如何移动未装饰的 JFrame window?

[英]How to move an undecorated JFrame window?

So I've been working on a program and I wanted to make it borderless with jframe.setUndecorated(true);所以我一直在研究一个程序,我想用jframe.setUndecorated(true); . . However, I can't really move the JFrame window around the screen.但是,我不能真正在屏幕上移动 JFrame window。 Is there a way I can fix that?有没有办法解决这个问题?

Here is my current code for the JFrame:这是我当前的 JFrame 代码:

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 450);
frame.setLocationRelativeTo(null);
frame.setUndecorated(true);
frame.getContentPane().setBackground(Color.BLACK);

To move the JFrame, left-click inside the JFrame somewhere, drag the mouse to the new position, and release the mouse button when the JFrame is in the correct position. To move the JFrame, left-click inside the JFrame somewhere, drag the mouse to the new position, and release the mouse button when the JFrame is in the correct position.

Here's the complete runnable code.这是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MoveUndecoratedJFrame implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new MoveUndecoratedJFrame());
    }
    
    private JFrame frame;

    @Override
    public void run() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        
        frame.add(createMainPanel(), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        MoveListener listener = new MoveListener();
        panel.addMouseListener(listener);
        panel.addMouseMotionListener(listener);
        panel.setBackground(Color.BLACK);
        panel.setPreferredSize(new Dimension(700, 400));
        
        return panel;
    }
    
    public class MoveListener implements MouseListener, MouseMotionListener {
        
        private Point pressedPoint;
        private Rectangle frameBounds;
        
        @Override
        public void mouseClicked(MouseEvent event) {
        }

        @Override
        public void mousePressed(MouseEvent event) {
            this.frameBounds = frame.getBounds();
            this.pressedPoint = event.getPoint();
        }

        @Override
        public void mouseReleased(MouseEvent event) {
            moveJFrame(event);
        }

        @Override
        public void mouseEntered(MouseEvent event) {
        }

        @Override
        public void mouseExited(MouseEvent event) {
        }

        @Override
        public void mouseDragged(MouseEvent event) {
            moveJFrame(event);
        }

        @Override
        public void mouseMoved(MouseEvent event) {
        }
        
        private void moveJFrame(MouseEvent event) {
            Point endPoint = event.getPoint();
            
            int xDiff = endPoint.x - pressedPoint.x;
            int yDiff = endPoint.y - pressedPoint.y;
            frameBounds.x += xDiff;
            frameBounds.y += yDiff;
            frame.setBounds(frameBounds);
        }
        
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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