How to move undecorated JFrame by holding click on a JPanel in Java? [duplicate]

瘦欲@ 提交于 2020-04-15 04:33:12

问题


I've been making an undecorated JFrame so far and I was wondering if it's possible to move the undecorated JFrame by holding click on a JPanel.

Here is the source code I'm working on.

private static void createFrame()
{
    JFrame frame = new JFrame("Text Frame");
    frame.setLayout(null);
    frame.setSize(500,300);
    frame.setUndecorated(true);

    JPanel panel = new JPanel();
    panel.setBounds(0, 2, 500, 50);
    panel.setBackground(new Color(60, 65, 70));
    frame.add(panel);

    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

What I want to do is: Click and hold my cursor at the JPanel area to be able to move the JFrame.

I did some research and came across to a similar question: Moving undecorated window by clicking on JPanel

I don't understand how to integrate the code provided (by user Sorter) on my code.

Or are there another solutions?


回答1:


The solution provided can easily be integrated into your example.

Just add Sorter's example as a separate class.

Then change

JPanel panel = new JPanel();

to

JPanel panel = new MotionPanel(frame);

The panel should now be movable.




回答2:


Check out Moving Windows for a class that does this for you.

The class is designed to allow you to move a window on the desktop, or components on a panel.



来源:https://stackoverflow.com/questions/29069167/how-to-move-undecorated-jframe-by-holding-click-on-a-jpanel-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!