围绕JTextPane与图像 [英] Surrounding JTextPane with images

查看:82
本文介绍了围绕JTextPane与图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a project on my own and wanted to create an JComponent or JFrame that looks like the the screenshot provided in the link(since it says here I don't have enough reputation to post images). The JTextPane is surrounded by three pictures and it does wrap words to next line.

所以请帮助我。如果你用一个例子证明你的答案,我将不胜感激。
以下是图片的链接。

So please help me.I would appreciate if you demonstrated your answer with an example. Here are the links to the images.

布局



屏幕截图

Layout

Screen Shot

推荐答案

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

A simple solution might be to create a JLabel and set its icon property to the background image...

Icon icon = ...;
JLabel background = new JLabel(icon);

将标签的布局管理器设置为类似 GridBagLayout ...

Set the label's layout manager to something like GridBagLayout...

background.setLayout(new GridBagLayout());

设置 GridBagConstraints insets以便文本窗格将在包含内... ...

Set the GridBagConstraints insets so that the text pane will be offset within the contain...

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

然后只需将 JTextPane 添加到label ...

And then simply add the JTextPane to the label...

JTextPane textPane = ...;
background.add(textPane, gbc);

然后你可以将 JLabel 添加到您想要的容器,甚至根据您的需要将其设置为 JFrame 的内容窗格。

You can then either add the JLabel to the what ever container you want or even set it as the JFrame's content pane depending on your needs.

ps-你需要让文本窗格透明...

ps- You'll need to make the text pane transparent...

例如......

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();
                }
            }
        });
    }

}

这篇关于围绕JTextPane与图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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