我想让边框透明,所以我尝试使用我自己的JFrame类来做到这一点……
private class ShadowBorder extends AbstractBorder {
private static final int RADIUS = 30;
@Override
public boolean isBorderOpaque() {
return false;
}
@Override
public Insets getBorderInsets(Component c) {
return new Insets(RADIUS, RADIUS, RADIUS, RADIUS);
}
@Override
public Insets getBorderInsets(Component c, Insets insets) {
insets.top = RADIUS;
insets.left = RADIUS;
insets.bottom = RADIUS;
insets.right = RADIUS;
return insets;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(new Color(0x66000000, true));
g2d.fillRect(0, 0, width - RADIUS, RADIUS);
}
}但是最终的边框是不透明的。边框内也有白色背景,但我不知道为什么(见atach )。图像)。有什么想法吗?

谢谢!
发布于 2012-05-02 17:13:18
您需要将窗口设置为非不透明,并在Graphics上使用复合窗口。此外,在您的代码中,您只打印一个边框,而不是四个边框,这就是为什么您只看到一个边框绘制的原因。像这样的东西应该可以做到(尽管基于insets而不是半径常量来绘制边框会更好):
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.io.UnsupportedEncodingException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.AbstractBorder;
import com.sun.awt.AWTUtilities;
public class Main {
private static class ShadowBorder extends AbstractBorder {
private static final int RADIUS = 30;
@Override
public boolean isBorderOpaque() {
return false;
}
@Override
public Insets getBorderInsets(Component c) {
return new Insets(RADIUS, RADIUS, RADIUS, RADIUS);
}
@Override
public Insets getBorderInsets(Component c, Insets insets) {
insets.top = RADIUS;
insets.left = RADIUS;
insets.bottom = RADIUS;
insets.right = RADIUS;
return insets;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(new Color(66, 0, 0));
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, 0.5f));
g2d.fillRect(0, 0, width - RADIUS, RADIUS);
g2d.fillRect(width - RADIUS, 0, RADIUS, height - RADIUS);
g2d.fillRect(0, RADIUS, RADIUS, height - RADIUS);
g2d.fillRect(RADIUS, height - RADIUS, width - RADIUS, RADIUS);
}
}
public static void main(String[] args) throws UnsupportedEncodingException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
AWTUtilities.setWindowOpaque(frame, false);
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("Hello");
panel.add(button);
panel.setBorder(new ShadowBorder());
frame.setContentPane(panel);
frame.setSize(300, 200);
frame.setVisible(true);
}
}发布于 2012-05-02 16:42:32
要使JFrame具有自定义透明边框,您必须首先更改框架的不透明度:
frame.setUndecorated ( true );
AWTUtilities.setWindowOpaque ( frame, false );在那之后,你的框架将不会有任何系统边框和背景,所以你可以画一些透明的东西(比如边框)。顺便说一句,你改变边框的组件必须是非透明的,以及组件下的容器,才能让帧后面的像素“通过”。
在某些情况下,您还可以使整个框架半透明:
AWTUtilities.setWindowOpacity ( frame, 0.5f );这将使它50%透明(它甚至可以与系统窗口装饰一起工作)。
虽然这些功能只在Win和Mac系统上得到了正确的支持。
https://stackoverflow.com/questions/10409500
复制相似问题