开发者

Circular JPanel Swing

I am trying to display a circular object in my gui, the circular object should contain a few labels therefore I thought the circle object should extend JPanel. 开发者_如何学运维Does anyone know how to make a circular JPanel? Or at least a JPanel which paints an oval and places a few JLables in the centre of the oval?

Thanks


To paint a circle, subclass JPanel and override paintComponent:

public class CirclePanel extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        g.drawOval(0, 0, g.getClipBounds().width, g.getClipBounds().height);
    }
}

Looks like this:

alt text http://img246.imageshack.us/img246/3708/so2343233.png

To place the labels, you could use GridBagLayout, hope that's what you want:

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);

alt text http://img694.imageshack.us/img694/4013/so23432332.png


In the book Swing Hacks from O'Reilly there is a hack for transparent and animated windows #41. The source code can be downloaded from http://examples.oreilly.com/9780596009076/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜