代码之家  ›  专栏  ›  技术社区  ›  Aly

圆形J面板摆动

  •  0
  • Aly  · 技术社区  · 15 年前

    我试图在图形用户界面中显示一个圆形对象,圆形对象应该包含一些标签,因此我认为圆形对象应该扩展jpanel。有人知道怎么做圆形jpanel吗?或者至少是一个画椭圆的jpanel,在椭圆的中心放置一些jtables?

    谢谢

    2 回复  |  直到 15 年前
        1
  •  7
  •   Peter Lang    15 年前

    绘制圆,子类 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);
    

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

        2
  •  0
  •   Kennet    15 年前

    在O'Reilly的Swing Hacks一书中,有一个关于透明和动画窗口的Hack 41。源代码可从下载 http://examples.oreilly.com/9780596009076/