javaSwing---jTextPane图片换行

一.写一个FontAttrib类

 //jTextPane换行,字体属性
    private class FontAttrib {
      public static final int GENERAL = 0; // 常规
      public static final int BOLD = 1; // 粗体
      public static final int ITALIC = 2; // 斜体
      public static final int BOLD_ITALIC = 3; // 粗斜体
      private SimpleAttributeSet attrSet = null; // 属性集
      private String text = null, name = null; // 要输入的文本和字体名称
      private int style = 0, size = 0; // 样式和字号
      private Color color = null, backColor = null; // 文字颜色和背景颜色


      public FontAttrib() {
      }

      public SimpleAttributeSet getAttrSet() {
          attrSet = new SimpleAttributeSet();
          if (name != null) {
           StyleConstants.setFontFamily(attrSet, name);
          }
          if (style == FontAttrib.GENERAL) {
           StyleConstants.setBold(attrSet, false);
           StyleConstants.setItalic(attrSet, false);
          } else if (style == FontAttrib.BOLD) {
           StyleConstants.setBold(attrSet, true);
           StyleConstants.setItalic(attrSet, false);
          } else if (style == FontAttrib.ITALIC) {
           StyleConstants.setBold(attrSet, false);
           StyleConstants.setItalic(attrSet, true);
          } else if (style == FontAttrib.BOLD_ITALIC) {
           StyleConstants.setBold(attrSet, true);
           StyleConstants.setItalic(attrSet, true);
          }
          StyleConstants.setFontSize(attrSet, size);
          if (color != null) {
           StyleConstants.setForeground(attrSet, color);
          }
          if (backColor != null) {
           StyleConstants.setBackground(attrSet, backColor);
          }
          return attrSet;
      }
      public void setAttrSet(SimpleAttributeSet attrSet) {
       this.attrSet = attrSet;
      }
      public String getText() {
       return text;
      }
      public void setText(String text) {
       this.text = text;
      }
      public Color getColor() {
       return color;
      }
      public void setColor(Color color) {
       this.color = color;
      }
      public Color getBackColor() {
       return backColor;
      }
      public void setBackColor(Color backColor) {
       this.backColor = backColor;
      }
      public String getName() {
       return name;
      }
      public void setName(String name) {
       this.name = name;
      }

      public int getSize() {
       return size;
      }

      public void setSize(int size) {
       this.size = size;
      }
      public int getStyle() {
       return style;
      }
      public void setStyle(int style) {
       this.style = style;
      }
   }

二.写一个换行函数

//Box1jTextPane换行
    private void Box1jTextPaneNewline(FontAttrib attrib) { 
        try {
            doc = Box1jTextPane.getStyledDocument();
            doc.insertString(doc.getLength(), attrib.getText() + "\n", 
                    attrib.getAttrSet());
        } catch (BadLocationException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

三.测试函数

 //测试插入图片
    private void testInsertPicture(){
        try {
            //插入一张图片
            Box1jTextPane.insertIcon(new ImageIcon("src\\resource\\Frame.png"));           
            //换行
            Box1jTextPaneNewline(new FontAttrib());  
            //删除第一个图片
            Box1jTextPane.getDocument().remove(0,1); 
        } catch (BadLocationException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值