e1008. sharing styles between jtextpanes.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 18 行

TXT
18
字号
Styles are stored in a style context. By default, text panes do not share style contexts, so any styles created for a text pane are available only in that text pane. This example creates two text panes that share a style context. Any new styles created in one text pane are available in the other. This also applies to changes in a shared style. 
    JTextPane c1 = new JTextPane();
    JTextPane c2 = new JTextPane();
    
    // Create new styled documents with the same style context
    StyleContext styleContext = new StyleContext();
    c1.setDocument(new DefaultStyledDocument(styleContext));
    c2.setDocument(new DefaultStyledDocument(styleContext));
    
    // Create a new style with one text pane
    Style style = c1.addStyle("style name", null);
    StyleConstants.setForeground(style, Color.red);
    
    // Modify an existing style using the other text pane
    style = c2.getStyle("style name");
    StyleConstants.setBold(style, true);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?