要透明窗口,您需要在框架背景色的alpha設置爲0
。這可能是我在一段時間內看到的最直觀的對話,就好像你對其他Swing組件做了這樣的事情,你會完全搞砸油漆過程。
你不想改變窗口的透明度,因爲它的有生力量整個窗口和它同樣的內容。
例如...
JWindow frame = new JWindow();
frame.setBackground(new Color(0, 0, 0, 0));
您不必使用JWindow
,但這意味着我不需要去除裝飾它自己...
您還需要使確保您添加到窗口任何內容即是透明的(不透明= FALSE),所以它不會「隱藏」什麼是它的下面...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class LeafWindow {
public static void main(String[] args) {
new LeafWindow();
}
public LeafWindow() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JWindow frame = new JWindow();
frame.setBackground(new Color(0, 0, 0, 0));
frame.setContentPane(new LeafPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setAlwaysOnTop(true);
}
});
}
public class LeafPane extends JPanel {
private BufferedImage leaf;
public LeafPane() {
setBorder(new CompoundBorder(
new LineBorder(Color.RED),
new EmptyBorder(0, 0, 250, 0)));
try {
leaf = ImageIO.read(getClass().getResource("/Leaf.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
setOpaque(false);
setLayout(new GridBagLayout());
JButton button = new JButton("Close");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
add(button);
}
@Override
public Dimension getPreferredSize() {
return leaf == null ? new Dimension(200, 200) : new Dimension(leaf.getWidth(), leaf.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (leaf != null) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(leaf, 0, 0, this);
g2d.dispose();
}
}
}
}
本示例故意爲內容添加一個線邊框,因爲您可以看到原始窗口邊界是什麼。它也使用EmptyBorder
強制JButton
到圖形上,但這僅僅是一個例子...
說實話,你會得到一個更好的結果,你使用透明的框架並將圖像繪製到背景中,您將獲得抗鋸齒而不是銳降像素。有了一點聰明的工作,你也可以生成一個剪切矩形,如果你需要它 – MadProgrammer 2014-10-05 20:25:37
@MadProgrammer當我搜索,我發現你回答的答案數量。你可以解釋更多如何做到這一點。我不需要代碼,但程序。我不會說流利的英語。我試圖通過將框架不透明度設置爲0.但所有框架隱藏。我試圖設置不透明的錯誤。它不工作 – 2014-10-05 23:56:02
所以@MadProgrammer我記得我在c#中使用透明key.but但我仍然沒有找到一種方法來使窗口透明和圖像完全可見 – 2014-10-06 00:06:30