本文介绍了如何向JFrame添加椭圆形边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个椭圆形的未经修饰的JFrame,我想添加一个边框.

I have an undecorated JFrame in the shape of an ellipse, that I would like to add a border.

我希望我不必实现rootPane.paintComponent方法,而我可以通过添加边框来做到这一点.

I am hoping I don't have to implement the rootPane.paintComponent method, and that I can just do this by adding a border.

在Java 7或8中可能吗?

Is this possible in Java 7 or 8?

推荐答案

在实现paintComponent()时,使用setClip(),其大小为Ellipse2D以匹配图像的widthheight.

In your implementation of paintComponent(), use setClip() with an Ellipse2D sized to match the image's width and 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(),如此处所示.

这篇关于如何向JFrame添加椭圆形边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 17:35