精华内容
下载资源
问答
  • JTextPane设置颜色出现的问题
    2021-05-12 19:31:56

    我做的是一个阅读器

    黑色的背景,白色的字。关键字用红色标识

    出现的问题是:

    第一次textPane加载的文字长度800,而且标识红色的位置在760

    刷新文字后,textPane加载的文字长度300,这时就出现问题了,不仅关键字是红色,所有字符都是红色了

    解决方法:在修改文字颜色前,先加以下代码

    		//先还原颜色
    		//这个bug出现在文字切换时,如果上一次标识的位置大于这一次文本的长度,颜色上就出现问题
    		MutableAttributeSet white = new SimpleAttributeSet();
    		StyleConstants.setForeground(white,Color.white);
    		doc.setCharacterAttributes(0,10000, white, true);

     

    更多相关内容
  • JTextPane 关键字变色

    2018-01-08 22:34:37
    SWT JTextPane 可以把控件中需要的字符串的颜色修改成自己想要的颜色
  • Swing对JTextPane字体颜色设置

    千次阅读 2013-11-11 15:20:12
    7、文本字体颜色设置 MutableAttributeSet attr = new SimpleAttributeSet();  StyleConstants.setForeground(attr, fg);  setCharacterAttributes(editor, attr, false); fg:为color 8、文本背景颜色设置 ...
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JEditorPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.UIManager;
    import javax.swing.WindowConstants;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.DefaultStyledDocument;
    import javax.swing.text.Document;
    import javax.swing.text.EditorKit;
    import javax.swing.text.MutableAttributeSet;
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledDocument;
    import javax.swing.text.StyledEditorKit;

    public class NewJFrame extends javax.swing.JFrame implements ActionListener {
     private JPanel jp1;

     private JButton color;

     private JTextPane jep;

     private JScrollPane jsp;

     private JButton font;

     /**
      * Auto-generated main method to display this JFrame
      */
     public static void main(String[] args) {
      NewJFrame inst = new NewJFrame();
      inst.setVisible(true);
     }

     public NewJFrame() {
      super();
      initGUI();
     }

     private void initGUI() {
      try {
       BorderLayout thisLayout = new BorderLayout();
       getContentPane().setLayout(thisLayout);
       setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
       {
        jp1 = new JPanel();
        getContentPane().add(jp1, BorderLayout.NORTH);
        {
         font = new JButton();
         font.addActionListener(this);
         jp1.add(font);
         font.setText("font");
        }
        {
         color = new JButton();
         jp1.add(color);
         color.addActionListener(this);
         color.setText("color");
        }
       }
       {
        jsp = new JScrollPane();
        getContentPane().add(jsp, BorderLayout.CENTER);
        {
         jep = new JTextPane();
         jsp.setViewportView(jep);
         jep.setDocument(new DefaultStyledDocument());
        }
       }
       pack();
       setSize(400, 300);
      } catch (Exception e) {
       e.printStackTrace();
      }
     }

     public static void setFontSize(JEditorPane editor, int size) {
      if (editor != null) {
       if ((size > 0) && (size < 512)) {
        MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setFontSize(attr, size);
        setCharacterAttributes(editor, attr, false);
       } else {
        UIManager.getLookAndFeel().provideErrorFeedback(editor);
       }
      }
     }

     public static void setForeground(JEditorPane editor, Color fg) {
      if (editor != null) {
       if (fg != null) {
        MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setForeground(attr, fg);
        setCharacterAttributes(editor, attr, false);
       } else {
        UIManager.getLookAndFeel().provideErrorFeedback(editor);
       }
      }
     }

     public static final void setCharacterAttributes(JEditorPane editor,
       AttributeSet attr, boolean replace) {
      int p0 = editor.getSelectionStart();
      int p1 = editor.getSelectionEnd();
      if (p0 != p1) {
       StyledDocument doc = getStyledDocument(editor);
       doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
      }
      StyledEditorKit k = getStyledEditorKit(editor);
      MutableAttributeSet inputAttributes = k.getInputAttributes();
      if (replace) {
       inputAttributes.removeAttributes(inputAttributes);
      }
      inputAttributes.addAttributes(attr);
     }

     protected static final StyledDocument getStyledDocument(JEditorPane e) {
      Document d = e.getDocument();
      if (d instanceof StyledDocument) {
       return (StyledDocument) d;
      }
      throw new IllegalArgumentException("document must be StyledDocument");
     }

     protected static final StyledEditorKit getStyledEditorKit(JEditorPane e) {
      EditorKit k = e.getEditorKit();
      if (k instanceof StyledEditorKit) {
       return (StyledEditorKit) k;
      }
      throw new IllegalArgumentException("EditorKit must be StyledEditorKit");
     }

     public void actionPerformed(ActionEvent e) {
      Object obj = e.getSource();
      if (obj == font) {
       JEditorPane editor = jep;
       setFontSize(editor, 20);
      }
      if (obj == color) {
       JEditorPane editor = jep;
       setForeground(editor, Color.red);
      }
     }

    }
    其他操作如下:
    1、对字体的操作
    MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setFontFamily(attr, family);
        setCharacterAttributes(editor, attr, false);
    family为字体
    2、对字体大小的操作
    MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setFontSize(attr, size);
        setCharacterAttributes(editor, attr, false);
    size为字号
    3、是否是粗体的操作
    StyledEditorKit kit = getStyledEditorKit(editor);
       MutableAttributeSet attr = kit.getInputAttributes();
       boolean bold = (StyleConstants.isBold(attr)) ? false : true;
       SimpleAttributeSet sas = new SimpleAttributeSet();
       StyleConstants.setBold(sas, bold);
       setCharacterAttributes(editor, sas, false);
    4、是否是斜体的操作
    StyledEditorKit kit = getStyledEditorKit(editor);
       MutableAttributeSet attr = kit.getInputAttributes();
       boolean italic = (StyleConstants.isItalic(attr)) ? false : true;
       SimpleAttributeSet sas = new SimpleAttributeSet();
       StyleConstants.setItalic(sas, italic);
       setCharacterAttributes(editor, sas, false);
    5、是否有下划线的操作
    StyledEditorKit kit = getStyledEditorKit(editor);
       MutableAttributeSet attr = kit.getInputAttributes();
       boolean underline = (StyleConstants.isUnderline(attr)) ? false
         : true;
       SimpleAttributeSet sas = new SimpleAttributeSet();
       StyleConstants.setUnderline(sas, underline);
       setCharacterAttributes(editor, sas, false);
    6、左中右对齐的处理
    MutableAttributeSet attr = new SimpleAttributeSet();
       StyleConstants.setAlignment(attr, a);
       setParagraphAttributes(editor, attr, false);
    public static final void setParagraphAttributes(JEditorPane editor,
       AttributeSet attr, boolean replace) {
      int p0 = editor.getSelectionStart();
      int p1 = editor.getSelectionEnd();
      StyledDocument doc = getStyledDocument(editor);
      doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
     }
    a:0:左,1:中,2:右

    7、文本字体颜色的设置
    MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setForeground(attr, fg);
        setCharacterAttributes(editor, attr, false);
    fg:为color
    8、文本背景颜色的设置
    MutableAttributeSet attr = new SimpleAttributeSet();
        StyleConstants.setBackground(attr, bg);
        setCharacterAttributes(editor, attr, false);
    展开全文
  • 二、JTextpane设定特定行的字体颜色 下面的代码在一个JTextPane中显示了一个图标,三行文字,每行用不同的颜色和大小显示: import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax....

    一、java获取文本文件的行数

    public int getRows(File file) {

    LineNumberReader lnr = null;

    int num = 0;

    try {

    lnr = new LineNumberReader(new FileReader(file));

    lnr.skip(Long.MAX_VALUE);

    num = lnr.getLineNumber();

    } catch (FileNotFoundException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    } finally {

    try {

    lnr.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    return ++num;

    }

    skip(Long.MAX_VALUE)     Parameters:n - The number of characters to skip

    java.io.LineNumberReader类是java.io.BufferedReader类的扩展,它封装了处理行号的额外能力,例如下例中得到不以“#”开头的行号:

    public void parseFile(String filePath) throws IOException {

    LineNumberReader in = new LineNumberReader(new FileReader(filePath));

    String line = "";

    while ((line = in.readLine()) != null) {

    if (line.startsWith("#")) {

    continue;

    } else {

    throw new IOException("第" + in.getLineNumber() + "行丢了#号");

    }

    }

    in.close();

    }

    行号0的意味着我们还没开始读入。如果在一个程序中我们每次读入一行,并输出该行和它的行号,行号将从1开始。

    二、JTextpane设定特定行的字体、颜色

    下面的代码在一个JTextPane中显示了一个图标,三行文字,每行用不同的颜色和大小显示:

    import javax.swing.*;

    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.text.*;

    import java.io.*;

    public class Test {

    JFrame frame;

    JTextPane textPane;

    File file;

    Icon image;

    public Test(){

    frame = new JFrame( "JTextPane ");

    textPane = new JTextPane();

    file = new File( "./classes/test/icon.gif ");

    image = new ImageIcon(file.getAbsoluteFile().toString());

    }

    public void insert(String str, AttributeSet attrSet) {

    Document doc = textPane.getDocument();

    str = "\n " + str ;

    try {

    doc.insertString(doc.getLength(), str, attrSet);

    }

    catch (BadLocationException e) {

    System.out.println( "BadLocationException: " + e);

    }

    }

    public void setDocs(String str,Color col,boolean bold,int fontSize) {

    SimpleAttributeSet attrSet = new SimpleAttributeSet();

    StyleConstants.setForeground(attrSet, col);

    //颜色

    if(bold==true){

    StyleConstants.setBold(attrSet, true);

    }//字体类型

    StyleConstants.setFontSize(attrSet, fontSize);

    //字体大小

    insert(str, attrSet);

    }

    public void gui() {

    textPane.insertIcon(image);

    setDocs( "第一行的文字 ",Color.red,false,20);

    setDocs( "第二行的文字 ",Color.BLACK,true,25);

    setDocs( "第三行的文字 ",Color.BLUE,false,20);

    frame.getContentPane().add(textPane, BorderLayout.CENTER);

    frame.addWindowListener(new WindowAdapter() {

    public void windowClosing(WindowEvent e) {

    System.exit(0);

    }});

    frame.setSize(200,300);

    frame.setVisible(true);

    }

    public static void main(String[] args) {

    Test test = new Test();

    test.gui();

    }

    }

    展开全文
  • JTextPane textPane = new JTextPane(doc); textPane.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has " + "been the industry's standard dummy text ever...

    如果要更改文本框中每个字符的样式,这里是一个完整的随机方式来执行.您为每个字符创建一个不同的属性集.由你找到适当的组合(前景/背景对比,字符大小不同等等).您还可以存储已应用的不同样式,以使两次不使用相同的样式.

    import java.awt.Color;

    import java.util.Random;

    import javax.swing.JFrame;

    import javax.swing.JScrollPane;

    import javax.swing.JTextPane;

    import javax.swing.text.DefaultStyledDocument;

    import javax.swing.text.SimpleAttributeSet;

    import javax.swing.text.StyleConstants;

    import javax.swing.text.StyledDocument;

    public class TestDifferentStyles {

    private void initUI() {

    JFrame frame = new JFrame(TestDifferentStyles.class.getSimpleName());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument doc = new DefaultStyledDocument();

    JTextPane textPane = new JTextPane(doc);

    textPane.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has "

    + "been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of "

    + "type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the "

    + "leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the"

    + " release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing "

    + "software like Aldus PageMaker including versions of Lorem Ipsum.");

    Random random = new Random();

    for (int i = 0; i < textPane.getDocument().getLength(); i++) {

    SimpleAttributeSet set = new SimpleAttributeSet();

    // StyleConstants.setBackground(set, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));

    StyleConstants.setForeground(set, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));

    StyleConstants.setFontSize(set, random.nextInt(12) + 12);

    StyleConstants.setBold(set, random.nextBoolean());

    StyleConstants.setItalic(set, random.nextBoolean());

    StyleConstants.setUnderline(set, random.nextBoolean());

    doc.setCharacterAttributes(i, 1, set, true);

    }

    frame.add(new JScrollPane(textPane));

    frame.setSize(500, 400);

    frame.setVisible(true);

    }

    public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {

    @Override

    public void run() {

    new TestDifferentStyles().initUI();

    }

    });

    }

    }

    展开全文
  • NULL 博文链接:https://gh0769.iteye.com/blog/598839
  • JTextPane的例子,模拟聊天客户端,演示了为每段文字设置字体、字号、样式、颜色、背景色和插入图片功能   import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; ...
  • // 设置字体的默认颜色为黑色; public ArtFont() { super("字体设置"); // 设置默认字体 boldStyle = 0; italicStyle = 0; underlineStyle = 0; fontSizeStyle = 10; fontNameStyle = "宋体"; font = new Font...
  • 7、文本字体颜色设置 MutableAttributeSet attr = new SimpleAttributeSet(); StyleConstants.setForeground(attr, fg); setCharacterAttributes(editor, attr, false); fg:为color 8、文本背景颜色设置 ...
  • java设置字体颜色

    千次阅读 2021-04-23 06:18:22
    Color color= colorChooser.showDialog(null, "字体颜色", Color.BLACK); Document document= editorPane.getDocument(); try { StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset...
  • Java设置字体颜色

    千次阅读 2021-03-07 14:21:15
    } private void appendToPane(JTextPane tp, String msg, Color c) { StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants....
  • 在swing界面开发的时候,为了给我们的界面改变的更加美观,所以要给界面添加背景图片,这里我总结了一个demo进行操作这些元素,包括背景图片的添加,JtextPane添加背景图片不覆盖文字,还有Swing界面启动的时候替换...
  • 怎样设置字体颜色?java swing关注:251答案:3mip版解决时间 2021-02-07 09:26提问者关系已逝2021-02-06 22:37""+rs.getString(2)+""+rs.getString(3)+""+rs.getString(4)+""我只想设置rs.getString(2)的字体颜色,rs...
  • 这将打印出两种不同颜色的“BLAH BLEG”.public class Main {public static void main(String[] args) {JTextPane textPane = new JTextPane();StyledDocument doc = textPane.getStyledDocument();Style style = ...
  • java swing 文本域中改变字体颜色

    万次阅读 多人点赞 2016-05-02 15:42:34
    我们常用的JTextArea是纯文本组件,不能改变字体颜色,可以用JTextPane组件,它要比...1.用JTextPane的setForeground设置字体颜色。 package java; import java.awt.Color; import javax.swing.JFrame; import
  • } } 相比较JTextPane还有更多的选项,例如,用HTML或不使用HTML、字体或通过使用HTML或直接将另一个JComponent放入(也知道JTextArea,但是.)import java.awt.*; import javax.swing.*; import javax.swing.text....
  • 在java中文本编辑器单独设置文字的控件有个JTextPane,而通常使用的JTextArea似乎不能设置所选文字颜色字体。//实例化一个文本编辑的控件JTextPane editorPane=new JTextPane();//根据所选颜色进行设置...
  • final JTextPane t = new JTextPane(); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Color selectedColor = JColorChooser.showDialog(f, "zb", t....
  • import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Shape;import javax.swing.JFrame;import javax.swing....import javax.swing.JTextPane;import javax.swing....
  • 该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class JTextPaneTest extends JFrame { private static final long ... // 字体 } }
  • java的JTextArea中怎么改变字体颜色

    千次阅读 2021-03-09 17:44:27
    展开全部java swing 中JTEXTAREA不能改变字体颜色,它是纯文本组件,可以使用JTEXTPANE,通过操作DOCUMENT文档来控制JTEXTPANE显示的内容,下面的代码在一个32313133353236313431303231363533e58685e5aeb...
  • } a:0:左,1:中,2:右 7、文本字体颜色设置 MutableAttributeSet attr = new SimpleAttributeSet(); StyleConstants.setForeground(attr, fg); setCharacterAttributes(editor, attr, false); fg:为color 8、...
  • I have a JTextPane (or JEditorPane, I can use either no problem). How can I change the font of a selected area to a specific font?textpane.getSelectedText().setFont() won't work. (Even with font-fa...
  • 如何在JTextField或两种颜色的jLabel中显示文本.例如:1 0 0 0 1 1 1 0 1textField.setForeground(Color.RED ,BLUE);例如,定位单个RED最佳答案Different font color in a JTextField直接从HERE使用JTextPane的示例...
  • //颜色 if(bold==true){ StyleConstants.setBold(attrSet, true); }//字体类型 StyleConstants.setFontSize(attrSet, fontSize); //字体大小 insert(str, attrSet); } public void gui() { textPane.insert...

空空如也

空空如也

1 2 3 4 5 ... 19
收藏数 369
精华内容 147
关键字:

jtextpane设置字体颜色