我有一个带有JFrame
窗口的大型java程序,我需要在其中创建透明背景,但这只有在没有滚动窗格的情况下才能实现(参见下面的测试程序和图片):
//TransparentWindow.java
import java.awt.*;
import javax.swing.*;
public class TransparentWindow extends JFrame
{
JMenuBar menuBar;
TransparentCanvas canvas;
JComponent pane;
JScrollPane scrollPane;
public TransparentWindow()
{
setBackground(new Color(0,0,0,0));
setSize(new Dimension(500,500));
setLocationRelativeTo(null); //set location at the center
setTitle("Transparency");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu help = new JMenu("Help");
menuBar.add(help);
canvas = new TransparentCanvas();
pane = (JComponent)this.getContentPane();
pane.add(canvas);
//scrollPane = new JScrollPane(canvas);
//pane.add(scrollPane, BorderLayout.CENTER);
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
TransparentWindow transparentWindow = new TransparentWindow();
transparentWindow.setVisible(true);
}
class TransparentCanvas extends JComponent
{
public TransparentCanvas()
{
super();
setPreferredSize(new Dimension(500,500));
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2D = (Graphics2D)g;
g2D.setColor(new Color(240, 240, 240, 128));
g2D.fillRect(0, 0, getWidth(), getHeight());
g2D.setColor(Color.blue);
g2D.fillOval(200, 150, 100, 100);
g2D.dispose();
}
}
}
透明窗口
使用JScrollPane
(通过取消注释上面构造函数末尾的两行),您将得到不透明的颜色(参见下图):
不透明窗口
调用setUndecorated(true)
也无法正常工作。
(顺便说一句,我需要使用Java 7,因为其他一些应用程序。)
请帮忙。提前感谢您的时间!
答案 0 :(得分:3)
大多数Swing组件都是不透明的,但JScrollPane
有点不同,它实际上是一个复合组件,由JScrollPane
和JViewPort
(和滚动条)组成,用于显示viewComponent
的一部分。
要使其按照您希望的方式运作,您需要使JScrollPane
和JViewPort
透明,例如
scrollPane = new JScrollPane(canvas);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
pane.add(scrollPane, BorderLayout.CENTER);
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 javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TransparentWindow extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
TransparentWindow transparentWindow = new TransparentWindow();
transparentWindow.setVisible(true);
transparentWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
transparentWindow.setVisible(true);
}
});
}
JMenuBar menuBar;
TransparentCanvas canvas;
JComponent pane;
JScrollPane scrollPane;
public TransparentWindow() {
setUndecorated(true);
setBackground(new Color(0, 0, 0, 0));
setSize(new Dimension(500, 500));
setLocationRelativeTo(null); //set location at the center
setTitle("Transparency");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu help = new JMenu("Help");
menuBar.add(help);
canvas = new TransparentCanvas();
pane = (JComponent) this.getContentPane();
pane.add(canvas);
scrollPane = new JScrollPane(canvas);
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false);
pane.add(scrollPane, BorderLayout.CENTER);
}
class TransparentCanvas extends JComponent {
public TransparentCanvas() {
super();
setPreferredSize(new Dimension(500, 500));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(new Color(240, 240, 240, 128));
g2D.fillRect(0, 0, getWidth(), getHeight());
g2D.setColor(Color.blue);
g2D.fillOval(200, 150, 100, 100);
g2D.dispose();
}
}
}
答案 1 :(得分:0)