按照Oracle 的 Myopia 指南,我有一个简单JPanel
的添加到 aJFrame
作为JLayer
. 很简单,这模糊了JPanel
's 的组件。但是,我试图在JPanel
此之上添加一秒JPanel
(这意味着它不会变得模糊)。
JPanel
与主要方法一起简单:
public class ContentPanel extends JPanel {
public ContentPanel() {
setLayout(new BorderLayout());
add(new JLabel("Hello world, this is blurry!"), BorderLayout.NORTH);
add(new JLabel("Hello world, this is blurry!"), BorderLayout.CENTER);
add(new JButton("Blurry button"), BorderLayout.SOUTH);
}
public static void main(String[] args) {
JFrame f = new JFrame("Foo");
f.setSize(300, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
LayerUI<JComponent> layerUI = new BlurLayerUI();
JPanel panel = new ContentPanel();
JLayer<JComponent> jlayer = new JLayer<JComponent>(panel, layerUI);
f.add(jlayer);
f.setVisible(true);
}
}
BlurLayerUI
这模糊了它的“孩子”:
class BlurLayerUI extends LayerUI<JComponent> {
private BufferedImage mOffscreenImage;
private BufferedImageOp mOperation;
public BlurLayerUI() {
float ninth = 1.0f / 9.0f;
float[] blurKernel = { ninth, ninth, ninth, ninth, ninth, ninth, ninth,
ninth, ninth };
mOperation = new ConvolveOp(new Kernel(3, 3, blurKernel),
ConvolveOp.EDGE_NO_OP, null);
}
@Override
public void paint(Graphics g, JComponent c) {
int w = c.getWidth();
int h = c.getHeight();
if (w == 0 || h == 0) {
return;
}
// Only create the offscreen image if the one we have
// is the wrong size.
if (mOffscreenImage == null || mOffscreenImage.getWidth() != w
|| mOffscreenImage.getHeight() != h) {
mOffscreenImage = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
}
Graphics2D ig2 = mOffscreenImage.createGraphics();
ig2.setClip(g.getClip());
super.paint(ig2, c);
ig2.dispose();
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(mOffscreenImage, mOperation, 0, 0);
}
}
这将产生以下内容:
我试图简单地将第二个添加JPanel
到第一个JFrame
之后,这只会导致第二个面板占用所有空间。使用各种布局管理器和set-Maximum/Preferred-size()
方法不会有任何好处。也不会使第二个面板背景透明。
如何在 a 上方添加JPanel
固定大小的 aJLayer
,从而允许第一个面板的一部分出现(仍然模糊)?