不久前我实现了一个解决方案JXLayer
,它使用它的锁定效果功能在容器上提供一个“阻塞”层。
它基于JXLayer 3.x并使用来自JHLabs的过滤器来生成它的“灰度”效果
import com.jhlabs.image.GrayscaleFilter;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.jdesktop.jxlayer.JXLayer;
import org.jdesktop.jxlayer.QualityHints;
import org.jdesktop.jxlayer.plaf.BetterBufferedImageOpEffect;
import org.jdesktop.jxlayer.plaf.LayerUI;
import org.jdesktop.jxlayer.plaf.ext.LockableUI;
public class CoreXPane extends JPanel {
private JXLayer<JPanel> layer;
private FadedLockUI fadedLockUI;
private JPanel pnlBody;
public CoreXPane(LayoutManager layout) {
super.setLayout(new BorderLayout());
super.addImpl(getLayer(), BorderLayout.CENTER, 0);
setLayout(layout);
}
public CoreXPane() {
this(new BorderLayout());
}
@Override
public void setEnabled(boolean enabled) {
getLockUI().setLocked(!enabled);
getBodyPane().setEnabled(enabled);
super.setEnabled(enabled);
}
@Override
protected void addImpl(Component comp, Object constraints, int index) {
getBodyPane().add(comp, constraints, index);
}
@Override
public void remove(int index) {
getBodyPane().remove(index);
}
@Override
public void removeAll() {
getBodyPane().removeAll();
}
protected FadedLockUI getLockUI() {
if (fadedLockUI == null) {
fadedLockUI = new FadedLockUI();
}
return fadedLockUI;
}
@Override
public void invalidate() {
getLockUI().invalidate();
super.invalidate();
}
@Override
public void revalidate() {
getLockUI().revalidate();
super.revalidate();
}
@Override
public void repaint() {
getLockUI().repaint();
super.repaint();
}
protected void getLayers(List<LayerUI> layers) {
layers.add(getLockUI());
}
protected JXLayer<JPanel> getLayer() {
if (layer == null) {
List<LayerUI> layers = new ArrayList<LayerUI>(4);
getLayers(layers);
JComponent wrapper = getBodyPane();
for (LayerUI ui : layers) {
wrapper = new JXLayer(wrapper, ui);
}
layer = (JXLayer<JPanel>) wrapper;
}
return layer;
}
@Override
public void setLayout(LayoutManager mgr) {
getBodyPane().setLayout(mgr);
}
@Override
public LayoutManager getLayout() {
return getBodyPane().getLayout();
}
public JPanel getBodyPane() {
if (pnlBody == null) {
pnlBody = new JPanel();
pnlBody.setLayout(new BorderLayout());
}
return pnlBody;
}
@Override
public void setOpaque(boolean isOpaque) {
super.setOpaque(isOpaque);
getBodyPane().setOpaque(isOpaque);
}
public static class FadedLockUI extends LockableUI {
public static Map<RenderingHints.Key, Object> mapRenderHints = new QualityHints();
public FadedLockUI() {
setLockedEffects(new BufferedImageOpEffect(new GrayscaleFilter()));
mapRenderHints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); // okay
mapRenderHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // bad
mapRenderHints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); // okay
mapRenderHints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
mapRenderHints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
mapRenderHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
mapRenderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
}
@Override
protected Map<RenderingHints.Key, Object> getRenderingHints(JXLayer<? extends JComponent> l) {
return mapRenderHints;
}
public void repaint() {
setDirty(true);
}
public void invalidate() {
setDirty(true);
}
public void revalidate() {
setDirty(true);
}
}
}
查看LockableUI了解更多详情