java get font style jtextpane

时间:2014-01-03 05:34:09

标签: java swing get styles jtextpane

我有这个代码将所选文本以粗体显示,并且工作正常,但我如何删除样式? 我想制作一个按钮,当点击按钮时,放置并删除样式。 我想过做一个if,但我怎么比较?

    StyledDocument doc = texto.getStyledDocument();
    int start = texto.getSelectionStart();
    int end = texto.getSelectionEnd();
    if (start == end) { // No selection, cursor position.
    return;
        }
        if (start > end) { // Backwards selection?
    int life = start;
    start = end;
    end = life;
        }
    Style style = texto.addStyle("negra", null);
    StyleConstants.setBold(style, true);
    doc.setCharacterAttributes(start, end - start, style, false);

2 个答案:

答案 0 :(得分:3)

我使用了你的代码并让它以这种方式工作。希望它有所帮助。

    package main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.text.*;

public class Main {

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JTextPane textPane = new JTextPane();

    JButton button = new JButton("Test");

    public Main() {
        frame.setTitle("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel.setLayout(new BorderLayout());
        panel.setPreferredSize(new Dimension(200, 200));

        panel.add(textPane, BorderLayout.CENTER);
        panel.add(button, BorderLayout.SOUTH);
        textPane.addStyle("negra", null);

        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                StyledDocument doc = textPane.getStyledDocument();
                int start = textPane.getSelectionStart();
                int end = textPane.getSelectionEnd();
                if (start == end) { // No selection, cursor position.
                    return;
                }
                if (start > end) { // Backwards selection?
                    int life = start;
                    start = end;
                    end = life;
                }
                Style style = textPane.getStyle("negra");

                // I think this is what you need
                if (StyleConstants.isBold(style)) {
                    StyleConstants.setBold(style, false);
                } else {
                    StyleConstants.setBold(style, true);
                }
                doc.setCharacterAttributes(start, end - start, style, false);
            }
        });

        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Main();
            }
        });

    }
}

答案 1 :(得分:0)

我为你做了一个演示,注意JTextPane相关问题似乎是一个复杂的问题,我还没有掌握它的所有方面。

该演示为您提供了一些可行的方法,您需要对其进行改进以满足您的需求。

选择文本并单击按钮以查看样式更改。

代码如下:

package com.learningjava;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.text.AttributeSet;
import javax.swing.*;
import javax.swing.text.*;

/**
 * Note: code is a possible way to do 
 * you need to improved it to meet your requirements
 * 
 * @author wangdq
 * 2013-1-3
 */
public class TextPaneStyleTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
                JFrame frame = new TextStyleTestFrame();
                frame.setSize(300, 300);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
/**
 * TextPane style test frame
 * Select text and click the button to see style change
 */
class TextStyleTestFrame extends JFrame {

    public TextStyleTestFrame() {
        super("TextPaneStyle Test");
        textPane.setText("Smaple String");
        this.add(textPane,BorderLayout.CENTER);
        JPanel panel = new JPanel();
        panel.add(btnStyle);
        this.add(panel,BorderLayout.NORTH);
        btnStyle.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                changeStyle();
            }
        });
    }
    /**
     * change style according to your rule
     */
    private void changeStyle() {

        //get selected text style
        StyledDocument doc=(StyledDocument)textPane.getDocument();
        int selectionEnd=textPane.getSelectionEnd();
        int selectionStart=textPane.getSelectionStart();
        if(selectionStart ==  selectionEnd)  {
            return;
        }
        Element element=doc.getCharacterElement(selectionStart);
        AttributeSet as = element.getAttributes();
        //apply a new style based on previous 
        MutableAttributeSet asNew = new SimpleAttributeSet(as.copyAttributes());
        StyleConstants.setBold(asNew, !StyleConstants.isBold(as));
        doc.setCharacterAttributes(selectionStart,textPane.getSelectedText().length(), asNew, true);
        String text = (StyleConstants.isBold(as) ? "Cancel Bold":"Bold");
        btnStyle.setText(text);
    }
    private static final long serialVersionUID = 1L;
    private JButton btnStyle = new JButton("Bold");
    private JTextPane textPane =new JTextPane();
}
相关问题