我在互联网上看到的唯一解决方法是在实例化组件之前更改 LookAndFeel,然后将其更改回来,这看起来是一个糟糕的解决方案
这是一个非常非常非常 x 10 倍的糟糕解决方案。
我是material-ui-swing的作者,使用材质风格,你需要使用不同风格的概念,这是我在开发库期间的主要关注点,也是因为同时当我们将该库集成到一个名为JMars的著名摇摆应用程序中时,我们需要尊重 UX 团队提供的设计系统。
举个例子,material-ui-swing 给出了两种 API:
- 一个是 Material Theme System,以声明方式定义应用程序周围的主题,以及
- 二是赋予底层API实现不同风格的UI组件。
在您的情况下,我们需要 material-ui-swing 的第二个力量,它是较低级别的 API,我将在以下链接在线存储库中添加一个示例,完整的文档可在此处获得
一个可能的定制示例如下
public class ContainedButtonUI extends MaterialButtonUI {
//The propriety order inside the method installUI is important
//because some propriety should be override
@Override
public void installUI(JComponent c) {
super.mouseHoverEnabled = false;
super.installUI(c);
super.mouseHoverEnabled = true;
super.colorMouseHoverNormalButton = MaterialColors.PURPLE_500;
super.background = MaterialColors.PURPLE_700;
c.setBackground(super.background);
if(super.mouseHoverEnabled){
c.addMouseListener(
MaterialUIMovement.getMovement(c, this.colorMouseHoverNormalButton)
);
}
//If you want use this style also for Default button
// super.defaultBackground = MaterialColors.PURPLE_700;
//super.colorMouseHoverDefaultButton = MaterialColors.PURPLE_500;
super.borderEnabled = false;
}
之后,为了保持所有应用程序架构干净,您可以添加以下 JButton 特化
/** @author https://github.com/vincenzopalazzo */
public class ContainedButton extends JButton {
public ContainedButton() {}
public ContainedButton(Icon icon) {
super(icon);
}
public ContainedButton(String text) {
super(text);
}
public ContainedButton(Action a) {
super(a);
}
public ContainedButton(String text, Icon icon) {
super(text, icon);
}
@Override
protected void init(String text, Icon icon) {
super.init(text, icon);
// When you don't want anymore you just delete the
// following line
setUI(new ContainedButtonUI());
}
诅咒,也许图书馆不能帮助你所有的组件样式,但没有人说图书馆不能在社区的帮助下发展。
可以在此处找到组件的不完整描述