标题说明了一切。假设我有一个带有“删除所选文本”选项的右键菜单。当我在 jtextpane 中选择了一些文本时,右键单击 --> "Strikethrough selected text" ,选定的文本就会被删除。
有任何想法吗?
标题说明了一切。假设我有一个带有“删除所选文本”选项的右键菜单。当我在 jtextpane 中选择了一些文本时,右键单击 --> "Strikethrough selected text" ,选定的文本就会被删除。
有任何想法吗?
Swing 文本组件用于Actions
提供文本窗格的各种格式设置功能。
以下是 的UnderlineAction
代码StyledEditorKit
。
public static class UnderlineAction extends StyledTextAction {
/**
* Constructs a new UnderlineAction.
*/
public UnderlineAction() {
super("font-underline");
}
/**
* Toggles the Underline attribute.
*
* @param e the action event
*/
public void actionPerformed(ActionEvent e) {
JEditorPane editor = getEditor(e);
if (editor != null) {
StyledEditorKit kit = getStyledEditorKit(editor);
MutableAttributeSet attr = kit.getInputAttributes();
boolean underline = (StyleConstants.isUnderline(attr)) ? false : true;
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setUnderline(sas, underline);
setCharacterAttributes(editor, sas, false);
}
}
}
所以基本上你需要通过替换“下划线”StyleConstants 方法来创建自己的“StrikeThroughAction”,以使用“删除线”StyleConstants 方法。
一旦您创建了一个动作,您就可以通过使用该动作创建一个 JMenuItem 或 JButton 来使用该动作。单击组件时,删除线属性将添加到选定的文本中。
在您的右键单击操作中
objJTextPane.setContentType( "text/html" );
String[] args = objJTextPane.getText().split(objJTextPane.getSelectedText());
objJTextPane.setText("<strike>" + objJTextPane.getSelectedText() + "</strike>"+ args[1].toString());
将您的逻辑应用于拆分字符串。