如何使用Nimbus L& F更改JSeparator的颜色

时间:2016-08-19 19:24:33

标签: java swing nimbus jseparator

以下是我尝试根据How to change the color of a JSeparator?中的答案将Nimbus默认黑色的垂直JSeparator颜色更改为红色时所尝试的内容:

public class TestFrame extends JFrame {

    public static void main(String[] args) {

        TestFrame frame = new TestFrame();
        frame.setSize(200, 200);
        frame.setLayout(new GridBagLayout());

        for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                try {
                    UIManager.setLookAndFeel(info.getClassName());
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(TestFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
            }
        }
        UIManager.put("Separator.background", Color.red);
        UIManager.put("Separator.foreground", Color.red);

        JSeparator separator = new JSeparator(JSeparator.VERTICAL);
        separator.setPreferredSize(new Dimension(2, 100));
        separator.setForeground(Color.red);
        separator.setBackground(Color.red);

        frame.add(separator, new GridBagConstraints());
        frame.setVisible(true);

    }

}

然而垂直分离器仍然是黑色的。我该怎么办?

注意:我知道Nimbus是问题,因为我试过没有将L& F设置为Nimbus,这很好用。另请注意,设置Separator[Enabled].backgroundPainter属性似乎已影响JSeperator但不符合我的预期(仅更改背景颜色与分色线颜色)

2 个答案:

答案 0 :(得分:1)

我通过更改Nimbus用来获取其他颜色的nimbusBlueGrey颜色来解决这个问题。将分隔符设置为不透明只会帮助更改背景颜色,但JSeperator's有2种颜色,前景和背景,因此设置为不透明并更改背景颜色可以解决问题的一半。 nimbusBlueGrey似乎处理前景色,使用setForegroundcolor()Separator.foreground属性似乎无法覆盖。

问题是更改nimbusBlueGrey会影响许多其他组件的颜色。我不确定如何将颜色更改包含在JSeperator中。

答案 1 :(得分:0)

  /**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Windows look and feel instead of NIMBUS*/
    //<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()) {
      /*Change This Line To Make Your TextField Transparent */      if ("WINDOWS".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Furious.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Furious().setVisible(true);
        }
    });
}

从NIMBUS到WINDOWS改变你的外观,它对我来说很好。

以下是我的用户界面快照:

Here is Snapshot Of My UI