0

这是我尝试JSeparator根据如何更改 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但不是我想要的方式(只是改变了背景颜色与分隔线颜色)

4

2 回答 2

1

我通过更改nimbusBlueGreyNimbus 用于派生其他颜色的颜色来解决此问题。将分隔符设置为不透明只会帮助更改背景颜色,但JSeperator's有两种颜色,一个前景和一个背景,因此设置为不透明并更改背景颜色解决了一半的问题。nimbusBlueGrey似乎处理前景色,这似乎不能被setForegroundcolor()Separator.foreground属性覆盖。

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

于 2016-08-19T20:57:41.130 回答
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,对我来说效果很好。

这是我的用户界面的快照:

这是我的用户界面的快照

于 2017-11-16T13:39:44.937 回答