【发布时间】:2021-04-04 01:29:04
【问题描述】:
我正在尝试在我的 JPanel 中添加一个圆圈,但它不会绘制 cricle。 下面的代码创建了一个 JFrame,创建了一个 JPanle 并调用了一个函数来向 JPanel(pgame) 添加一个圆圈,但实际上并没有添加它。 帮助表示赞赏
fgame = new JFrame("Backgammon");
fgame.setSize(1000, 1000);
pgame = new JPanel();
pgame.setPreferredSize(new Dimension(1000, 687));
pgame.setLayout(new GridLayout(3, 10));
pgame.setBorder(BorderFactory.createEmptyBorder(309,460,150,460));
Circle Circlepanel = new Circle();
pgame.add(Circlepanel);
Circlepanel.setVisible(true);
fgame.add(pgame,BorderLayout.CENTER);
fgame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fgame.setTitle("Backgammon");
fgame.pack();
fgame.setVisible(true);
public class Circle extends JPanel {
public void paint(Graphics g) {
g.drawOval(500, 500, 100, 100);
g.setColor(Color.RED);
g.fillOval(500, 500, 100, 100);
}
}
【问题讨论】:
-
容器在调用paint之前将图形对象的坐标系转换为孩子的位置,因此您可以假设(0,0)是您的Circle面板的左上角。
-
你应该重写paintComponent()而不是paint()。
标签: java swing graphics jframe jpanel