我在为给定字符串生成html代码时遇到问题,该字符串来自
JTextArea textArea=new JTextArea(10,10);
String textToHtml=textArea.getText();在这个生成的html代码中,它应该包含与给定字符串相关的所有html标记。
就像<p>something like this</p>,<br>等等,这也不是一个web应用程序。如果你知道怎么做的话。建议我。谢谢。
更新
例如,如果我键入一个带有换行符的文本,它应该会自动插入<br>或<p>。这种功能在Dreamweaver中是可用的。当您开始在网页中填充内容时,它会自动插入代码。问题是,我不希望非技术用户在编写段落时键入HTML代码,因为默认情况下,
标记和所有标记都被插入。在输入此文本后,我将直接将其发送给用户,因此这种格式是必不可少的。
值得注意的是,我不知道用户会输入什么,这完全取决于他。因此,无论我使用什么方法,都应该能够识别它可以放置标记的位置(比如段落标记)并继续前进。
发布于 2015-09-07 08:19:07
例如,如果我键入一个带有换行符的文本,它应该自动插入
<br>或<p>。
基本要求是在标记输入enter时使用DocumentFilter注入标记。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class TestMarkup {
public static void main(String[] args) {
new TestMarkup();
}
public TestMarkup() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JTextArea ta = new JTextArea(10, 20);
add(new JScrollPane(ta));
((AbstractDocument) ta.getDocument()).setDocumentFilter(new DocumentFilter() {
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if (text.endsWith("\n")) {
super.replace(fb, offset, 0, "<br>", attrs);
offset += 4;
}
super.replace(fb, offset, length, text, attrs);
}
});
}
}
}插入<p>/</p>“可能”更困难,但是,如果假设第一行以<p>开头,那么只需在换行符之前注入</p>,在换行符之后插入<p>,然后追加其余的文本.
用段落支持和粘贴更新的
显然我不能离开..。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.StringJoiner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;
import javax.swing.text.Element;
public class TestMarkup {
public static void main(String[] args) {
new TestMarkup();
}
public TestMarkup() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JTextArea ta = new JTextArea(10, 20);
add(new JScrollPane(ta));
((AbstractDocument) ta.getDocument()).setDocumentFilter(new DocumentFilter() {
protected String getLastLineOfText(Document document) throws BadLocationException {
// Find the last line of text...
Element rootElem = document.getDefaultRootElement();
int numLines = rootElem.getElementCount();
Element lineElem = rootElem.getElement(numLines - 1);
int lineStart = lineElem.getStartOffset();
int lineEnd = lineElem.getEndOffset();
String lineText = document.getText(lineStart, lineEnd - lineStart);
return lineText;
}
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
if (text.length() > 1) {
String lastLineOfText = getLastLineOfText(fb.getDocument());
if (!lastLineOfText.startsWith("<p>")) {
if (!text.startsWith("<p>")) {
text = "<p>" + text;
}
}
// Replace any line breaks with a new line
String[] lines = text.split("\n");
if (lines.length > 0) {
StringJoiner sj = new StringJoiner("<br>\n");
for (String line : lines) {
sj.add(line);
}
text = sj.toString();
}
if (!text.endsWith("</p>")) {
text += "</p>";
}
super.replace(fb, offset, length, text, attrs);
} else {
String postInsert = null;
if (text.endsWith("\n")) {
// Find the last line of text...
String lastLineOfText = getLastLineOfText(fb.getDocument());
lastLineOfText = lastLineOfText.substring(0, lastLineOfText.length() - 1);
postInsert = "<p>";
if (!lastLineOfText.endsWith("</p>")) {
super.replace(fb, offset, 0, "</p>", attrs);
offset += 4;
}
}
super.replace(fb, offset, length, text, attrs);
if (postInsert != null) {
offset += text.length();
super.replace(fb, offset, 0, "<p>", attrs);
}
}
}
});
}
}
}发布于 2015-09-07 07:50:21
也许你需要这样的东西:
String textToHtml="something like this";
textToHtml = "<p>".concat(textToHtml).concat("</p>").concat("<br>");https://stackoverflow.com/questions/32433508
复制相似问题