在JScrollPane中的JEditorPane中包装HTML文本 [英] Wrapping HTML text in a JEditorPane inside a JScrollPane

查看:106
本文介绍了在JScrollPane中的JEditorPane中包装HTML文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个应用程序中,我使用不可编辑的JEditorPanes作为一种通用的UI小部件,它可以显示一些复杂的内容(HTML可以做到这一点),包装文本行并捕获鼠标点击。不知道JEditorPane是否是一个很好的选择,所以请随时建议其他选择。



以下示例代码运行得很好:

  import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class Main {

private static JPanel createPanel(){
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());

for(int i = 0; i <3; i ++){
JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setContentType(text / html);

String text =
这是< b> item#+ i +< / b> ;. +
它上面有文字,应该被打包。
;

editorPane.setText(text);

GridBagConstraints约束= new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = i;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weightx = 1.0;
constraints.insets.bottom = 5;

panel.add(editorPane,constraints);
}

返回面板;
}


public static void main(String [] args){
JPanel panel = createPanel();

JFrame frame = new JFrame();
frame.setSize(200,200);
frame.setLocation(200,200);

//更改此以在示例之间切换
布尔useScrollPane = false;

if(useScrollPane){
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizo​​ntalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_​​NEVER);
scrollPane.setViewportView(panel);
frame.add(scrollPane);
}
else {
frame.add(panel);
}

frame.setVisible(true);
}

}

并产生以下内容: p>



然而,我可能有大量的这些,并可以使用垂直滚动条。

所以我把整个东西放在一个JScrollPane中(改变 useScrollPane 变量到 true 在示例代码中查看此版本)。

如果我缩小窗口高度,这会给我一个垂直滚动条,但问题是现在文本不再被包裹: b
$ b



所以问题是:我如何获得文本包装和垂直滚动条?



正如你所看到的,我禁用了水平滚动条,但没有多大帮助。



PS。我对Swing没有太多的经验,所以如果你在这段代码中看到一些初学者的WTF,请指出:)

解决方案

  // JPanel panel = new JPanel(); 
ScrollablePanel面板=新的ScrollablePanel();
panel.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.FIT);

请参阅 Scrollable Panel ,以及类的工作原理。


In an application, I'm using uneditable JEditorPanes as a sort of a generic UI widget that can display somewhat complex content (HTML will do the trick), wrap text lines and catch mouse clicks. Not sure if JEditorPane is a good choice for this, so feel free to suggest alternatives.

The following sample code works fairly well:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class Main {

  private static JPanel createPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    for (int i = 0; i < 3; i++) {
      JEditorPane editorPane = new JEditorPane();
      editorPane.setEditable(false);
      editorPane.setContentType("text/html");

      String text =
        "This is <b>item #" + i + "</b>." +
        " It's got text on it that should be wrapped."
      ;

      editorPane.setText(text);

      GridBagConstraints constraints = new GridBagConstraints();
      constraints.gridx = 0;
      constraints.gridy = i;
      constraints.fill = GridBagConstraints.HORIZONTAL;
      constraints.weightx = 1.0;
      constraints.insets.bottom = 5;

      panel.add(editorPane, constraints);
    }

    return panel;
  }


  public static void main(String[] args) {
    JPanel panel = createPanel();

    JFrame frame = new JFrame();
    frame.setSize(200, 200);
    frame.setLocation(200, 200);

    // Change this to switch between examples
    boolean useScrollPane = false;

    if (useScrollPane) {
      JScrollPane scrollPane = new JScrollPane();
      scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
      scrollPane.setViewportView(panel);
      frame.add(scrollPane);
    }
    else {
      frame.add(panel);
    }

    frame.setVisible(true);
  }

}

and produces the following:

However, I may have a large number of these and could use a vertical scrollbar.

So I put the whole thing in a JScrollPane (change the useScrollPane variable to true in the sample code to see this version).

This gives me a vertical scrollbar if I shrink the window height, but the problem is that now the text is no longer wrapped:

So the question is: how can I get both the text wrapping and the vertical scrollbar?

As you can see, I disabled the horizontal scrollbar, but it didn't help much.

PS. I don't have much experience with Swing, so if you see some beginner WTFs in this code, please point them out :)

解决方案

// JPanel panel = new JPanel();
ScrollablePanel panel = new ScrollablePanel();
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );

See Scrollable Panel for the class and an explanation on how the class works.

这篇关于在JScrollPane中的JEditorPane中包装HTML文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆