2

我一直在尝试更改 UIManager 以使这些看起来很愚蠢的绿色方块消失。我如何改变用户的外观和感觉?它依赖于系统吗?编译我的代码的其他人有一个恒定的梯度。理想情况下,它只是一个实心正方形,而不是较小的块。

谢谢

在此处输入图像描述

4

4 回答 4

4

您是否尝试将其设置为系统(用户)的外观和感觉?

设置外观的最简单方法是在调用后启动 GUI:

try
{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e)
{
    // ...
}

话虽如此,上面似乎是 Windows XP 主题,并且可能确实是系统(用户)主题。除非有很好的理由(例如,客户/用户要求),否则我通常会远离 GUI 中的自定义主题。

也就是说,上面的代码使它依赖于系统,这很好,因为它符合用户的期望。

于 2011-05-04T19:38:28.280 回答
2

“这些看起来很笨的绿色方块”是 Windows XP 外观和感觉的元素。如果您希望它们看起来不同,您可以更改此特定组件的外观。

只需使用以下解决方法:

try {
    javax.swing.UIManager.setLookAndFeel(/* Look and Feel for your JProgressBar*/);
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}

MyProgressBar = new javax.swing.JProgressBar();

try {
    javax.swing.UIManager.setLookAndFeel(/* Previous, main Look and Feel */);
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}

我正在使用这段代码给 JFileChooser 另一个外观,它运行良好。

您只需在创建组件之前更改外观和感觉,并在创建之后恢复之前的外观。

于 2013-03-16T09:33:51.150 回答
0

编辑 下面的代码更改整个 JFrame 的 L&F。

static Main m;//Reference to JFrame to be updated
static String maxOSLookAndFeel = "ch.randelshofer.quaqua.QuaquaLookAndFeel";//Package of particular L&F    
private void MacOSLFjMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MacOSLFjMenuItemActionPerformed
                    // TODO add your handling code here:
                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                            try {
                                UIManager.setLookAndFeel(maxOSLookAndFeel);
                                SwingUtilities.updateComponentTreeUI(m);
                                m.validate();
                            } catch (ClassNotFoundException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (InstantiationException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (IllegalAccessException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            } catch (UnsupportedLookAndFeelException ex) {
                                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    });

                }//GEN-LAST:event_MacOSLFjMenuItemActionPerformed

Secundo: 在我看来(根据经验搜索了很多),当您附加一个新的 L&F 时,不可能只影响一个 JComponent。您可以在整个 JFrame 中更改 L&F,或者您可以编写自己的 Swing 组件。

实现目标的另一种方法是研究 java源代码并找到将 JProgressBar 图像添加到组件的位置,并通过扩展 JProgressBar 覆盖此方法。

于 2011-05-04T20:07:08.447 回答
0

可以在 Sun Java 文档中找到对 Java 外观的全面描述,这里是: PDF 中的Java,外观设计指南

于 2011-08-15T14:50:15.203 回答