gpt4 book ai didi

java - 启用nimbus时无法在JDK7中透明和未修饰的JFrame

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:40:20 24 4
gpt4 key购买 nike

看看这张图: Transparent JFrame

这是透明框架的代码:

GraphicsEnvironment ge = 
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();

if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
System.err.println(
"Translucency is not supported");
System.exit(0);
}

JFrame.setDefaultLookAndFeelDecorated(true);

这很好用,但是当尝试通过添加

启用 LookAndFeel 时
    try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}catch(.......)

它给了我这个错误

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated

这是什么错误?以及如何解决?

感谢您的回答和建议。

编辑

提问/交叉发布

最佳答案

Change the laf in the main method before ui is created by @Sri Harsha Chilakapati

and @Sri Harsha Chilakapati I am sorry but I didn't get you I'll be appreciated if you describe more by @Azad Omer

  • Oracle 教程中的更多信息 Modifying the Look and Feel ,

  • 问题是由代码行 JFrame.setDefaultLookAndFeelDecorated(true); 引起的,需要禁用/注释此代码行 //JFrame.setDefau.../p>

  • 默认情况下,使用 Nimbus L&F 创建半透明 JFrame 没有问题

enter image description here

来自代码

import java.awt.*;
import javax.swing.*;

public class TranslucentWindow extends JFrame {

private static final long serialVersionUID = 1L;

public TranslucentWindow() {
super("Test translucent window");
setLayout(new FlowLayout());
add(new JButton("test"));
add(new JCheckBox("test"));
add(new JRadioButton("test"));
add(new JProgressBar(0, 100));
JPanel panel = new JPanel() {

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
private static final long serialVersionUID = 1L;

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(0, 0, getWidth(), getHeight());
}
};
panel.add(new JLabel("Very long textxxxxxxxxxxxxxxxxxxxxx "));
add(panel);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
//JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
Window w = new TranslucentWindow();
w.setVisible(true);
com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.7f);
}
});
}
}

关于java - 启用nimbus时无法在JDK7中透明和未修饰的JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16219111/

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