gpt4 book ai didi

java - Windows 10 中无法使用透明 JFrame 背景吗?

转载 作者:行者123 更新时间:2023-12-02 13:38:29 25 4
gpt4 key购买 nike

我在未修饰的 JFrame 中有一个 JPanel。我想在 JPanel 上绘制图像。当图像包含透明像素时,我希望它们是“透明的”,以便您可以看到窗口后面的任何内容。

我所有的研究都告诉我,我应该做

myJFrame.setUndecorated(true);
myJFrame.setBackground(new Color(0,0,0,0));
myJPanel.setOpaque(false);

,但是一旦我的 JFrame 背景颜色的 alpha 值不是 255,我的 JPanel 就不再被吸引。

最佳答案

因此,根据我有限的测试,它似乎在 Windows 10 上运行良好

Transparent Window

import java.awt.Color;
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 javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}

public class TestPane extends JPanel {

private BufferedImage img;

public TestPane() throws IOException {
img = ImageIO.read(...);
setOpaque(false);
setBorder(new LineBorder(Color.RED));
}

@Override
public Dimension getPreferredSize() {
return new Dimension(img.getWidth(), img.getHeight());
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - img.getWidth()) / 2;
int y = (getHeight() - img.getHeight()) / 2;
g2d.drawImage(img, x, y, this);
g2d.dispose();
}

}

}

为了确定,我使用 JLabel 进行了测试...

import java.awt.BorderLayout;
import java.awt.Color;
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 javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}

public class TestPane extends JPanel {

public TestPane() throws IOException {
BufferedImage img = ImageIO.read(...);
setOpaque(false);
setBorder(new LineBorder(Color.RED));
setLayout(new BorderLayout());
add(new JLabel(new ImageIcon(img)));
}

}

}

这表明问题出在您未向我们展示的代码中的某个位置。考虑提供 runnable example这说明了你的问题。这不是代码转储,而是您正在执行的操作的示例,它突出显示了您遇到的问题。这将减少困惑并获得更好的响应

关于java - Windows 10 中无法使用透明 JFrame 背景吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42867413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com