概要
Screenshot
サンプルコード
JDesktopPane desktop = new JDesktopPane();
desktop.setDesktopManager(new DefaultDesktopManager() {
@Override public void beginDraggingFrame(JComponent f) {
setDraggingFrame(f);
super.beginDraggingFrame(f);
}
@Override public void endDraggingFrame(JComponent f) {
setDraggingFrame(null);
super.endDraggingFrame(f);
f.repaint();
}
@Override public void beginResizingFrame(JComponent f, int direction) {
setDraggingFrame(f);
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
super.beginResizingFrame(f, direction);
}
@Override public void endResizingFrame(JComponent f) {
setDraggingFrame(null);
desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
super.endResizingFrame(f);
}
});
View in GitHub: Java, Kotlin
解説
DesktopManager#beginDraggingFrame(...)をオーバーライドしてマウスでドラッグ中のJInternalFrameを記憶
DefaultDesktopManagerではJInternalFrame#isDraggingを切り替えているが、このフィールドはパッケージプライベートなのでこのサンプルでは使用できない
JInternalFrame#paintComponent(...)をオーバーライドして自身がドラッグ中の場合は半透明で描画する
JInternalFrame frame = new JInternalFrame(title, true, true, true, true) {
@Override protected void paintComponent(Graphics g) {
// if (isDragging) { // JInternalFrame#isDragging: package private
if (getDraggingFrame() == this) {
((Graphics2D) g).setComposite(
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .2f));
}
super.paintComponent(g);
}
};
DesktopManager#endDraggingFrame(...)をオーバーライドしてマウスでドラッグ中のJInternalFrameの参照をクリアし、自身を再描画
JDesktopPane.LIVE_DRAG_MODEではドラッグ移動終了後自動的に再描画は実行されないので、ここでJInternalFrame.repaint()を実行しないとフレームが半透明のままになってしまう
- 同様に
DesktopManager#beginResizingFrame(...)、DesktopManager#endResizingFrame(...)をオーバーライドしてリサイズ中のJInternalFrameを半透明に変更することも可能
- このサンプルでは
JDesktop#setDragMode(...)でリサイズ中開始時にJDesktopPane.OUTLINE_DRAG_MODE、リサイズ終了時にJDesktopPane.LIVE_DRAG_MODEに切り替えてリサイズ中は完全透明にしている
参考リンク
コメント