gpt4 book ai didi

java - 如何向 JFrame 添加椭圆边框

转载 作者:行者123 更新时间:2023-12-02 04:47:59 26 4
gpt4 key购买 nike

我有一个未装饰的椭圆形 JFrame,我想为其添加边框。

我希望不必实现 rootPane.paintComponent 方法,并且我可以通过添加边框来实现此目的。

这在 Java 7 或 8 中可能吗?

最佳答案

paintComponent() 的实现中,使用setClip()Ellipse2D大小与图像的 width 相匹配和height .

private Ellipse2D.Double border = new Ellipse2D.Double();

public void paintComponent(Graphics g) {
super.paintComponent()
Graphics2D g2d = (Graphics2D) g;

int width = getWidth();
int height = getHeight();
g2d.setPaint(…);
g2d.fillRect(0, 0, width, height);
border.setFrame(0, 0, width, height);
g2d.setClip(border);
g2d.drawImage(image, 0, 0, width, height, this);
}

同时覆盖 getPreferredSize() ,如图here .

关于java - 如何向 JFrame 添加椭圆边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29505474/

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