我试图在我的图形用户界面中显示一个圆形对象,这个圆形对象应该包含一些标签,因此我认为这个圆形对象应该扩展JPanel。有谁知道怎么做一个圆形的JPanel吗?或者至少是一个画了一个椭圆形并在椭圆形中心放置了几个JLables的JPanel?
谢谢
发布于 2010-02-26 17:11:22
要绘制圆,请创建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
发布于 2010-02-26 17:16:21
在O‘’Reilly的Swing Hacks一书中,有一个针对透明和动画窗口#41的技巧。源代码可以从http://examples.oreilly.com/9780596009076/下载
https://stackoverflow.com/questions/2343233
复制