gpt4 book ai didi

Java UIManager - 更改组件样式

转载 作者:行者123 更新时间:2023-11-29 05:49:17 25 4
gpt4 key购买 nike

我想使用 UIManager 更改我的组件样式。

例如:

我单击一个按钮,按钮前景从黑色变为绿色。 JCheckbox 也是如此......

在我的示例中,更改仅适用于 Button.gradient....我没有获得 Button.foreground 的更新,也没有获得 JCheckbox 的更新!

这是我的 UIManagerClass:

  package components;

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.SwingUtilities;


public class OwnUiManager {

ButtonDemo Bd;

OwnUiManager(ButtonDemo aThis) {
Bd = aThis;
}

public void setNormal() {
ArrayList<Object> gradients = new ArrayList();
gradients.add(0.3);
gradients.add(0.0);
gradients.add(new Color(221, 232, 243));
gradients.add(new Color(255, 255, 255));
gradients.add(new Color(184, 207, 229));
javax.swing.UIManager.put("RadioButton.background", Color.PINK);
javax.swing.UIManager.put("Button.gradient", gradients);
javax.swing.UIManager.put("Button.foreground", Color.PINK);
SwingUtilities.updateComponentTreeUI(Bd);
}

public void setNightVision() {
System.out.println("Tes");
ArrayList<Object> gradients = new ArrayList();
gradients.add(0.18f);
gradients.add(0.17f);
gradients.add(Color.BLACK);
gradients.add(Color.DARK_GRAY);
gradients.add(Color.DARK_GRAY);
javax.swing.UIManager.put("RadioButton.background", Color.GRAY);
javax.swing.UIManager.put("Button.gradient", gradients);
javax.swing.UIManager.put("Button.foreground", Color.red);

SwingUtilities.updateComponentTreeUI(Bd);
}
}

这是我的 Buttondemo/Main-Class:

package components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class ButtonDemo extends JPanel
implements ActionListener {

protected JButton b1,b2;
private JRadioButton b3;

public ButtonDemo() {

b1 = new JButton("ON");
b1.addActionListener(this);
add(b1);
b2 = new JButton("OFF");
b2.addActionListener(this);
add(b2);
//For Testing the Style
b3=new JRadioButton("Test");
add(b3);
}

public void actionPerformed(ActionEvent e) {
OwnUiManager test = new OwnUiManager(this);
if (e.getSource().equals(b1)) {
test.setNormal();
} else {
test.setNightVision();
}
}

private static void createAndShowGUI() {
JFrame frame = new JFrame("ButtonDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonDemo newContentPane = new ButtonDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
}


public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

最佳答案

关于Java UIManager - 更改组件样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14583878/

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