gpt4 book ai didi

java - RoundRect 不适合 JFrame

转载 作者:行者123 更新时间:2023-12-01 23:15:06 28 4
gpt4 key购买 nike

我有一个 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 中我有:

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRoundRect(0, 0, 500, 500, 50, 50);
}

我希望 roundRect 完全适合 JFrame,并且 roundRect 的边缘接触 JFrame 的边缘。

最佳答案

I am drawing a fillRoundRect with the same dimensions as the JFrame.

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

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

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

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

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

关于java - RoundRect 不适合 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58356303/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com