在让 Nimbus 和这些* Painters 一起交给我几个月后,我取得了胜利。
请注意,使用的密钥可以在操作系统之间改变(所以不能保证,但它确实适用于我的)。只要您有一个有效的边界密钥,JTextArea
您就可以将其转移到您的JTextPane
.
import java.awt.Insets;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
public class NimbusBorderPainting extends Box{
public NimbusBorderPainting(){
super(BoxLayout.Y_AXIS);
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (UnsupportedLookAndFeelException e2) {
e2.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Retrieve the TextArea painter from the defaults
Object o = UIManager.get("TextArea[Enabled+NotInScrollPane].borderPainter");
//Transfer the Painter to a TextPane key
UIDefaults paneDefaults = new UIDefaults();
paneDefaults.put("TextPane.borderPainter",o);
JTextPane pane = new JTextPane();
pane.setMargin(new Insets(10, 10, 10, 10));
//Apply the new UI to your text pane
pane.putClientProperty("Nimbus.Overrides",paneDefaults);
pane.putClientProperty("Nimbus.Overrides.InheritDefaults",false);
pane.setText("Lots of Text\nWell, as much as I'm willing to type\n");
add(pane);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new NimbusBorderPainting());
frame.validate();
frame.pack();
frame.setVisible(true);
}
}