如何轻松编辑JTextPane中选定文本的样式?这似乎没有很多资源。即使你能指导我一个很好的资源,我也会非常感激。如何轻松编辑JTextPane中选定文本的样式?
另外,如何获取所选文本的当前样式?我试过styledDoc.getLogicalStyle(textPane.getSelectionStart());
,但它似乎没有工作。
如何轻松编辑JTextPane中选定文本的样式?这似乎没有很多资源。即使你能指导我一个很好的资源,我也会非常感激。如何轻松编辑JTextPane中选定文本的样式?
另外,如何获取所选文本的当前样式?我试过styledDoc.getLogicalStyle(textPane.getSelectionStart());
,但它似乎没有工作。
采取这种引擎收录一起来看看下面的代码:
http://pbin.oogly.co.uk/listings/viewlistingdetail/d6fe483a52c52aa951ca15762ed3d3
的例子就是从这里开始:
http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample3.htm
看起来你可以用下面的改变风格在动作监听器中:
final Style boldStyle = sc.addStyle("MainStyle", defaultStyle);
StyleConstants.setBold(boldStyle, true);
doc.setCharacterAttributes(0, 10, boldStyle, true);
它将给定偏移量和长度之间的文本样式设置为特定样式。
查看完整的pastebin获取更多详细信息。这应该解决您的问题。
我建议看看Sun的Java Tutorial关于编辑器窗格。
下面是插入格式化“Hello World!”的代码片段。字符串在JEditorPane
:
Document doc = yourEditorPane.getDocument();
StyleContext sc = new StyleContext();
Style style = sc.addStyle("yourStyle", null);
Font font = new Font("Arial", Font.BOLD, 18);
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setFontFamily(style, font.getFamily());
StyleConstants.setBold(style, true);
doc.insertString(doc.getLength(), "Hello World!", style);
我不知道为什么这是因为这是最正确的答案而被低估了! – 2009-09-17 11:53:46
好的,哇。难以回答的问题。所以我还没有找到一种方法来获得给定角色的风格。但是,您可以获取给定字符的MutableAttributeSet,然后测试该样式是否属于该属性集。与得到的风格字符范围
Style s; //your style
Element run = styledDocument.getCharacterElement(
textPane.getSelectionStart());
MutableAttributeSet curAttr =
(MutableAttributeSet)run.getAttributes();
boolean containsIt = curAttr.containsAttributes(s);
的一个问题是,有可能应用到的范围超过一个风格(例如:你可以选择文本,其中一些是大胆的,有些则不是)。
要更新选定的文本,您可以:
Style s; //your style
JTextPane textPane; //your textpane
textPane.setCharacterAttributes(s, false);
哦,看来该功能getLogicalStyle不起作用,因为它返回的默认样式(或者只是样式)为包含段落p,而不是p上角色的风格。
操作文本面板的最简单方法是使用editor kits及其关联的actions。您可以在JDK示例(jdk \ demo \ jfc \ Stylepad)下找到此示例。
public static void main(String[] args) {
// create a rich text pane
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// install the editor kit
StyledEditorKit editorKit = new StyledEditorKit();
textPane.setEditorKit(editorKit);
// build the menu
JMenu fontMenu = new JMenu("Font Size");
for (int i = 48; i >= 8; i -= 10) {
JMenuItem menuItem = new JMenuItem("" + i);
// add an action
menuItem
.addActionListener(new StyledEditorKit.FontSizeAction(
"myaction-" + i, i));
fontMenu.add(menuItem);
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(fontMenu);
// show in a frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setJMenuBar(menuBar);
frame.setContentPane(scrollPane);
frame.setVisible(true);
}
(提示:如果你想使用FontFamilyAction,看看GraphicsEnvironment.getAvailableFontFamilyNames()和logical font family names)
能安装 StyledEditorKit和使用 FontSizeAction处理的文字示例代码
很好的例子,但我有一个问题。我如何使用样式获取文本以及a如何在数据库中使用样式保存此文本? – Krismorte 2016-07-04 20:53:35
我刚刚访问了java2s链接,哇,很多示例。 – extraneon 2009-05-14 09:22:09