java - 如何设置JButton的背景?

标签 java swing netbeans jbutton look-and-feel

我尝试使用 NetBeans 中的属性设置 JButton 的颜色,但没有成功。当我选择一种颜色时,只有边框颜色发生变化:

enter image description here

我一直在阅读有关此内容的内容,我认为这与我的外观和感觉有关。我在 Netbans 中将项目创建为“Java 桌面应用程序”,并且我认为我正在使用 Nimbus:

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Entrenamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Entrenamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Entrenamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Entrenamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the dialog */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            Entrenamiento dialog = new Entrenamiento(new javax.swing.JFrame(), true);
            dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                @Override
                public void windowClosing(java.awt.event.WindowEvent e) {
                    System.exit(0);
                }
            });
            dialog.setVisible(true);
        }
    });
}

我尝试以这种方式设置 JButton 的背景: UIManager.put("Button.background", Color.WHITE);

但是它不起作用。我怎样才能实现这个目标?谢谢。

最佳答案

定制的 Java L&F 是当前社会(如 JFormDesigner)或开源程序员(如我)面临的问题。

java l&f java 定制可以通过三种方法实现,例如:

  • 使用 UIManager
  • 使用个人组件 UI
  • 使用手动配置。

UIManager配置

它更复杂而不简单,因为不同的 l&f 在 map 内使用不同的属性。 另外,您可以在数字字符串中犯错误,例如“button.background”而不是“Button.background”。

使用自定义组件 UI

您可以使用个人组件UI,例如this answer内部但这对自定义更改有意义,因为如果将 l&f 更改为运行时,您就会丢失此个人 l&f。

例如:您想要一个没有鼠标悬停效果或没有边框的特定按钮

创建样式的手动配置。

您可以使用手动配置,例如the answer ,这种方法不好,因为您必须使用类似的方法或组件来组织代码

public void configureStyle(String theme){
  //some code
}

为什么要采用配置样式的方法?因为如果您在项目中实现了在没有这些方法或组件的情况下更改 l&f 的可能性,您就会丢失您的个人设置。

即:您必须有一种重置所有个人属性(property)的方法。

结论

我想在我的答案中添加一个结论,它是:如果你正在开发一个新项目,我认为你可以使用新的 l&f 到你的项目的基础上,如果你在 Github 上搜索它,你可以找到很多新的l&f。 但我想在我的回答中谈谈我的一个项目,名为 MaterialTheming,并在 Material-UI-Swing 内实现。

Material 主题

MaterialTheming 是material-ui-swing l&f 的第二层,它包括实现一个具有 l&f 所有配置的组件,该组件配置器称为主题

因此,目前 MaterialTheming 处于 alpha 版本并且仅支持程序设置。 此外,通过 MaterialTheming,您可以开发您的个人主题,并且可以完全控制 L&f 的风格。

我有开发人员需要使用相同的界面和java内部的方法来实现精简版和深色主题,我在客户端应用程序中编写了很多代码(这是一个糟糕的选择,因为应用程序的GUI)。

个人主题的拳头(部分示例)是this称为MaterialDarkPinkTheme

因此,要为您的应用程序实现一个主题来更改默认颜色按钮,代码如下

import mdlaf.themes.MaterialLiteTheme;
import mdlaf.utils.MaterialColors;

/**
 * @author https://github.com/vincenzopalazzo
 */
public class PersonalMaterialTheme extends MaterialLiteTheme {

    @Override
    protected void installColor() {
        super.installColor();
        super.buttonBackgroundColor = MaterialColors.AMBER_800;
        super.buttonBackgroundColorMouseHover = MaterialColors.AMBER_500;
        super.buttonDefaultBackgroundColor = MaterialColors.COSMO_ORANGE;
    }
}

效果是这样的

enter image description here

关于java - 如何设置JButton的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60153972/

相关文章:

java - 如何通过 Maven 使用 Netbeans 调试 Spring Boot

java - SpringBoot 无法 Autowiring 。 Mapstruct 映射器未找到 'UserMapper' 类型的 beans 错误

java - 无法调用可执行 Jar 中的 .exe 文件。如何?

java - 从 javax.json.JsonObject 中移除键值对

java - 如何使用java.applet.AudioClip?

java - 如何在 Java SWING 中设置自定义字体的大小和其他属性(粗体、斜体等)

java - 指定顶级容器的 ContentPane 有优势吗?

java - 迭代 3d 数组?

java - JTabbedPane 自动调整大小

java - 关闭后重置 JDialog