gpt4 book ai didi

java - 无法覆盖 Nimbus 属性

转载 作者:太空宇宙 更新时间:2023-11-04 07:39:22 26 4
gpt4 key购买 nike

我有例如

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("text",Color.GREEN);`

文本仍然是黑色,但为什么?.

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


public class test999 extends JFrame {
private JLabel jLabel1 = new JLabel();

public test999(String title) {
super(title);
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
UIDefaults defaults = new UIDefaults();
defaults.put("text",new Color(255,0,0));
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 300;
int frameHeight = 300;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);

jLabel1.setBounds(72, 72, 147, 57);
jLabel1.setText("text");
cp.add(jLabel1);

setVisible(true);
}
public static void main(String[] args) {
new test999("test999");
}
}

最佳答案

这不是 UI 默认值的工作方式:"text" 不是有效名称,并且没有组件可以看到您的 defaults 实例。相反,尝试

jLabel1.setForeground(Color.red);

另外,不要使用setBounds();使用合适的layout manager .

附录:如图here , "text" 是有效的主色键,而不是组件键。

I…want to…override the nimbus default.

在大多数 L&F 上,您可以指定 "Label.foreground" 键:

UIManager.put("Label.foreground", Color.red);

在 Nimbus 上你必须做 this :

UIManager.put("text", Color.red);
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

关于java - 无法覆盖 Nimbus 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16297734/

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