如何輕鬆編輯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