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 一书中,有一个用于透明和动画窗口的 hack #41。源代码可以从http://examples.oreilly.com/9780596009076/下载

于 2010-02-26T17:16:20.630 回答