- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 RTFEditorKit()
创建一个简单的 .rtf
文件编辑器应用程序。我添加了代码来创建新文档,打开 .rtf
文档,另存为 .rtf
文档,并向文档内容添加样式(例如粗体) 、斜体和下划线。
我正在使用JTextPane
。
这是我的问题:我在文本内容中添加了一些样式(例如粗体、斜体、下划线或颜色)。然后,无论保存还是不保存该文档,我都可以通过单击“新文档”图标打开一个新文档。
如果我在新文档中输入一些文本,该文本将以我在上一个文档中使用的粗体、斜体、下划线和颜色样式显示;而我本希望这些已被清除。
我怎样才能实现这个目标?我在“新文档” Action 监听器中尝试了三种不同的方法 - 它们都不起作用。这些可以在下面看到:
StyledDocument styledDocument = new DefaultStyledDocument();
textPane.setStyledDocument(styledDocument = new DefaultStyledDocument());
<小时/>
Style bold = textPane.addStyle("bold", null);
textPane.getStyledDocument().setCharacterAttributes(0, textPane.getStyledDocument().getLength(), bold, false);
<小时/>
textPane.setFont(new Font("Arial", Font.PLAIN, 15));
textPane.setText("");
我的应用程序最少代码:
public class MyNotepadMini implements ActionListener {
public static JFrame frame;
public static JPanel panel;
public static JTextPane textPane;
public static RTFEditorKit rtf;
public static StyleContext styleContext;
public static DefaultStyledDocument defaultStyleDoc;
public static JScrollPane scrollPane;
public static JMenuBar menuBar;
public static JMenu fileMenu;
public static JMenu editMenu;
public static JMenu formatMenu;
public static JMenuItem newSubMenu;
public static JMenuItem openSubMenu;
public static JMenuItem save;
public static JMenuItem saveAs;
public static JMenuItem cut;
public static JMenuItem copy;
public static JMenuItem paste;
public static JMenuItem selectAll;
public static JMenuItem bold;
public static JMenuItem italic;
public static JMenuItem underline;
public static JMenuItem exit;
public static JFileChooser fc;
public static boolean openFileExtFlag = true;
public static boolean saveFileExtFlag = true;
public static File openFile;
public static File saveFile;
public static boolean saveWindowTitle = false;
public static boolean openFileFlag;
public static boolean saveFileFlag;
public static boolean saved = true;
public static boolean dontSaveOption;
public static BufferedReader br;
public static boolean saveForNewOpenExitListener;
public static boolean saveAsFlag;
public static int returnVal;
public static String filePath;
public static boolean flagForOpenListener;
public static StyledDocument styledDocument;
public static Style defaultStyle;
public MyNotepadMini() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
frame = new JFrame("My Notepad");
panel = new JPanel(new BorderLayout());
styledDocument = new DefaultStyledDocument();
rtf = new RTFEditorKit();
textPane = new JTextPane(styledDocument);
textPane.setEditorKit(rtf);
styleContext = new StyleContext();
scrollPane = new JScrollPane();
scrollPane.getViewport().add(textPane);
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
newSubMenu = new JMenuItem("New");
newSubMenu.setAccelerator(KeyStroke.getKeyStroke('N', CTRL_DOWN_MASK));
openSubMenu = new JMenuItem("Open...");
openSubMenu.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
save = new JMenuItem("Save");
save.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
cut = new JMenuItem("Cut");
cut.setAccelerator(KeyStroke.getKeyStroke('X', CTRL_DOWN_MASK));
copy = new JMenuItem("Copy");
copy.setAccelerator(KeyStroke.getKeyStroke('C', CTRL_DOWN_MASK));
paste = new JMenuItem("Paste");
paste.setAccelerator(KeyStroke.getKeyStroke('V', CTRL_DOWN_MASK));
selectAll = new JMenuItem("Select All");
selectAll.setAccelerator(KeyStroke.getKeyStroke('A', CTRL_DOWN_MASK));
exit = new JMenuItem("Exit");
exit.setMnemonic(KeyEvent.VK_X);
formatMenu = new JMenu("Format");
formatMenu.setMnemonic(KeyEvent.VK_F);
bold = new JMenuItem(new StyledEditorKit.BoldAction());
bold.setText("Bold");
bold.setMnemonic(KeyEvent.VK_B);
bold.setAccelerator(KeyStroke.getKeyStroke('B', CTRL_DOWN_MASK));
italic = new JMenuItem(new StyledEditorKit.ItalicAction());
italic.setText("Italic");
italic.setMnemonic(KeyEvent.VK_I);
italic.setAccelerator(KeyStroke.getKeyStroke('I', CTRL_DOWN_MASK));
underline = new JMenuItem(new StyledEditorKit.UnderlineAction());
underline.setText("Underline");
underline.setMnemonic(KeyEvent.VK_U);
underline.setAccelerator(KeyStroke.getKeyStroke('U', CTRL_DOWN_MASK));
defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
exit.addActionListener(this);
newSubMenu.addActionListener(this);
editMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent event) {
if (textPane.getSelectionStart() == textPane.getSelectionEnd()) {
cut.setEnabled(false);
copy.setEnabled(false);
} else {
cut.setEnabled(true);
copy.setEnabled(true);
}
}
public void menuDeselected(MenuEvent event) {
}
public void menuCanceled(MenuEvent event) {
}
});
scrollPane.setPreferredSize(new Dimension(700,500));
fileMenu.add(newSubMenu);
fileMenu.add(openSubMenu);
fileMenu.add(save);
fileMenu.add(exit);
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
editMenu.addSeparator();
editMenu.add(selectAll);
formatMenu.add(bold);
formatMenu.add(italic);
formatMenu.add(underline);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(formatMenu);
textPane.setFont(new Font("Arial", Font.PLAIN, 12));
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(new JLabel(" "), BorderLayout.EAST);
panel.add(new JLabel(" "), BorderLayout.WEST);
panel.add(new JLabel(" "), BorderLayout.SOUTH);
frame.setJMenuBar(menuBar);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.pack();
textPane.requestFocus();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new MyNotepadMini();
}
public void actionPerformed(ActionEvent ae) {
if ((ae.getSource() == cut)) {
textPane.cut();
textPane.requestFocus();
} else if ((ae.getSource() == copy)) {
textPane.copy();
textPane.requestFocus();
} else if ((ae.getSource() == paste)) {
textPane.paste();
textPane.requestFocus();
} else if (ae.getSource() == selectAll) {
textPane.selectAll();
} else if ((ae.getSource() == exit)){
System.exit(0);
} else if ((ae.getSource() == newSubMenu)) { /** New document **/
textPane.setText("");
textPane.setStyledDocument(styledDocument = new DefaultStyledDocument());
Style bold = textPane.addStyle("bold", null);
textPane.getStyledDocument().setCharacterAttributes(0, textPane.getStyledDocument().getLength(), bold, false);
/*sampleDocument.setCharacterAttributes(0, sampleDocument.getLength(), defaultStyle, true);
defaultStyleDoc = new DefaultStyledDocument(styleContext);
textPane.setDocument(defaultStyleDoc);*/
/*textPane.setFont(new Font("Arial", Font.PLAIN, 15));
textPane.setText("");*/
textPane.requestFocus();
}
}
}
最佳答案
I have given the minimal code here.
这不是最少的代码或适当的 MCVE”。
我们应该能够复制/粘贴/编译/测试。因此您需要包含导入语句
剪切/复制/粘贴操作和菜单项与问题无关,因此不应包含它们。
我们只有一定的时间来回答问题,因此我们只想查看与问题直接相关的最少代码。
How to get rid of these styles in the New document - the text should be plain.
问题不在于文档。
每次移动文本 Pane 的插入符号时,文本 Pane 都会跟踪插入符号位置处的输入属性。因此,当您创建新文档时,如果脱字符号恰好位于具有 3 个属性的字符上,则这些属性将在您下次开始输入时保留。
您可以使用以下方法清除这些属性:
MutableAttributeSet mas = textPane.getInputAttributes();
System.out.println("before: " + mas);
mas.removeAttributes(mas);
System.out.println("after: " + mas);
当您创建新文档时。
另外,
public static JFrame frame;
public static JPanel panel;
public static JTextPane textPane;
您不应在任何变量中使用 static 关键字。
关于java - 如何清除JTextPane中的所有样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52790001/
我喜欢调整 目录的样式(例如背景颜色、字体)预订 , Gitbook 风格 HTML 文档。 这可能吗?如果是这样,有人可以善意地指出我可以开始这样做的地方吗? 谢谢你。 最佳答案 两个步骤: 1)
是否可以使用纯 CSS 选择器根据子节点的兄弟节点数量为节点子节点(在我的例子中为 UL)提供不同的属性,特别是高度? 例如,如果一个节点有 1 个子节点,则 UL 的高度是自动的,但是如果该节点有
我正在与 Vala 一起工作,它首先编译为 C,然后正常从 C 编译。 valac 的一项功能(Vala 编译器)是为 .vala 生成“fast-vapi”文件。 fast-vapi 本质上是为 .
我有两个具有 .body 类的 div,但是,一个位于另一个具有 .box 类的 div 中 - 如下所示: 我只想为 .box 内部的 .body 设置样式...但我在下面所
**注意所有 <> 标签已被删除以允许代码显示**我已经玩了好几个小时了,如果不在设计结束时使用解决方法(即 Corel 绘图),我就无法真正让它工作 *在我继续之前, 首先,网站 URL 是 Adv
我从一个服务中接收到一个字符串,该字符串显然使用 UTF-32 编码对其 unicode 字符进行编码,例如:\U0001B000(C 风格的 unicode 编码)。但是,为了在 JSON 中序列化
我在应用程序资源中有一种样式,我想将其应用于许多不同的饼图。样式如下所示: 为了简单起见,我排除了更多的属性。这一切都很好。现在,我的一些馅饼需要有一个不同的“模型
想象一下,我有一个名为“MyCheckBoxStyle”的 CheckBox 自定义样式。 如何制作基于 MyCheckBoxStyle 嵌入自定义 DataGridCheckBoxColumn 样式
我有一个 Button我在 WPF 中开发的样式,如 this question 中所述.我想用这种风格做的另一件事是拥有 Button缩小一点点,使其看起来像被点击一样被点击。现在,转换代码如下所示
我为超链接控件创建了一个样式:
不知道为什么,但我的 typeahead.js 远程自动完成停止工作。我没有更改任何关于 typeahead.js 的代码,但既然它坏了,我一定是错的。你能看看我的site here吗? ?我会创建
有没有办法创建扩展当前样式的样式,即不是特定样式? 我有一个 WPF 应用程序,我在其中创建样式来设置一些属性,例如边框或验证。 现在我想尝试一些主题,看看哪
我正在为一个网站提出问题,并希望 var reltext 中的正确/再试消息具有不同的颜色,即绿色表示正确,红色表示错误,并且每个旁边可能有一个小 png。 有什么想法吗? A local co
我想到达列表的父节点(使用 id 选择器)并使用纯 JavaScript 添加背景颜色来设置其样式。这是我的代码,但不起作用。 var listParentNode; listPare
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 4 年前。 Improve th
过去几天我一直在与这段代码作斗争,我真的不知道该如何处理它。 基本上,当用户将鼠标滚动到主导航菜单中的某个 LI 元素上时,就会运行一个 Javascript 函数,并根据触发该函数的元素将链接放入下
使用这个可爱的 html 和 css 作为指南,我能够在我的照片上显示我的姓名首字母。 这很好,但是,如果图像不存在,我只想显示首字母;如果图像存在,则不应渲染 peron 首字母。 换句话说,当该图
使用这个可爱的 html 和 css 作为指南,我能够在我的照片上显示我的姓名首字母。 这很好,但是,如果图像不存在,我只想显示首字母;如果图像存在,则不应渲染 peron 首字母。 换句话说,当该图
是否有人尝试过将 JButton 设计为看起来像 NetBeans 工具栏按钮?这将只显示一张图片,当您将鼠标悬停在它上面时,会显示 1px 圆形角灰色边框,并且按钮顶部和底部的背景不同......似
在 Ax2012 中使用图表,它们工作正常。但我想更改它在启动时显示的图表类型,例如“样条”图表,而不是默认的“柱状图”图表。 这是我现在拥有的: http://i.stack.imgur.com/R
我是一名优秀的程序员,十分优秀!