问题
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