使 java 摆动框架可移动和 setUndecorated
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16046824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a java swing frame movable and setUndecorated
提问by Imri Persiado
I have created a frame without the title bar, for that I used the setUndecorated(true); method but after that the frame is became unmovable for some reason.
我创建了一个没有标题栏的框架,为此我使用了 setUndecorated(true); 方法,但在那之后框架由于某种原因变得不可移动。
How can I make my frame movable and still hide my title bar?
如何使我的框架可移动并仍然隐藏我的标题栏?
回答by Erik Pragt
The following code will create a JFrame without a title bar, which you can still move around:
以下代码将创建一个没有标题栏的 JFrame,您仍然可以移动它:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class FrameDragListenerExample {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
final JFrame frame = new JFrame("Hello");
frame.setUndecorated(true);
frame.setBounds(0, 0, 400, 400);
JPanel contentPane = new JPanel(new BorderLayout());
JLabel label = new JLabel("Click anywhere in the Jframe and drag");
label.setFont(label.getFont().deriveFont(16f));
label.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
contentPane.add(label);
frame.setContentPane(contentPane);
FrameDragListener frameDragListener = new FrameDragListener(frame);
frame.addMouseListener(frameDragListener);
frame.addMouseMotionListener(frameDragListener);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
};
SwingUtilities.invokeLater(runnable);
}
public static class FrameDragListener extends MouseAdapter {
private final JFrame frame;
private Point mouseDownCompCoords = null;
public FrameDragListener(JFrame frame) {
this.frame = frame;
}
public void mouseReleased(MouseEvent e) {
mouseDownCompCoords = null;
}
public void mousePressed(MouseEvent e) {
mouseDownCompCoords = e.getPoint();
}
public void mouseDragged(MouseEvent e) {
Point currCoords = e.getLocationOnScreen();
frame.setLocation(currCoords.x - mouseDownCompCoords.x, currCoords.y - mouseDownCompCoords.y);
}
}
}
You can still drag it around by dragging the body of the frame.
您仍然可以通过拖动框架的主体来拖动它。
回答by DRastislav
Maybe this will help you Moving Window
也许这会帮助你 移动窗口
回答by Super-ilad
I encapsulate a extended JFrame
class for you, I called it MoveaFrame
, you just need to "extend MoveaFrame" in your practice:
我JFrame
给你封装了一个扩展类,我叫它MoveaFrame
,你只需要在你的实践中“扩展MoveaFrame”即可:
Just copy below codes to your project, and extend it, you can make your Frame window draggable!
只需将以下代码复制到您的项目中,并扩展它,您就可以使您的 Frame 窗口可拖动!
Extend MoveJFrame
like extend a JFrame
, you can directly drag your window:
延长MoveJFrame
像扩展JFrame
,你可以直接拖动窗口:
public class ContactUi extends MoveJFrame implements Runnable {
The MoveJFrame
class code, just copy it and extend it like extend JFrame
:
在MoveJFrame
类代码,只需将它复制并扩展它就像延伸JFrame
:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
public class MoveJFrame extends JFrame {
public MoveJFrame() {
this.setUndecorated(true);
FrameDragListener frameDragListener = new FrameDragListener(this);
this.addMouseListener(frameDragListener);
this.addMouseMotionListener(frameDragListener);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) throws IOException {
new MoveJFrame();
}
public static class FrameDragListener extends MouseAdapter {
private final JFrame frame;
private Point mouseDownCompCoords = null;
public FrameDragListener(JFrame frame) {
this.frame = frame;
}
public void mouseReleased(MouseEvent e) {
mouseDownCompCoords = null;
}
public void mousePressed(MouseEvent e) {
mouseDownCompCoords = e.getPoint();
}
public void mouseDragged(MouseEvent e) {
Point currCoords = e.getLocationOnScreen();
frame.setLocation(currCoords.x - mouseDownCompCoords.x, currCoords.y - mouseDownCompCoords.y);
}
}
}