我想要将JDesktopPane设置为透明,并允许我单击下面的内容(例如桌面图标等)。内部框架应该保持不透明,并且能够像现在一样在屏幕周围重新定位。有什么想法吗?
package test1;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
public class Test1 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
JDesktopPane desktop = new JDesktopPane();
JInternalFrame internalFrame = new JInternalFrame("Title", false, true, false, true);
desktop.setBackground(Color.RED);
//desktop.setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
desktop.add(internalFrame);
internalFrame.setBounds(25, 25, 200, 100);
internalFrame.setVisible(true);
frame.add(desktop, BorderLayout.CENTER);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
}发布于 2015-12-07 05:31:09
第1部分半透明JInternalFrame
创建半透明组件比仅仅将背景颜色设置为基于alpha的值要复杂得多。
Swing只知道如何绘制不透明或完全透明的组件,API也进行了优化,如果它更新一个不透明的组件,它不会更新组件后面的父级区域。使用基于alpha的颜色将生成各种令人讨厌的绘画伪像
相反,你需要“伪造”它。这涉及到将组件设置为完全覆盖components的paint方法,并使用AlphaComposite生成半透明效果(通常我会使用paintComponent方法,但您必须记住,这会使子组件不受影响)

import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JDesktopPane dp = new BackgroundDesktopPane();
BufferedImage img = ImageIO.read(new File("C:\\Users\\shane\\Dropbox\\Ponies\\SmallPony.png"));
TransparentInternalFrame iframe = new TransparentInternalFrame("Banana", true, true, true, true);
iframe.setLocation(10, 10);
iframe.add(new JLabel(new ImageIcon(img)));
iframe.pack();
iframe.setVisible(true);
dp.add(iframe);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(dp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public class TransparentInternalFrame extends JInternalFrame {
public TransparentInternalFrame() {
super();
}
public TransparentInternalFrame(String title) {
super(title);
}
public TransparentInternalFrame(String title, boolean resizable) {
super(title, resizable);
}
public TransparentInternalFrame(String title, boolean resizable, boolean closable) {
super(title, resizable, closable);
}
public TransparentInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable) {
super(title, resizable, closable, maximizable);
}
public TransparentInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable) {
super(title, resizable, closable, maximizable, iconifiable);
}
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.25f));
super.paint(g2d);
g2d.dispose();
}
}
public class BackgroundDesktopPane extends JDesktopPane {
private BufferedImage background;
public BackgroundDesktopPane() throws IOException {
background = ImageIO.read(new File("C:\\Users\\shane\\Dropbox\\Ponies\\800px-Rainbow_Dash_flying_past_1_S2E16.png"));
}
@Override
public Dimension getPreferredSize() {
return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g2d.drawImage(background, x, y, this);
g2d.dispose();
}
}
}
}第2部分点击...
提供通过组件单击的功能要困难得多,如果您添加支持鼠标事件的组件,就会变得更加困难。下面将允许鼠标事件“跌落”组件(除了框架板),但内部框架中响应鼠标事件的任何组件都将停止工作。

public class TransparentInternalFrame extends JInternalFrame {
public TransparentInternalFrame() {
super();
setOpaque(false);
init();
}
public TransparentInternalFrame(String title) {
super(title);
setOpaque(false);
init();
}
public TransparentInternalFrame(String title, boolean resizable) {
super(title, resizable);
setOpaque(false);
init();
}
public TransparentInternalFrame(String title, boolean resizable, boolean closable) {
super(title, resizable, closable);
setOpaque(false);
init();
}
public TransparentInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable) {
super(title, resizable, closable, maximizable);
setOpaque(false);
init();
}
public TransparentInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable) {
super(title, resizable, closable, maximizable, iconifiable);
setOpaque(false);
init();
}
protected void init() {
MouseAdapter proxy = new MouseAdapter() {
protected void dispatchEventToParent(MouseEvent e) {
Container parent = getParent();
if (parent != null) {
e = SwingUtilities.convertMouseEvent(e.getComponent(), e, parent);
parent.dispatchEvent(e);
}
}
@Override
public void mouseMoved(MouseEvent e) {
dispatchEventToParent(e);
}
@Override
public void mouseDragged(MouseEvent e) {
dispatchEventToParent(e);
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
dispatchEventToParent(e);
}
@Override
public void mouseExited(MouseEvent e) {
dispatchEventToParent(e);
}
@Override
public void mouseEntered(MouseEvent e) {
dispatchEventToParent(e);
}
@Override
public void mouseReleased(MouseEvent e) {
dispatchEventToParent(e);
}
@Override
public void mousePressed(MouseEvent e) {
dispatchEventToParent(e);
}
@Override
public void mouseClicked(MouseEvent e) {
dispatchEventToParent(e);
}
};
addMouseListener(proxy);
addMouseMotionListener(proxy);
addMouseWheelListener(proxy);
}
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.SrcOver.derive(0.25f));
super.paint(g2d);
g2d.dispose();
}
}
public class BackgroundDesktopPane extends JDesktopPane {
private BufferedImage background;
private List<Point> points;
public BackgroundDesktopPane() throws IOException {
points = new ArrayList<>(25);
background = ImageIO.read(new File("C:\\Users\\shane\\Dropbox\\Ponies\\800px-Rainbow_Dash_flying_past_1_S2E16.png"));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g2d.drawImage(background, x, y, this);
}
g2d.setColor(Color.RED);
for (Point p : points) {
g2d.fillOval(p.x - 5, p.y - 5, 10, 10);
}
g2d.dispose();
}
}https://stackoverflow.com/questions/34122512
复制相似问题