我有以下课程:
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();
}
}
}
我想使用addPortal
方法将多个JPanel添加到垂直框中,该框包含在JScrollPane中。 JPanels使用BorderLayout
并在中心显示JEditorPane
,显示可变长度的HTML。
问题是当我将垂直框放在JScrollpane中时,JEditorPanes的换行停止工作。如果我添加没有Jscrollpane的垂直框,换行工作,但是,如你所料,没有滚动。如何进行换行+滚动?
为了使事情更清楚,请参阅下面的屏幕截图:
答案 0 :(得分:2)
JEditorPane
应始终将自身调整为显示内部文本所需的确切大小。这就是它与JScrollPane
一起工作的原因:内部组件增长,滚动窗格随后会询问大小并相应地配置滚动条。
您没有向我们展示如何创建您尝试添加的JPanel
门户网站。我的猜测是这段代码有问题。也许BorderLayout
会让事情变得混乱。
我建议您将JPanel
更改为Component
,并将JEditorPane
添加到JScrollPane
内的面板中,而不添加任何布局或包装组件。这应该将正确的尺寸暴露给BoxLayout
,从而正确地调整视口大小。
答案 1 :(得分:1)
找到了一个有效的解决方案:http://i.stack.imgur.com/63PHx.jpg
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();
JScrollPane sp = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
sp.setAutoscrolls(false);
sp.setViewportView(right);
j.getContentPane().add(sp, 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());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
sp.getViewport().setViewPosition(new java.awt.Point(0, 0));
}
});
// 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 implements Scrollable
{
Box vertical_box = null;
public WorkBench()
{
setLayout(new BorderLayout());
this.vertical_box = Box.createVerticalBox();
//** Scenario A - no vertical scrolling **
add(this.vertical_box , BorderLayout.CENTER);
//** Scenario B - no line wrapping **
//JScrollPane sp = new JScrollPane(current );
//sp.setViewportView(current );
//add(sp, BorderLayout.CENTER);
}
public void addPortal(JPanel portal)
{
this.vertical_box.add(portal);
validate();
}
@Override
public Dimension getPreferredScrollableViewportSize()
{
return getPreferredSize();
}
@Override
public int getScrollableUnitIncrement(
Rectangle visibleRect,
int orientation,
int direction)
{
return 20;
}
@Override
public int getScrollableBlockIncrement(
Rectangle visibleRect,
int orientation,
int direction)
{
return 60;
}
@Override
public boolean getScrollableTracksViewportWidth()
{
return true;
}
@Override
public boolean getScrollableTracksViewportHeight()
{
if (getParent() instanceof JViewport)
{
return (((JViewport)getParent()).getHeight() > getPreferredSize().height);
}
return false;
}
}
}