簡體   English   中英

如何更改JSeparator的顏色?

[英]How to change the color of a JSeparator?

問題出在標題中。

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

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

但分隔符保持默認顏色,如212,212,212。

必須改變'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);
    }
}

JSeparator有2種顏色,一種用於線條,一種用於陰影。 您可以分別更改兩者,將顏色設置為背景和前景。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM