我尝试从数据库中检索数据并将其填充到Jlist中。之后,当我单击Jlist时,数据将填充到3个文本字段。其中两个将以html文件的形式填充到Jeditorpane作为组合。我尝试了以下方法,但它会产生错误。
String meaning1 = txtMeanings.getText();
String source1 = txtSources.getText();
String htmlText = "<html>"
+ "<body>"
+ "<div class='content'>{meaning1}</div>"
+ "<div class='footer'>{source1}</div>"
+"</body>"
+ "</html>";
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(10, 40, 529, 387);
panel_2.add(scrollPane_1);
HTMLEditorKit hed = new HTMLEditorKit();
StyleSheet ss = hed.getStyleSheet();
ss.addRule("BODY {...}");
ss.addRule("h1{...}");
ss.addRule("p{...}");
ss.addRule("blockquote{...}");
ss.addRule("#title{...}");
ss.addRule("hr{...}");
ss.addRule("#content{ ...}");
ss.addRule("#footer{...}");
Document doc = hed.createDefaultDocument();
editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setEditorKit(hed);
editorPane.setDocument(doc);
editorPane.setEditable(false);
scrollPane_1.setViewportView(editorPane);
ListboxEntry.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
try
{
String query ="select EntryLists, Meanings, Sources from Entry where EntryLists like ? ";
PreparedStatement pst=Connection.prepareStatement(query);
pst.setString(1,(String)ListboxEntry.getSelectedValue());
ResultSet rs=pst.executeQuery();
while(rs.next())
{
txtEntry.setText(rs.getString("EntryLists"));
txtMeanings.setText(rs.getString("Meanings"));
txtSources.setText(rs.getString("Sources"));
}
pst.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();}
editorPane.setText(htmlText);
}
});
我该怎么办?有可能直接将两个数据(含义和来源)填充到Jeditorpane而不在两个文本字段中首先填充它吗?在VB.Net中,我简单地通过这个简单的代码来解决问题:
WebBrowser1.DocumentText = String.Format("<html><head><style><!--body{{....}}--></style></head></head><body><div id=content>{0}</div><div id=footer><b>Notes:<br></b>{1} </div></body></html>", txtMeanings.Text, txtSources.Text)
如何用Java做到这一点?
答案 0 :(得分:0)
我通过更改为以下代码来解决自己的问题:
ListboxEntry.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
try
{
String query ="select EntryLists, Meanings, Sources from Entry where EntryLists like ? ";
PreparedStatement pst=Connection.prepareStatement(query);
pst.setString(1,(String)ListboxEntry.getSelectedValue());
ResultSet rs=pst.executeQuery();
while(rs.next())
{
txtEntry.setText(rs.getString("EntryLists"));
txtMeanings.setText(rs.getString("Meanings"));
txtSources.setText(rs.getString("Sources"));
}
pst.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();}
String htmlText = "<html>"
+ "<body>"
+ "<div class='content'>"+ txtMeanings.getText() +"</div>"
+ "<div class='footer'>"+ txtSources.getText() +"</div>"
+"</body>"
+ "</html>";
editorPane.setText(htmlText);
}
});
但是,如果有人想改进它,欢迎提出建议。