【问题标题】:How to change the color of a JSeparator?如何更改 JSeparator 的颜色?
【发布时间】:2012-10-16 12:56:15
【问题描述】:

问题在标题中。

我目前正在做类似的事情:

jSperator = new JSeparator();
jSeparator1.setForeground(new java.awt.Color(255, 51, 51));

但是分隔符保持他的默认颜色,比如 212,212,212。

【问题讨论】:

    标签: java swing colors jseparator


    【解决方案1】:

    必须更改 ’Background’ 而不是 ’Foreground’

    Nimbus Look and Feel 的逻辑可能不同

    金属L&F

    import javax.swing.*;
    import java.awt.*;
    
    public class GridBagSeparator1 {
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("Laying Out Components in a Grid");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
            sep.setBackground(Color.black);
            JSeparator sep1 = new JSeparator(SwingConstants.HORIZONTAL);
            sep1.setBackground(Color.blue);
            JSeparator sep2 = new JSeparator(SwingConstants.HORIZONTAL);
            sep2.setBackground(Color.green);
            JSeparator sep3 = new JSeparator(SwingConstants.HORIZONTAL);
            sep3.setBackground(Color.red);
    
            frame.setLayout(new GridLayout(4, 0));
            frame.add(sep);
            frame.add(sep1);
            frame.add(sep2);
            frame.add(sep3);
            frame.pack();
            frame.setVisible(true);
        }
    }
    

    【讨论】:

    • 改变背景并不能解决。我正在使用 Netbeans GUI 构建器 Matiss 在 mac 上构建界面。也许这是对外观和感觉的限制。
    • 可能需要使用UIManager并更改Separator.foreground
    • 使用 Sythetica Look And Feel,我必须更改背景并将 opaque 属性设置为 true。
    【解决方案2】:

    JSeparator 有两种颜色,一种用于线条,一种用于阴影。 您可以更改两者,分别将颜色设置为背景和前景。

    JSeparator sep = new JSeparator();
    sep.setForeground(Color.green); // top line color
    sep.setBackground(Color.green.brighter()); // bottom line color
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 2011-03-23
      • 2011-01-02
      • 2012-06-01
      • 2021-10-23
      • 2018-10-01
      相关资源
      最近更新 更多