Swing 的 BasicHTML.createHTMLView 指标使用情况
我正在做一些图形处理,而 HTML 是设置显示内容样式的完美选择。 我正在尝试重用 swing 的内置 html 支持,如果我对传递给 View.paint 的高度进行硬编码,它会完美地工作,但我不知道如何确定运行时渲染内容的边界有多高给定特定宽度。
Graphics2D g = ...
JLabel label = new JLabel("blah blah blah...");
View view = BasicHTML.createView(label, label.getText());
int minHeight = .... // Calculation magic goes here
Rectangle htmlSize = new Rectangle(0, 0, 50, minHeight);
g.setClip(htmlSize);
view.paint(g, htmlSize);
如果我用 getPreferredSize() 询问 JLabel 方向,它根本不考虑换行。 如果我尝试使用 JEditorPane,它会返回一个更大但大小固定的矩形。
谢谢。
I'm doing some graphics processing and HTML is a perfect choice for styling the displayed content. I'm trying to reuse swing's built in html support, and it works perfectly if I hard code the height passed to View.paint, but I can't figure out how to determine how tall the bounds of the rendered content would be at runtime given a specific width.
Graphics2D g = ...
JLabel label = new JLabel("blah blah blah...");
View view = BasicHTML.createView(label, label.getText());
int minHeight = .... // Calculation magic goes here
Rectangle htmlSize = new Rectangle(0, 0, 50, minHeight);
g.setClip(htmlSize);
view.paint(g, htmlSize);
If I ask the JLabel direction with getPreferredSize() it doesn't consider wrapping at all. If I try using a JEditorPane it returns a larger, but fixed size rectangle.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这方面取得了成功:
I have had success with this:
在知道宽度之前无法计算高度。 在处理 Swing 组件时,我认为您需要执行以下操作:
或者也许您可以使用此 发布:
The height can't be calculated until the width is known. When dealing with Swing components I think you need to do something like:
Or maybe you can use the concepts present in this posting: