0

GUIで円形オブジェクトを表示しようとしています。円形オブジェクトにはいくつかのラベルが含まれている必要があるため、円オブジェクトはJPanelを拡張する必要があると考えました。円形の JPanel を作成する方法を知っている人はいますか? または、少なくとも楕円形をペイントし、楕円形の中心にいくつかの JLables を配置する JPanel?

ありがとう

4

2 に答える 2

7

円を描画するには、サブクラス化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

于 2010-02-26T17:11:21.730 に答える
0

O'Reilly の Swing Hacks という本には、透明でアニメーション化されたウィンドウのハック #41 があります。ソース コードはhttp://examples.oreilly.com/9780596009076/からダウンロードできます。

于 2010-02-26T17:16:20.630 に答える