我尝试使用 Java Synth 和XML来设置 jTextPane 的样式。我可以将样式应用于 jPanes 和 jButtons ,但不能应用于jTextPanes。问题是我的自定义画家中的paint方法 paintTextPaneBackground (甚至paintTextPaneBorder)没有被调用。
XML代码:
<?xml version="1.0" encoding="UTF-8"?>
<synth>
<object id="painter" class="ui.themes.MyPainter" />
<!-- BASIC -->
<style id="basic">
<font name="Helvetica" size="18" />
<state>
<color value="#ff0000" type="BACKGROUND" />
<color value="#000000" type="FOREGROUND" />
<opaque value="true" />
<insets top="12" left="12" bottom="12" right="12" />
</state>
</style>
<bind style="basic" type="region" key=".*" />
<!-- PANE -->
<style id="panel">
<painter method="panelBackground" idref="painter" />
</style>
<bind style="panel" type="region" key="Panel" />
<!-- JBUTTON -->
<style id="button">
<insets top="12" left="12" bottom="12" right="12" />
<state>
<color value="#212121" type="FOREGROUND" />
<painter method="buttonBackground" idref="painter" />
</state>
</style>
<bind style="button" type="region" key="BUTTON" />
<!-- JTEXTPANE -->
<style id="textpane">
<insets top="12" left="12" bottom="12" right="12" />
<state>
<color value="#0000ff" type="BACKGROUND" />
<color value="#212121" type="FOREGROUND" />
<painter method="textPaneBackground" idref="painter" />
</state>
</style>
<bind style="textpane" type="region" key="TEXT_PANE" />
</synth>
我的自定义画家:
public class MyPainter extends SynthPainter {
@Override
public void paintButtonBackground(SynthContext context, Graphics g, int x, int y, int w, int h) {
System.out.println("This method is called. :)");
}
@Override
public void paintPanelBackground(SynthContext context, Graphics g, int x, int y, int w, int h) {
System.out.println("This method is called. :)");
}
public void paintTextPaneBackground(SynthContext context, Graphics g, int x, int y, int w, int h) {
System.out.println("This method is NOT CALLED. :(");
}
}
如何将样式应用于jTextPane?