GUIで円形オブジェクトを表示しようとしています。円形オブジェクトにはいくつかのラベルが含まれている必要があるため、円オブジェクトはJPanelを拡張する必要があると考えました。円形の JPanel を作成する方法を知っている人はいますか? または、少なくとも楕円形をペイントし、楕円形の中心にいくつかの JLables を配置する JPanel?
ありがとう
円を描画するには、サブクラス化JPanel
してオーバーライドしますpaintComponent
。
public class CirclePanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
g.drawOval(0, 0, g.getClipBounds().width, g.getClipBounds().height);
}
}
次のようになります。
代替テキスト http://img246.imageshack.us/img246/3708/so2343233.png
ラベルを配置するには、 を使用できますGridBagLayout
。
CirclePanel panel = new CirclePanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gc;
gc = new GridBagConstraints();
gc.gridy = 0;
panel.add(new JLabel("Label 1"), gc);
gc = new GridBagConstraints();
gc.gridy = 1;
panel.add(new JLabel("Label 2"), gc);
代替テキスト http://img694.imageshack.us/img694/4013/so23432332.png
O'Reilly の Swing Hacks という本には、透明でアニメーション化されたウィンドウのハック #41 があります。ソース コードはhttp://examples.oreilly.com/9780596009076/からダウンロードできます。