Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

java - Surrounding JTextPane with images

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
Layout
Screen Shot enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

background.setLayout(new GridBagLayout());

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;

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

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

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- You'll need to make the text pane transparent...

For example...

Background pane

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...