问题在标题中。
我目前正在做类似的事情:
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);
}
}
关于java - 如何更改 JSeparator 的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13083876/