有没有通过点击窗口中的面板之一时,未加装饰的那个窗口移动窗口的可能性?
我有磨砂边框40像素尺寸的主面板,并与对照几屏里面,我想对边境点击时移动窗口。 那可能吗?
有没有通过点击窗口中的面板之一时,未加装饰的那个窗口移动窗口的可能性?
我有磨砂边框40像素尺寸的主面板,并与对照几屏里面,我想对边境点击时移动窗口。 那可能吗?
您可以将另一个小组在与边框面板 ,而使边境visible.Use下面的代码来移动你的窗口。
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:您可以通过扩展来代替的JPanel的JComponent概括这个例子。
我有磨砂边框40个像素尺寸的主面板,并与对照几屏内,并就边境点击,当我想移动窗口
我认为, ComponetMover通过@camickr是正确类你
是的,这是很有可能的。 你需要的MouseListener监听鼠标事件。 你开始移动鼠标按下上并停止移动的鼠标松开。 然后,你只需通过翻译在该阶段鼠标平移相同量的窗口位置(计算增量bewteen旧鼠标位置和新的鼠标位置,并添加到帧的位置)。 你应该能够用鼠标监听器要做到这一点很容易。
我从我的项目一个简单的解决方案。 这是我未加装饰的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();
}
}
此代码的工作非常好,单显示器。 它使用一个简单的MouseListener和鼠标移动侦听器,其中做一些基本的代数不移动框。
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);
}
}
int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
thisX + -thisX = 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) {
}
}
在你的框架构造
MouseLiestenerX IMove = new MouseLiestenerX(this);
addMouseListener(IMove);
addMouseMotionListener(IMove);