Swing/WindowShape のバックアップ(No.15)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/WindowShape へ行く。
- 1 (2011-12-19 (月) 19:46:54)
- 2 (2012-03-02 (金) 17:24:36)
- 3 (2012-12-14 (金) 13:43:51)
- 4 (2012-12-26 (水) 05:54:10)
- 5 (2014-12-03 (水) 00:46:42)
- 6 (2015-02-09 (月) 17:17:36)
- 7 (2016-12-02 (金) 17:56:05)
- 8 (2017-11-29 (水) 20:35:57)
- 9 (2019-08-01 (木) 22:55:54)
- 10 (2021-04-01 (木) 04:57:14)
- 11 (2025-01-03 (金) 08:57:02)
- 12 (2025-01-03 (金) 09:01:23)
- 13 (2025-01-03 (金) 09:02:38)
- 14 (2025-01-03 (金) 09:03:21)
- 15 (2025-01-03 (金) 09:04:02)
- category: swing
folder: WindowShape
title: Windowの形を変更
tags: [JFrame, Shape, TextLayout]
author: aterai
pubdate: 2011-12-19T19:46:54+09:00
description: JFrameのタイトルバーなどを非表示にし、Windowの形を非矩形図形に変更します。
image:
Summary
JFrame
のタイトルバーなどを非表示にし、Window
の形を非矩形図形に変更します。
Screenshot

Advertisement
Source Code Examples
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().setBackground(Color.GREEN);
frame.pack();
String str = textField.getText().trim();
TextLayout tl = new TextLayout(str, font, frc);
Rectangle2D b = tl.getBounds();
Shape shape = tl.getOutline(AffineTransform.getTranslateInstance(-b.getX(), -b.getY()));
frame.setBounds(shape.getBounds());
// JDK 1.6.0: com.sun.awt.AWTUtilities.setWindowShape(frame, shape);
frame.setShape(shape);
frame.setLocationRelativeTo(parent);
frame.setVisible(true);
View in GitHub: Java, KotlinExplanation
上記のサンプルでは、JDK 1.7.0
で導入されたWindow#setShape(Shape)
メソッドを使用してJFrame
の形を変更しています。
JDK 1.6.0_10
の場合はcom.sun.awt.AWTUtilities.setWindowShape(...)
を使用する
Reference
- Translucent and Shaped Windows in JDK7 (The Java Tutorials' Weblog)
- Window#setShape(Shape) (Java Platform SE 8)