滚动浏览 JScrollPane 中保存的 JLabel
我有一个 JScrollPane,它使用以下代码保存 JLabel:
//Create TEXT LOG JPanel textLogPane = new JScrollPane(logLabel); textLogPane.setPreferredSize(textLogPaneDim); //textLogPane.setOpaque(true); textLogPane.setBorder(BorderFactory.createLineBorder(Color.BLACK)); textLogPane.getViewport().setBackground(Color.DARK_GRAY);
JLabel logLabel 由使用
进行回车的 HTML 编码的字符串表示。我根据某些行的内容显示某些图像,并且我希望能够滚动 JScrollPane、textLogPane,以便在显示该图形时显示该行。我知道我想要显示的行的内容,但我似乎无法弄清楚如何让它向下(或向上)滚动到相关行。
如果需要,我可以更改为 JLabel 以外的其他内容,只要我可以保留 HTML 编码并使其看起来像多行文本。
抱歉,如果这是重复的,我尝试搜索但找不到任何结果。
谢谢
I have a JScrollPane that holds a JLabel using the following code:
//Create TEXT LOG JPanel
textLogPane = new JScrollPane(logLabel);
textLogPane.setPreferredSize(textLogPaneDim);
//textLogPane.setOpaque(true);
textLogPane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
textLogPane.getViewport().setBackground(Color.DARK_GRAY);
The JLabel, logLabel, is represented by a string with an HTML encoding using
for carriage returns. I display certain images based on the contents of certain lines and I would like to be able to scroll the JScrollPane, textLogPane, to show that line whenever I am displaying that graphic. I know the contents of the line that I want to display but I can't seem to figure out how to get it to scroll down(or up) to the relevant line.
If need be I can change to something other than a JLabel as long as I can keep the HTML encoding and have it look just like multiple lines of text.
Sorry if this is a repeat I tried searching but couldn't find any results.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以进行一些自定义数学运算并使用 scrollRectToVisible() 。我不知道如何计算 JLabel 中特定行的矩形。更好的解决方案是将字符串粘贴到 JList 中,也许使用 html 的自定义渲染器,然后使用
You can do some custom maths and use scrollRectToVisible() in your viewport. I don't know how to compute the rect of a specific line in your JLabel. A better solution would be to stick your strings into a JList instead, perhaps with a custom renderer for the html, and use
HTML 中不使用“回车符”,而是使用“br”标签。
我建议您应该使用 JTextPane 来显示多行文本。我还发现不使用 HTML,而是添加带有属性的字符串会更容易。您还可以将图标插入到 JTextPane 中。
阅读 Swing 教程中有关使用文本组件 一个工作示例。
You don't use "carriage returns" in HTML, you use "br" tags.
I would suggest you should probably be using a JTextPane for multiline text. I also find it easier to not use HTML, but instead to add strings with attributes. You can also insert icons into a JTextPane.
Read the section from the Swing tutorial on Using Text Components for a working example.