是否可以在 Java Swing 中更改段落的背景颜色?我尝试使用 setParagraphAttributes 方法(下面的代码)设置它,但似乎不起作用。
StyledDocument doc = textPanel.getStyledDocument();
Style style = textPanel.addStyle("Hightlight background", null);
StyleConstants.setBackground(style, Color.red);
Style logicalStyle = textPanel.getLogicalStyle();
doc.setParagraphAttributes(textPanel.getSelectionStart(), 1, textPanel.getStyle("Hightlight background"), true);
textPanel.setLogicalStyle(logicalStyle);