我试图在我的 gui 中显示一个圆形对象,该圆形对象应该包含一些标签,因此我认为圆形对象应该扩展 JPanel。有谁知道如何制作一个圆形的JPanel?或者至少有一个 JPanel 绘制一个椭圆形并在椭圆形中心放置一些 JLable?
谢谢
最佳答案
要绘制圆形,请子类化 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
关于java - 圆形 JPanel Swing ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2343233/