Swing/DragWindow のバックアップの現在との差分(No.1)
TITLE:JWindowをマウスで移動
JWindowをマウスで移動
編集者:Terai Atsuhiro~
作成日:2004-09-06
更新日:2024-02-03 (土) 14:10:31
概要
JWindow
などのタイトルバーのないフレームをマウスで移動します。
Screenshot
Advertisement
概要
JWindowなどのタイトルバーのないフレームをマウスで移動します。サンプルコード
#spanend
#spanadd
public void createSplashScreen(String path) {
#spanend
ImageIcon img = new ImageIcon(getClass().getResource(path));
DragWindowListener dwl = new DragWindowListener();
splashLabel = new JLabel(img);
splashLabel.addMouseListener(dwl);
splashLabel.addMouseMotionListener(dwl);
splashScreen = new JWindow(getFrame());
splashScreen.getContentPane().add(splashLabel);
splashScreen.pack();
splashScreen.setLocationRelativeTo(null);
#spanadd
}
#spanend
#spandel
#screenshot
#spanend
#spanadd
class DragWindowListener extends MouseAdapter {
#spanend
private final Point startPt = new Point();
private Window window;
#spandel
**サンプルコード [#r3a65b7b]
#spanend
public void createSplashScreen(String path) {
ImageIcon img = new ImageIcon(getClass().getResource(path));
splashLabel = new JLabel(img);
splashLabel.addMouseListener(new MyMouseListener());
splashLabel.addMouseMotionListener(new MyMouseMotionListener());
splashScreen = new JWindow(getFrame());
splashScreen.getContentPane().add(splashLabel);
splashScreen.pack();
splashScreen.setLocationRelativeTo(null);
}
private MouseEvent start;
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent me) {
start = me;
}
}
class MyMouseMotionListener extends MouseMotionAdapter {
private Point loc;
private Window frame;
public void mouseDragged(MouseEvent me) {
if(frame==null) {
Component cmp = (Component)me.getSource();
frame = SwingUtilities.windowForComponent(cmp);
}
loc = frame.getLocation(loc);
int x = loc.x - start.getX() + me.getX();
int y = loc.y - start.getY() + me.getY();
frame.setLocation(x, y);
}
}
@Override public void mousePressed(MouseEvent me) {
startPt.setLocation(me.getPoint());
}
-&jnlp;
-&jar;
-&zip;
@Override public void mouseDragged(MouseEvent me) {
if (window == null) {
window = SwingUtilities.windowForComponent(me.getComponent());
}
Point eventLocationOnScreen = me.getLocationOnScreen();
window.setLocation(eventLocationOnScreen.x - startPt.x,
eventLocationOnScreen.y - startPt.y);
// loc = window.getLocation(loc);
// int x = loc.x - start.getX() + me.getX();
// int y = loc.y - start.getY() + me.getY();
// window.setLocation(x, y);
}
#spanadd
}
#spanend
#spanadd
View in GitHub: Java, Kotlin解説
JWindowや、setUndecorated(true)したJFrameのようにタイトルバーのないフレームをマウスのドラッグで移動します。実際はJWindowに、MouseMotionListenerなどを実装したコンポーネントを追加しています。解説
JWindow
やsetUndecorated(true)
したJFrame
のようにタイトルバーのないフレームをマウスのドラッグで移動します。実際はJWindow
自体にリスナーを設定するのではなく子コンポーネントにMouseMotionListener
などを追加しています。
上記のサンプルでは、Splash ScreenのJLabelにリスナを追加し、これをJWindowに貼り付けてドラッグできるようにしています。
上記のサンプルではJLabel
にリスナーを追加し、これをJWindow
に配置してドラッグ可能にしています。
次に開くsetUndecorated(true)したJFrameでは、青いラベル部分をドラッグすることが出来ます。
- スプラッシュスクリーンの次に開く
JFrame
はJFrame#setUndecorated(true)
を設定してタイトルバーなどは非表示 - 代わりに青いラベル部分をドラッグ可能に設定
コメント
- -
- マルチディスプレイなどで別画面に移動できないバグ?を修正
- ただし
Web Start
のSandBox
内では以前と同じく画面の外までは移動不可?-
JNLP
のセキュリティにall-permissions
を設定する必要がある
-
- ただし
-
Swing Tutorial
の FrameDemo2で試しても、同様?-
Look and feel decorated
: 画面外に移動不可 -
Window system decorated
: 画面外に移動可能
-