问题
I can't seem to find out how I can add a JScrollPane to a JLabel. The JLabel that I'm using is populated with a long formatted HTML string. Please help.
area = new JLabel();
JScrollPane scroller = new JScrollPane(area,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
回答1:
Really not good idea to hold or display long Html formatted text in the JLabel, since is possible, better would be use JEditorPanes / JTextPanes, these JComponets support styled and html formatted text, Icons etc ... , examples for JTextPane and JEditorPane
回答2:
Can you provide us your code? Are you setting the viewport view to the JLabel? Instantiate your JLabel and a JScrollPane. then set the JScrollPane viewport to the JLabel (setViewPortView(jlabel);) then add the JScrollPane to whatever component you want the scrolling JLabel to be on.
Hope this helps!
回答3:
You can't add a JScrollPane to a JLabel , what you can do is to create a JScrollPane and add a JLabel.
See this: http://www.cs.cf.ac.uk/Dave/HCI/HCI_Handout_CALLER/node63.html
回答4:
You need to set the JScrollPane's viewport view:
scroller.setViewPortView(area);
stick that line just before you go panel.add(scroller);
Let us know if that helps or not.
来源:https://stackoverflow.com/questions/9335138/add-a-jscrollpane-to-a-jlabel