当窗口未装饰时,是否可以通过单击窗口中的一个面板来移动窗口?
我有一个带有 40 像素大小的哑光边框的主面板,以及几个带有控件的面板,我想在单击该边框时移动窗口。那可能吗?
您可以在具有边框的面板上放置另一个面板,使边框可见。使用以下代码移动您的窗口。
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);
}
});
}
}
我一直在使用这段代码为未装饰的窗口制作自定义标题栏。PS:您可以通过扩展 JComponent 而不是 JPanel 来概括此示例。
我有一个带有 40 像素大小的哑光边框的主面板,以及几个带有控件的面板,我想在单击该边框时移动窗口
我认为@camickr 的ComponetMover适合你
是的,很有可能。您需要一个 MouseListener 来监听鼠标事件。您开始在 mousedown 上移动并在 mouseup 上停止移动。然后,您只需将窗口位置平移与鼠标在该阶段平移的量相同的量(计算旧鼠标位置和新鼠标位置之间的增量并将其添加到框架位置)。你应该可以很容易地用鼠标监听器做到这一点。
我的项目有一个简单的解决方案。这是我未装饰的 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();
}
}
int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
这个X + -这个X = 0
int xMoved = e.getX()-initialClick.x;
我正在使用什么。
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) {
}
}
并在您的 Frame 构造函数中
MouseLiestenerX IMove = new MouseLiestenerX(this);
addMouseListener(IMove);
addMouseMotionListener(IMove);