栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用图像包围JTextPane

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用图像包围JTextPane

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

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

将标签的布局管理器设置为

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…

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438381.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号