1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
// ici "this" est la barre titre que j'ai créé pour remplacer celle de Windows
void installListeners() {
MouseInputHandler handler = new MouseInputHandler();
Window window = SwingUtilities.getWindowAncestor(this);
window.addMouseListener(handler);
window.addMouseMotionListener(handler);
}
private class MouseInputHandler implements MouseInputListener {
private boolean isMovingWindow;
private int dragOffsetX;
private int dragOffsetY;
private static final int BORDER_DRAG_THICKNESS = 5;
public void mousePressed(MouseEvent ev) {
Point dragWindowOffset = ev.getPoint();
Window w = (Window)ev.getSource();
if (w != null) {
w.toFront();
}
Point convertedDragWindowOffset = SwingUtilities.convertPoint(
w, dragWindowOffset, TitlePanel.this);
Frame f = null;
Dialog d = null;
if (w instanceof Frame) {
f = (Frame)w;
} else if (w instanceof Dialog) {
d = (Dialog)w;
}
int frameState = (f != null) ? f.getExtendedState() : 0;
if (TitlePanel.this.contains(convertedDragWindowOffset)) {
if ((f != null && ((frameState & Frame.MAXIMIZED_BOTH) == 0)
|| (d != null))
&& dragWindowOffset.y >= BORDER_DRAG_THICKNESS
&& dragWindowOffset.x >= BORDER_DRAG_THICKNESS
&& dragWindowOffset.x < w.getWidth()
- BORDER_DRAG_THICKNESS) {
isMovingWindow = true;
dragOffsetX = dragWindowOffset.x;
dragOffsetY = dragWindowOffset.y;
}
}
else if (f != null && f.isResizable()
&& ((frameState & Frame.MAXIMIZED_BOTH) == 0)
|| (d != null && d.isResizable())) {
dragOffsetX = dragWindowOffset.x;
dragOffsetY = dragWindowOffset.y;
}
}
public void mouseReleased(MouseEvent ev) {
isMovingWindow = false;
}
public void mouseDragged(MouseEvent ev) {
Window w = (Window)ev.getSource();
if (isMovingWindow) {
Point windowPt = MouseInfo.getPointerInfo().getLocation();
windowPt.x = windowPt.x - dragOffsetX;
windowPt.y = windowPt.y - dragOffsetY;
w.setLocation(windowPt);
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
} |
Partager