Java 用图像包围JTextPane

Java 用图像包围JTextPane,java,swing,layout,Java,Swing,Layout,我自己在做一个项目,想创建一个JComponent或JFrame,它看起来像链接中提供的屏幕截图(因为这里说我没有足够的声誉来发布图片)。JTextPane由三张图片包围,它将单词包装到下一行 所以请帮助我。如果你能举例说明你的答案,我将不胜感激。 以下是图片的链接 布局 截屏 一个简单的解决方案可能是创建JLabel,并将其图标属性设置为背景图像 Icon icon = ...; JLabel background = new JLabel(icon); 将标签的布局管理器设置为类似于Gr

我自己在做一个项目,想创建一个JComponent或JFrame,它看起来像链接中提供的屏幕截图(因为这里说我没有足够的声誉来发布图片)。JTextPane由三张图片包围,它将单词包装到下一行

所以请帮助我。如果你能举例说明你的答案,我将不胜感激。 以下是图片的链接

布局

截屏
一个简单的解决方案可能是创建
JLabel
,并将其
图标
属性设置为背景图像

Icon icon = ...;
JLabel background = new JLabel(icon);
将标签的布局管理器设置为类似于
GridBagLayout

background.setLayout(new GridBagLayout());
设置
GridBagConstraints
insets,使文本窗格在包含中偏移

GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(40, 40, 40, 40);
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
然后简单地将
JTextPane
添加到标签中

JTextPane textPane = ...;
background.add(textPane, gbc);
然后,您可以将
JLabel
添加到所需的容器中,甚至根据需要将其设置为
JFrame
的内容窗格

ps-您需要使文本窗格透明

例如

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class TextPaneWrapped {

    public static void main(String[] args) {
        new TextPaneWrapped();
    }

    public TextPaneWrapped() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                try {
                    JLabel background = new JLabel(
                            new ImageIcon(
                                    ImageIO.read(
                                            new File("background.jpg"))));
                    background.setLayout(new GridBagLayout());

                    JTextPane textPane = new JTextPane();
                    textPane.setOpaque(false);

                    Style centerStyle = textPane.addStyle("center", null);
                    StyleConstants.setAlignment(centerStyle, StyleConstants.ALIGN_CENTER);
                    StyleConstants.setFontFamily(centerStyle, textPane.getFont().getFamily());
                    textPane.setParagraphAttributes(centerStyle, true);

                    Style defaultStyle = textPane.addStyle("defaultStyle", centerStyle);
                    StyleConstants.setFontSize(defaultStyle, 24);

                    Style capWord = textPane.addStyle("capWord", defaultStyle);
                    StyleConstants.setForeground(capWord, Color.RED);
                    StyleConstants.setFontSize(capWord, 48);

                    StyledDocument doc = textPane.getStyledDocument();
                    try {
                        doc.insertString(0, "H", capWord);
                        doc.insertString(1, "ello ", defaultStyle);
                        doc.insertString(doc.getLength(), "W", capWord);
                        doc.insertString(doc.getLength(), "orld", defaultStyle);
                    } catch (BadLocationException exp) {
                        exp.printStackTrace();
                    }

                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.insets = new Insets(40, 40, 40, 40);
                    gbc.fill = GridBagConstraints.BOTH;
                    gbc.weightx = 1;
                    gbc.weighty = 1;
                    background.add(textPane, gbc);

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(background);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

}


我希望使用的GridBagLayout put不能正确显示图片。@user3380414您可能遇到的问题是使图像相互对齐。如果
JTextPane
不在
JScrollPane
中,这将是一个问题,因为它希望在您更改内容时不断更改大小…@user3380414您还必须记住,字体的大小(以像素为单位)在不同的平台/渲染管道之间会有所不同,这使得依靠布局管理器精确地排列单独的图像变得非常困难。请记住,你永远不要试图实现像素完美的布局,它只会保持你在后面…你的答案是非常棒的,我喜欢你的风格的文本窗格。但还有一件事,我希望JTextPane在空间不合适时水平继续,而不是将单词包装到下一行。在本例中,我如何实现这一点?谢谢