JScrollPane后台的JLabel没有变化

JLabel in JScrollPane background does not change

我想在 JScrollPaneRowHeaderView 中更改我的 JLabel 的背景。

JLabel jl = new JLabel("asdfasdfasdf");
jl.setForeground(Color.RED);
JScrollPane scroll = new JScrollPane();

// I Have tried
jl.setBackground(new Color(0, 0, 0, 255));
scroll.setBackground(new Color(0, 0, 0, 255));
scroll.setOpaque(true);
scroll.setVisible(true);

scroll.setRowHeaderView(jl);

然而,文本是红色的,但背景仍然是白色的。

编辑

这是 How do I set a JLabel's background color? 的重复问题,但我无法将此问题设置为重复问题,因为我需要 250 个代表。

JLabel 的默认 Opaque 为 false。这意味着不可见。 使用 jl.setOpaque(真);

祝你好运。