java - 如何更改 JFrame 中的字体颜色

标签 java swing fonts jframe

我想在按下按钮时更改字体颜色。

字体颜色由 3 个单选按钮选择。

当我按下按钮时,makeStyleSheet 函数会生成一个 css。

在此 css 中,仅定义了一种颜色。

但是字体颜色没有改变......

为什么???

@SuppressWarnings("javadoc")
public class Sample extends JFrame {

    JRadioButton[] radio;
    JTextPane textPane;
    HTMLEditorKit kit;
    HTMLDocument doc;
    JButton btnNewButton;
    ButtonGroup group;
    List<JRadioButton> list;

    /**
     * @param args
     */
    public static void main(String[] args) {
        Sample frame = new Sample();
        ActionListener action = new MyButtonAction(frame.kit, frame.doc, frame.list);

        frame.addAction(action);
        frame.setVisible(true);
    }

    private void addAction(ActionListener action) {

        btnNewButton.addActionListener(action);
    }

    @SuppressWarnings("javadoc")
    public Sample() {

        doc = new HTMLDocument();
        textPane = new JTextPane();
        kit = new HTMLEditorKit();

        setBounds(100, 100, 450, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

       textPane.setEditorKit(kit);
       textPane.setDocument(doc);
        textPane.setContentType("text/html");
        textPane.setBounds(12, 13, 282, 229);
        getContentPane().add(textPane);

        btnNewButton = new JButton("HTML");
        btnNewButton.setBounds(303, 211, 117, 31);

        getContentPane().add(btnNewButton);

        group = new ButtonGroup();

        list = new ArrayList<JRadioButton>();

       JRadioButton rdbtnRed = new JRadioButton("apple", true);
        JRadioButton rdbtnYellow = new JRadioButton("banana");
        JRadioButton rdbtnGreen = new JRadioButton("mellon");

       list.add(rdbtnRed);
       list.add(rdbtnYellow);
      list.add(rdbtnGreen);

       group.add(rdbtnRed);
       group.add(rdbtnYellow);
        group.add(rdbtnGreen);

        rdbtnRed.setBounds(302, 161, 113, 21);
        getContentPane().add(rdbtnRed);
        rdbtnYellow.setBounds(302, 138, 113, 21);
        getContentPane().add(rdbtnYellow);
        rdbtnGreen.setBounds(302, 115, 113, 21);
        getContentPane().add(rdbtnGreen);
    }
}

class MyButtonAction implements ActionListener {

    HTMLEditorKit kit;
        HTMLDocument doc;
        List<JRadioButton> list;

    public MyButtonAction(HTMLEditorKit kit, HTMLDocument doc, List<JRadioButton> list) {

        this.kit = kit;
        this.doc = doc;
        this.list = list;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        String tag = null;

        for(JRadioButton btn : list) {

            if (btn.isSelected()){

                tag = btn.getText();
            }
        }

        kit.setStyleSheet(makeStyleSheet(tag));

        try {

            kit.insertHTML(doc, doc.getLength(), "<font class=\"apple\">apple</font>,<font class=\"banana\">banana</font>,<font class=\"mellon\">mellon</font>", 0, 0, null);
        } catch (IOException e1) {

            e1.printStackTrace();
        } catch (BadLocationException e1) {

            e1.printStackTrace();
        }
    }

    private StyleSheet makeStyleSheet(String tag) {

        StyleSheet styleSheet = new StyleSheet();
        styleSheet.addRule("." + tag + " {color: red;}");

        return styleSheet;
    }
}

最佳答案

将 CSS 规则添加到 doc 的样式表,而不是 kit

    doc.getStyleSheet().addRule(".apple {color: red;}");
    doc.getStyleSheet().addRule(".banana {color: yellow;}");
    doc.getStyleSheet().addRule(".mellon {color: green;}");

顺便说一句:更常规的是使用 <span> i.o.一个<font> .

关于java - 如何更改 JFrame 中的字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16817742/

相关文章:

java - 在里面渲染一个图标

java - 如何在 Java Swing 中的拖放过程中启用工具提示显示

css - SCSS/SASS 条件化一个变量

javascript - 字符串的正则表达式模式包含 "at least one digit and allows only two special chars"

java - this.getClass().getClassLoader().getResourceAsStream 始终返回 null

具有多个 JFrame 的 java.util.observer

java - Docx4j - 更改 docDefaultStyle 的语言设置/将自定义(或更改的)库存样式正确应用到 docx 中的任何文本

java - 使用 Nimbus 外观时 writeObject() 期间出现 NullPointerException

java - 如何将这个 &lt;input type ="text"name ="name[]"value =""/> 从html传递到jsp

c - 渲染的字体看起来比应有的更粗