Java Swing HTML呈现显示了非常大的要点
我在swing应用程序中使用JEditorPane呈现一些HTML。我使用了一个项目符号列表,Java Swing HTML呈现显示了非常大的要点,java,html,swing,rendering,jeditorpane,Java,Html,Swing,Rendering,Jeditorpane,我在swing应用程序中使用JEditorPane呈现一些HTML。我使用了一个项目符号列表,..,并且在输出中获得了过大的项目符号。相同的HTML块将在真实浏览器中显示正常大小的项目符号 我在Windows7/JDK7和iirc上以及Ubuntu和OpenJDK1.7.0_09上观察到了这一点 这是已知的吗?有办法绕过它吗 工作示例: import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.Wi
- ..
,并且在输出中获得了过大的项目符号。相同的HTML块将在真实浏览器中显示正常大小的项目符号
我在Windows7/JDK7和iirc上以及Ubuntu和OpenJDK1.7.0_09上观察到了这一点
这是已知的吗?有办法绕过它吗
工作示例:
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
/**
*
*/
public class HTMLTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// create new frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setSize(500, 500);
// create editor pane and fill with some html
JEditorPane pane = new JEditorPane();
pane.setContentType("text/html");
pane.setEditable(false);
pane.setText("<html><h1>Heading</h1>Text<ul><li>Bullet point</li></ul></html>");
// add editor pane to frame and set frame visible
frame.add(pane);
frame.setVisible(true);
}
}
在css文件和
<li>• Item 1</li>
&bull;项目1
在html中有一个像样的要点。受aterai启发的解决方案。请参阅ListView源代码
public void paint(Graphics g, Shape allocation) {
super.paint(g, allocation);
Rectangle alloc = allocation.getBounds();
Rectangle clip = g.getClipBounds();
// Since listPainter paints in the insets we have to check for the
// case where the child is not painted because the paint region is
// to the left of the child. This assumes the ListPainter paints in
// the left margin.
if ((clip.x + clip.width) < (alloc.x + getLeftInset())) {
Rectangle childRect = alloc;
alloc = getInsideAllocation(allocation);
int n = getViewCount();
int endY = clip.y + clip.height;
for (int i = 0; i < n; i++) {
childRect.setBounds(alloc);
childAllocation(i, childRect);
if (childRect.y < endY) {
if ((childRect.y + childRect.height) >= clip.y) {
listPainter.paint(g, childRect.x, childRect.y,
childRect.width, childRect.height,
this, i);
}
}
else {
break;
}
}
}
}
drawShape如下所示,您可以看到尺寸硬编码为8
void drawShape(Graphics g, CSS.Value type, int ax, int ay, int aw,
int ah, float align) {
// Align to bottom of shape.
int gap = isLeftToRight ? - (bulletgap + 8) : (aw + bulletgap);
int x = ax + gap;
int y = Math.max(ay, ay + (int)(align * ah) - 8);
if (type == CSS.Value.SQUARE) {
g.drawRect(x, y, 8, 8);
} else if (type == CSS.Value.CIRCLE) {
g.drawOval(x, y, 8, 8);
} else {
g.fillOval(x, y, 8, 8);
}
}
您可以尝试用自己提供的方法替换样式表的ListPainter
public ListPainter getListPainter(AttributeSet a) {
return new ListPainter(a, this);
}
在这里,您可以更改项目符号的渲染,下面是另一个示例(使用css列表样式图像属性): bullet.png:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class HTMLTest2 {
public JComponent makeEditorPane(String bullet) {
// create editor pane and fill with some html
JEditorPane pane = new JEditorPane();
pane.setContentType("text/html");
pane.setEditable(false);
if(bullet!=null) {
HTMLEditorKit htmlEditorKit = (HTMLEditorKit)pane.getEditorKit();
StyleSheet styleSheet = htmlEditorKit.getStyleSheet();
//String u = getClass().getResource(bullet).toString();
String u = "http://i.stack.imgur.com/jV29K.png";
styleSheet.addRule(String.format("ul{list-style-image:url(%s);margin:0px 20px;", u));
//styleSheet.addRule("ul{list-style-type:circle;margin:0px 20px;}");
//styleSheet.addRule("ul{list-style-type:disc;margin:0px 20px;}");
//styleSheet.addRule("ul{list-style-type:decimal;margin:0px 20px;}");
}
pane.setText("<html><h1>Heading</h1>Text<ul><li>Bullet point</li></ul></html>");
return pane;
}
public JComponent makeUI() {
JPanel p = new JPanel(new GridLayout(2,1));
p.add(new JScrollPane(makeEditorPane(null)));
p.add(new JScrollPane(makeEditorPane("bullet.png")));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new HTMLTest2().makeUI());
f.setSize(320, 320);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.text.*;
导入javax.swing.text.html.*;
公共类HTMLTest2{
公共JComponent makeEditorPane(字符串项目符号){
//创建编辑器窗格并填充一些html
JEditorPane=新的JEditorPane();
pane.setContentType(“text/html”);
pane.setEditable(false);
如果(项目符号!=null){
HTMLEditorKit HTMLEditorKit=(HTMLEditorKit)窗格。getEditorKit();
StyleSheet StyleSheet=htmlEditorKit.getStyleSheet();
//字符串u=getClass().getResource(bullet.toString();
字符串u=”http://i.stack.imgur.com/jV29K.png";
addRule(String.format(“ul{list-style-image:url(%s);边距:0px 20px;”,u));
//addRule(“ul{list样式类型:圆圈;边距:0px 20px;}”);
//addRule(“ul{list样式类型:disc;边距:0px 20px;}”);
//addRule(“ul{list样式类型:decimal;边距:0px 20px;}”);
}
窗格.setText(“HeadingText- 项目符号
”;
返回窗格;
}
公共JComponent makeUI(){
JPanel p=新JPanel(新网格布局(2,1));
p、 添加(新的JScrollPane(makeEditorPane(null));
p、 添加(新的JScrollPane(makeEditorPane(“bullet.png”));
返回p;
}
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@重写公共无效运行(){
createAndShowGUI();
}
});
}
公共静态void createAndShowGUI(){
JFrame f=新的JFrame();
f、 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f、 getContentPane().add(新的HTMLTest2().makeUI());
f、 设置大小(320320);
f、 setLocationRelativeTo(空);
f、 setVisible(真);
}
}
感谢您的出色搜索。这实际上让所有列表形状在swing的html呈现中都很难看。我认为在浏览器中,项目符号的大小可能取决于实际的字体大小。您可以覆盖ListView的“public void paint”(图形g,形状分配)“方法并在那里提供自己的绘画,而不是替换“ListPainter”,但为什么默认的答案如此难看?我将此答案标记为解决方案,因为它似乎比StanislavL的另一个答案更容易实现,尽管两者都很好。@aterai五年过去了,有没有更简单的解决方案,Oracle解决了这一问题。@Paul Taylor CSS在Swing组件中的支持自5年前以来一直没有改变。这与类似文本缩进:-20px;padding left:20px
的功能相结合,可以将文本缩进到项目符号的右侧。不过,我还没有发现如何自动将缩进与项目符号大小对齐。
public ListPainter getListPainter(AttributeSet a) {
return new ListPainter(a, this);
}
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class HTMLTest2 {
public JComponent makeEditorPane(String bullet) {
// create editor pane and fill with some html
JEditorPane pane = new JEditorPane();
pane.setContentType("text/html");
pane.setEditable(false);
if(bullet!=null) {
HTMLEditorKit htmlEditorKit = (HTMLEditorKit)pane.getEditorKit();
StyleSheet styleSheet = htmlEditorKit.getStyleSheet();
//String u = getClass().getResource(bullet).toString();
String u = "http://i.stack.imgur.com/jV29K.png";
styleSheet.addRule(String.format("ul{list-style-image:url(%s);margin:0px 20px;", u));
//styleSheet.addRule("ul{list-style-type:circle;margin:0px 20px;}");
//styleSheet.addRule("ul{list-style-type:disc;margin:0px 20px;}");
//styleSheet.addRule("ul{list-style-type:decimal;margin:0px 20px;}");
}
pane.setText("<html><h1>Heading</h1>Text<ul><li>Bullet point</li></ul></html>");
return pane;
}
public JComponent makeUI() {
JPanel p = new JPanel(new GridLayout(2,1));
p.add(new JScrollPane(makeEditorPane(null)));
p.add(new JScrollPane(makeEditorPane("bullet.png")));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new HTMLTest2().makeUI());
f.setSize(320, 320);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}