圆形 JPanel 摆动

发布于 2024-08-23 15:10:28 字数 138 浏览 1 评论 0原文

我试图在我的 gui 中显示一个圆形对象,该圆形对象应该包含一些标签,因此我认为圆形对象应该扩展 JPanel。有谁知道如何制作一个圆形的JPanel?或者至少有一个 JPanel 绘制一个椭圆形并将一些 JLable 放置在椭圆形的中心?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你与清晨阳光 2024-08-30 15:10:28

要绘制圆形,请子类化JPanel并覆盖paintComponent

public class CirclePanel extends JPanel {

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

如下所示:

alt text 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

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

傲娇萝莉攻 2024-08-30 15:10:28

在 O'Reilly 的《Swing Hacks》一书中,有一个针对透明和动画窗口的 hack #41。源代码可以从http://examples.oreilly.com/9780596009076/下载

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文