我有下面的類:搖擺 - JEditorPane中內部JScrollPane中 - 線不被「包裝」
import java.awt.*;
import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
public class test
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// now add it all to a frame
JFrame j = new JFrame("Test");
WorkBench right = new WorkBench();
j.getContentPane().add(right, BorderLayout.CENTER);
right.addPortal(getPortal());
right.addPortal(getPortal());
right.addPortal(getPortal());
right.addPortal(getPortal());
right.addPortal(getPortal());
right.addPortal(getPortal());
right.addPortal(getPortal());
right.addPortal(getPortal());
right.addPortal(getPortal());
// center the jframe, then make it visible
j.setSize(800, 600);
j.setLocationRelativeTo(null);
j.setVisible(true);
}
});
}
public static JPanel getPortal()
{
JEditorPane swingbox = new JEditorPane();
swingbox.setEditorKit(new HTMLEditorKit());
swingbox.setOpaque(true);
swingbox.setContentType("text/html");
String htmlString = "<html>\n"
+ "<body>\n"
+ "<h1>Welcomfve! sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h1>\n"
+ "<h2>This is an H2 header. sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h2>\n"
+ "<p>This is some sample text sdsdsdsdsd sdsdsd sdsd sdsd sdsd </p>\n"
+ "<p><a href=\"http://devdaily.com/blog/\">devdaily blog</a></p>\n"
+ "</body>\n";
swingbox.setText(htmlString);
swingbox.setBackground(Color.WHITE);
JPanel p = new JPanel(new BorderLayout());
p.setBackground(Color.WHITE);
JLabel title = new JLabel("Heading ");
title.setBackground(Color.WHITE);
p.add(title, BorderLayout.NORTH);
p.add(swingbox, BorderLayout.CENTER);
return p;
}
public static class WorkBench extends JPanel
{
private Box current = null;
public WorkBench()
{
setLayout(new BorderLayout());
current = Box.createVerticalBox();
//** Scenario A - no vertical scrolling **
add(current, BorderLayout.CENTER);
//** Scenario B - no line wrapping **
//JScrollPane sp = new JScrollPane(current);
//sp.setViewportView(current);
//add(sp, BorderLayout.CENTER);
}
public void addPortal(JPanel portal)
{
current.add(portal);
validate();
}
}
}
我要添加多個JPanels,使用addPortal
方法,到垂直框,它被包圍在JScrollPane中。 JPanels使用BorderLayout
並在中心顯示一個JEditorPane
,它顯示可變長度的HTML。
問題是,當我把垂直框放在JScrollpane內,然後JEditorPanes的換行停止工作。如果我添加沒有Jscrollpane的垂直框,則換行可行,但是,如您所期望的那樣,不滾動。我如何獲得線包裝+滾動?
爲了讓事情更清晰,見下圖:
- 在A,我已經直接添加垂直框JPanel的工作臺。 請注意,包線工作,但沒有垂直滾動 。
- 在B中,我已經添加了一個JScrollPane中的垂直框,其中我添加到了JPanel WorkBench中,內容爲 。請注意垂直滾動條 存在,但換行不起作用。
http://i.stack.imgur.com/9K49t.png
正從您可運行實例的任何機會呢? – 2014-11-04 08:57:37
http://java-sl.com/tip_text_height_measuring.html使用該方法來確定您的內容html的高度並定義寬度 – StanislavL 2014-11-04 09:18:16
謝謝大家。我剛剛搬到一個新的住所,很抱歉拖延。我會盡快回復! – SoupMonster 2014-11-07 08:24:49