我正在使用JTextPane,并希望仅在一部分内容中显示两列中的文本。我不能使用HTML,因为我必须在UI上显示一个图标(我从外部API获取javax.swing.Icon的实例,然后使用StyleConstants.setIcon添加它。)
我需要一个像this这样的布局,但使用的是DefaultStyledDocument。这可以实现吗?如果是这样我该怎么办?
以下是我的代码。我想和Paragraph2和Paragraph3并排。
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
public class Test
{
public static void main(String[] args)
{
try
{
JFrame f = new JFrame("Test");
f.getContentPane().setLayout(new GridBagLayout());
JTextPane pane = new JTextPane();
URL url = new URL(
"http://files.softicons.com/download/toolbar-icons/fatcow-hosting-icons-by-fatcow/png/32/status_online.png");
BufferedImage img = ImageIO.read(url);
ImageIcon icon = new ImageIcon(img);
Document document = pane.getDocument();
Style style = ((DefaultStyledDocument) document).addStyle("StyleName", null);
StyleConstants.setIcon(style, icon);
document.insertString(document.getLength(), "ignored text", style);
document.insertString(document.getLength(),
"\n\nParagraph 1. Lorem Ipsum is simply dummy text of the printing and typesetting industry. "
+ "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,"
+ " when an unknown printer took a galley of type and scrambled it to make a type specimen book."
+ " It has survived not only five centuries, but also the leap into electronic typesetting",
new SimpleAttributeSet());
document.insertString(document.getLength(),
"\n\nParagraph 2. Lorem Ipsum is simply dummy text of the printing and typesetting industry. "
+ "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,"
+ " when an unknown printer took a galley of type and scrambled it to make a type specimen book."
+ " It has survived not only five centuries, but also the leap into electronic typesetting",
new SimpleAttributeSet());
document.insertString(document.getLength(),
"\n\nParagraph 3. Lorem Ipsum is simply dummy text of the printing and typesetting industry. "
+ "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,"
+ " when an unknown printer took a galley of type and scrambled it to make a type specimen book."
+ " It has survived not only five centuries, but also the leap into electronic typesetting",
new SimpleAttributeSet());
JScrollPane jScrollPane = new JScrollPane(pane);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
f.getContentPane().add(
jScrollPane,
new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0,
0, 0, 0), 0, 0));
f.setSize(500, 400);
f.setVisible(true);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}