Tag: jtextpane

使用Swing设置选定的文本颜色

我想显示一个用户选择颜色的选定文本。我的问题是我选择了一些文本并点击settextcolor它应用于所有文本未选择的文本。请给我..这是我的代码: public class SetTextColor extends javax.swing.JFrame { int i=0; JTextPane textPane; JScrollPane scrollPane; public SetTextColor() { initComponents(); } @SuppressWarnings(“unchecked”) // private void initComponents() { tabbedPane = new javax.swing.JTabbedPane(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); Create = new javax.swing.JMenuItem(); SetTextColor = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jMenu1.setText(“File”); Create.setText(“Create”); Create.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { […]

保持文本检索的格式

我正在制作一个具有聊天function的网络应用程序。 在聊天中,我有一个用于显示消息的JTextPane和一个用于输入的消息。 然后我有一些按钮,允许在输入文本上添加样式(粗体,斜体,字体大小,颜色)。 文本在输入窗格上正确格式化,但是当移动到显示窗格时(一旦按下正确的JButton ),它只具有最后一个字符的格式。 如何在保持原始格式的同时移动文本?例如,如果我在输入上写“Hello Worl d ”,则显示“Hello Worl d” textPane是输入窗格 设置地点: final SimpleAttributeSet set = new SimpleAttributeSet(); 使输入文本加粗的代码(与添加其他样式相同): bold.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { StyledDocument doc = textPane.getStyledDocument(); if (StyleConstants.isBold(set)) { StyleConstants.setBold(set, false); bold.setSelected(false); } else { StyleConstants.setBold(set, true); bold.setSelected(true); } textPane.setCharacterAttributes(set, true); } }); 用于将文本从输入窗格移动到显示窗格的代码: getInput.addActionListener(new ActionListener() { @Override […]

当我将文本从java servlet加载到JTextPane时,为什么要松开换行符呢?

我尝试使用java servlet加载包含多行文本的文本文件的内容。 当我在浏览器中测试servlet时它运行正常。 文本加载了新的行号。 但是当我将它加载到我的swing应用程序中的字符串然后使用textpane.setText(text); 新线路消失了。 我尝试了很多我在网上找到的解决方案,但仍然无法做到正确。 Servlet代码: 从文件中读取文本(简化): File file = new File(path); StringBuilder data = new StringBuilder(); BufferedReader in = new BufferedReader(new FileReader(file)); String line; while ((line = in.readLine()) != null) { data.append(line); data.append(“\n”); } in.close(); 发送文字: PrintWriter out = response.getWriter(); out.write(text)); 这是一个平台问题吗? Servlet是在Linux上编写和编译的,但是我在Windows上运行它(在JBoss上)。 文本文件也存储在我的机器上。

Java字符串replaceAll()

我一直在想,例如: JTextPane chatTextArea = new JTextPane(); s.replaceAll(“:\\)”, emoticon()); public String emoticon(){ chatTextArea.insertIcon(new ImageIcon(ChatFrame.class.getResource(“/smile.png”))); return “`”; } 可以放一张图片和“`”到处“:)”被找到。 当我像这样运行它时如果s包含“:)”那么整个s只会被图标替换掉。 有办法吗?

Java JTextPane RTF保存

我有以下代码试图将JTextPane的内容保存为RTF。 虽然在以下代码中创建了一个文件,但它是空的! 关于我做错什么的任何提示? (像往常一样不要忘记我是初学者!) if (option == JFileChooser.APPROVE_OPTION) { //////////////////////////////////////////////////////////////////////// //System.out.println(chooser.getSelectedFile().getName()); //System.out.println(chooser.getSelectedFile().getAbsoluteFile()); /////////////////////////////////////////////////////////////////////////// StyledDocument doc = (StyledDocument)textPaneHistory.getDocument(); RTFEditorKit kit = new RTFEditorKit(); BufferedOutputStream out; try { out = new BufferedOutputStream(new FileOutputStream(chooser.getSelectedFile().getName())); kit.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength()); } catch (FileNotFoundException e) { } catch (IOException e){ } catch (BadLocationException e){ } } 编辑:HTMLEditorKit如果我使用HTMLEditorKit它的工作原理,这就是我真正想要的。 解决了!

将图片插入JTextPane

在我的记事本应用程序中,我试图通过单击名为Picture的JMenuItem将图像添加到JTextPane ,就好像它是JLabel 。 private class Picture implements ActionListener { public void actionPerformed(ActionEvent event) { fc = new JFileChooser(); FileNameExtensionFilter picture = new FileNameExtensionFilter(“JPEG files (*.jpg)”, “jpg”); fc.setFileFilter(picture); fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); if (fc.showDialog(Notepad.this, “Insert”)!=JFileChooser.APPROVE_OPTION) return; filename = fc.getSelectedFile().getAbsolutePath(); // If no text is entered for the file name, refresh the dialog box if (filename==null) return; // NullPointerException textArea.insertIcon(createImageIcon(filename)); […]

Java JTextPane HTML编辑器UTF-8字符编码

我正在使用JTextPane作为简单的html编辑器。 jtp=new JTextPane(); jtp.setContentType(“text/html;charset=UTF-8”); jtp.setEditorKit(new HTMLEditorKit()); 当我调用jtp.getText()时,我获得了很好的html代码,所有特殊字符都被转义。 但是我不希望逃避国家角色(波兰语),但只有特殊的html字符,如&,当我进入编辑器时 ą ś & 我明白了 <foo>ą ś & 但我想得到 <foo>ą ś & 怎么可能?

在JTextPane(Java Swing)中更改段落的背景颜色

是否可以在Java Swing中更改段落的背景颜色? 我尝试使用setParagraphAttributes方法(下面的代码)设置它,但似乎不起作用。 StyledDocument doc = textPanel.getStyledDocument(); Style style = textPanel.addStyle(“Hightlight background”, null); StyleConstants.setBackground(style, Color.red); Style logicalStyle = textPanel.getLogicalStyle(); doc.setParagraphAttributes(textPanel.getSelectionStart(), 1, textPanel.getStyle(“Hightlight background”), true); textPanel.setLogicalStyle(logicalStyle);

如何垂直居中JTextPane中的文本和JComponent?

目前它看起来如此 怎么做它看起来如此? 以下是我的代码: JFrame f = new JFrame(); JTextPane textPane = new JTextPane(); JTextField component = new JTextField(” “); component.setMaximumSize(component.getPreferredSize()); textPane.setSelectionStart(textPane.getDocument().getLength()); textPane.setSelectionEnd(textPane.getDocument().getLength()); textPane.insertComponent(component); try { textPane.getDocument().insertString(textPane.getDocument().getLength(), “text”, new SimpleAttributeSet()); } catch (BadLocationException e) { // TODO Auto-generated catch block e.printStackTrace(); } f.add(new JScrollPane(textPane)); f.setSize(200, 100); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); 我发现接近这个主题的单个问题: JTextPane插入组件,错误的垂直对齐但是如何更改对齐没有答案。 但根据那里的讨论,它必须是可能的。

Java Print API – 使用等宽的“Courier New”字体错误地打印空格字符

我先来描述下图: 有两份印刷纸。 它们之间的唯一区别是左边纸张中的空格“”字符很少被点“替换”。 右边的文字中的人物。 红线表示文本应对齐的左边框 绿色曲线表示我打算将它连接的所有字符对齐到一个列中。 事实上,绿色曲线应该是一条垂直线。 我希望绿线突出显示的所有字符都打印在一列中。 String的字体是monospaced Courier New。 但是,似乎空格字符不会打印为等宽字体(请参阅“虚线”线与开头的空格字符行)。 要打印字符串,我在JTextPane组件上使用标准Java Print Service API: PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPrintable(myTextPane); pj.print(); 据我所知,Java Print Service API实际上调用了myTextPane的paint()方法。 因此,预览应与String的打印版本完全相同。 但事实并非如此。 预览似乎没有误解等宽空间字符(参见最后一张图)。 预览看起来与我想要打印的文本完全相同。 有关如何强制JavaPrintServiceAPI正确打印等宽空格字符的任何建议吗?