如何更改JSeparator的颜色?

8
问题在标题中。
我目前正在做类似于以下内容:
jSperator = new JSeparator();
jSeparator1.setForeground(new java.awt.Color(255, 51, 51));

但是分隔符保持默认颜色,类似于212,212,212。

2个回答

13

需要改变'Foreground'而不是'Background'

Nimbus Look and Feel的逻辑可能不同

Metal 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);
    }
}

2
更改背景并不能解决问题。我正在使用Matiss,Netbeans GUI构建器在Mac上构建界面。也许这是外观和感觉的限制。 - nathan
4
可能需要使用 UIManager 并更改 Separator.foreground - trashgod
5
使用Sythetica外观,我需要更改背景并将opaque属性设置为true。 - Béatrice Cassistat

7

JSeparator有两种颜色,一种是线的颜色,另一种是阴影的颜色。您可以分别将颜色设置为背景和前景来更改两种颜色。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接