Java 如何使JFrame透明/不透明,而不必设置为未装饰(true) 是否有一种方法可以将JFrame设置为透明,同时仍不影响按钮/文本 如果不是,如何在不使用.setUndercorated(true)的情况下使JFrame透明 这是一个完全不同的问题,但我如何添加渐变作为背景色,而不是将其设置为一种纯色 单击以查看程序运行时JFrame的外观
以下是透明JFrame的源代码:Java 如何使JFrame透明/不透明,而不必设置为未装饰(true) 是否有一种方法可以将JFrame设置为透明,同时仍不影响按钮/文本 如果不是,如何在不使用.setUndercorated(true)的情况下使JFrame透明 这是一个完全不同的问题,但我如何添加渐变作为背景色,而不是将其设置为一种纯色 单击以查看程序运行时JFrame的外观,java,swing,jframe,gradient,transparent,Java,Swing,Jframe,Gradient,Transparent,以下是透明JFrame的源代码: public class Example extends JFrame { public Example() { super("TranslucentWindowDemo"); setLayout(new GridBagLayout()); setSize(500,300); setLocationRelativeTo(null); setUndecorated(true);
public class Example extends JFrame {
public Example() {
super("TranslucentWindowDemo");
setLayout(new GridBagLayout());
setSize(500,300);
setLocationRelativeTo(null);
setUndecorated(true);
getContentPane().setBackground(Color.blue);
JButton Close = new JButton("Close");
add(Close);
ActionListener al;
al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
};
Close.addActionListener (al);
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
System.err.println("Translucency is not supported");
System.exit(0);
}
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Example e = new Example();
e.setOpacity(0.55f);
e.setVisible(true);
}
});
}
}
对于不透明度,您可以尝试
Pane.setOpaque(false)代码>
对于内容,您应该更改为setBackground(新颜色(0,0,0,0))代码>
您可以阅读更多内容1:是的,制作一个透明窗口,然后在其上添加一个透明JPanel,覆盖它的paintComponent并用透明颜色填充它;2:没有;3:见答案一:
public class Example extends JFrame {
public Example() {
super("TranslucentWindowDemo");
setLayout(new GridBagLayout());
setSize(500,300);
setLocationRelativeTo(null);
setUndecorated(true);
getContentPane().setBackground(Color.blue);
JButton Close = new JButton("Close");
add(Close);
ActionListener al;
al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
};
Close.addActionListener (al);
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}
public static void main(String args[]) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
System.err.println("Translucency is not supported");
System.exit(0);
}
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Example e = new Example();
e.setOpacity(0.55f);
e.setVisible(true);
}
});
}
}