java 通过单击 JPanel 移动未装饰的窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10773713/
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
Moving undecorated window by clicking on JPanel
提问by Jan Kowalski
Is there a possibility to move window by clicking on one of the panels in the window when that window is undecorated?
当窗口未装饰时,是否可以通过单击窗口中的面板之一来移动窗口?
I have a main panel with matte border 40 pixels size, and few panels with controls inside, and I would like to move the window when clicking on that border. Is that possible?
我有一个带有 40 像素大小的磨砂边框的主面板,里面有几个带有控件的面板,我想在单击该边框时移动窗口。那可能吗?
回答by Sorter
You can place another panel over the panel with the border, leaving the border visible.Use the following code to move your window.
您可以在带有边框的面板上放置另一个面板,使边框保持可见。使用以下代码移动窗口。
public class MotionPanel extends JPanel{
private Point initialClick;
private JFrame parent;
public MotionPanel(final JFrame parent){
this.parent = parent;
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
initialClick = e.getPoint();
getComponentAt(initialClick);
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
// get location of Window
int thisX = parent.getLocation().x;
int thisY = parent.getLocation().y;
// Determine how much the mouse moved since the initial click
int xMoved = e.getX() - initialClick.x;
int yMoved = e.getY() - initialClick.y;
// Move window to this position
int X = thisX + xMoved;
int Y = thisY + yMoved;
parent.setLocation(X, Y);
}
});
}
}
I've been working with this code for a while now to make a custom titlebar for undecorated windows. P.S.:You can generalize this example by extending JComponent instead of JPanel.
我已经使用这段代码一段时间了,为未装饰的窗口制作自定义标题栏。PS:你可以通过扩展 JComponent 而不是 JPanel 来概括这个例子。
回答by mKorbel
I have a main panel with matte border 40 pixels size, and few panels with controls inside, and I would like to move the window when clicking on that border
我有一个带有 40 像素大小的磨砂边框的主面板,里面有几个带有控件的面板,我想在单击该边框时移动窗口
I think that ComponetMoverby @camickr is right class for you
我认为@camickr 的ComponetMover适合你
回答by Polygnome
Yes, it is very possible. You need a MouseListener to listen on mouse events. you start moving on mousedown and stop moving on mouseup. Then you simply translate the window position by the same amount the mouse translates during that phase (calculate the delta bewteen old mouse position and new mouse position and add that to the frames position). You should be able to do this with a mouse listener fairly easily.
是的,这是很有可能的。您需要一个 MouseListener 来监听鼠标事件。您开始在 mousedown 上移动并停止在 mouseup 上移动。然后,您只需将窗口位置平移与鼠标在该阶段平移的量相同(计算旧鼠标位置和新鼠标位置之间的增量,并将其添加到帧位置)。您应该可以很容易地使用鼠标侦听器执行此操作。
回答by user12043
I have a simple solution from my project. Here is my undecorated JDialog class.
我的项目有一个简单的解决方案。这是我未修饰的 JDialog 类。
public class TimerDialog extends JDialog {
// some fields here
private Point mouseClickPoint; // Will reference to the last pressing (not clicking) position
private TimerDialog() {
initComponents();
addEventsForDragging();
}
private void addEventsForDragging() {
// Here is the code does moving
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
mouseClickPoint = e.getPoint(); // update the position
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
Point newPoint = event.getLocationOnScreen();
newPoint.translate(-mouseClickPoint.x, -mouseClickPoint.y); // Moves the point by given values from its location
setLocation(newPoint); // set the new location
}
});
}
private void initComponents() {
setLayout(new FlowLayout());
// adding components
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setAlwaysOnTop(true);
setUndecorated(true);
setResizable(false);
pack();
}
}
回答by Long Nguyen
This code works very well with single monitor. It uses a simple mouselistener and mouse motion listener, which do some basic algebra do move the frame.
此代码适用于单显示器。它使用一个简单的鼠标监听器和鼠标运动监听器,它们做一些基本的代数来移动框架。
class Frame extends JFrame{
private int framePositionX, framePositionY, mousePositionX, mousePositionY, newMousePositionX, newMousePositionY;
Frame(){
addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
framePositionX = mouseEvent.getComponent().getX();
framePositionY = mouseEvent.getComponent().getY();
mousePositionX = mouseEvent.getX();
mousePositionY = mouseEvent.getY();
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
framePositionX = mouseEvent.getComponent().getX();
framePositionY = mouseEvent.getComponent().getY();
mousePositionX = mouseEvent.getX();
mousePositionY = mouseEvent.getY();
}
@Override public void mouseReleased(MouseEvent mouseEvent) { }
@Override public void mouseEntered(MouseEvent mouseEvent) { }
@Override public void mouseExited(MouseEvent mouseEvent) { }
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
newMousePositionX = e.getX();
newMousePositionY = e.getY();
int newFramePositionX = (newMousePositionX - mousePositionX)+ framePositionX;
int newFramePositionY = (newMousePositionY - mousePositionY)+ framePositionY;
e.getComponent().setLocation(newFramePositionX, newFramePositionY);
}
});
setUndecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
回答by Pierre A.
int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
thisX + -thisX = 0
这个X + -这个X = 0
int xMoved = e.getX()-initialClick.x;
What i'm using.
我在用什么。
public class MouseLiestenerX implements MouseListener,MouseMotionListener{
private theFrame;
public MouseLiestenerX(Frame theFrame){
this.theFrame = theFrame;
}
private Point startClick;
public void mouseDragged(MouseEvent e) {
int deltaX = e.getX()-startClick.x;
int deltaY = e.getY()-startClick.y;
Core.getSp().setLocation(theFrame.getLocation().x+deltaX, theFrame.getLocation().y+deltaY);
}
public void mousePressed(MouseEvent e) {
startClick = e.getPoint();
}
public void mouseMoved(MouseEvent e){
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
}
and in your Frame constructor
并在您的 Frame 构造函数中
MouseLiestenerX IMove = new MouseLiestenerX(this);
addMouseListener(IMove);
addMouseMotionListener(IMove);