为了账号安全,请及时绑定邮箱和手机立即绑定

RoundRect 不适合 JFrame

RoundRect 不适合 JFrame

隔江千里 2023-10-19 21:25:40
我有一个 JFrame,其中有一个扩展 JPanel 的类,并使用 BorderLayout.CENTER 添加到其中。在 JPanel 子类中的 PaintComponent 方法中,我正在绘制一个与 JFrame 尺寸相同的 fillRoundRect。当我运行它时,roundRect 的底部和右侧部分被切断。在主课中我有这个:SwingUtilities.invokeLater(() -> {    JFrame frame = new JFrame();    PlayingField playingField = new PlayingField(); // subclass of JPanel    frame.add(playingField, BorderLayout.CENTER);    frame.setSize(500, 500);    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    frame.setVisible(true);});在 PlayingField 中我有:@Overrideprotected void paintComponent(Graphics g) {    super.paintComponent(g);    g.setColor(Color.BLACK);    g.fillRoundRect(0, 0, 500, 500, 50, 50);}我希望 roundRect 完美地适合 JFrame,并且 roundRect 的边缘接触 JFrame 的边缘。
查看完整描述

1 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

我正在绘制一个与 JFrame 尺寸相同的 fillRoundRect。

您不应该使用与框架相同的尺寸。框架包括装饰(标题栏和边框)。

另外,您不应该对值进行硬编码。如果调整框架大小,您的绘画将如何工作?

相反,您应该使用面板的实际大小:

g.fillRoundRect(0, 0, getWidth(), getHeight(), 50, 50);

现在,绘画是根据面板的大小动态变化的。



查看完整回答
反对 回复 2023-10-19
  • 1 回答
  • 0 关注
  • 58 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信