已经把html格式的文件显示在JEditorPane,但怎样可以得到这个html文件里面的一个文本框的值 但这个文本框里的值是在显示了这个html文件后才输进去的,不是本来就有的,就是说:在JEditorPane里显示了html文件后,用户在一个文本框里填写了东西,我可以得到他填写的东西吗?/ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果可以,以后就不用做界面了。一个 JEditorPane + HTML 然后 …… 哈哈 HTMLDocument doc = (HTMLDocument)pane.getDocument();HTMLDocument中有如下方法:public Element getElement(String id);自己看看文档吧!哈哈! pengji(彭乃超) 有意思 to:lucumu(沸腾的咖啡) 什么意思!? to:pengji(彭乃超),谢谢你的回答,你的方法我之前已经试过了,像<input id="a">的html我最多只能get到input这个字符窜,而input框里的值我怎么也get不到,你能告诉我到底能不能get到吗?你试过吗? package swingtest;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.text.*;import javax.swing.text.html.*;public class TestEditPanel extends JFrame{ private JEditorPane jEditorPane1 = new JEditorPane(); private JButton jButton1 = new JButton(); private JTextField jTextField1 = new JTextField(); public TestEditPanel() throws HeadlessException { try { jbInit();// jEditorPane1.addInputMethodListener(new InputMethodListener(){// public void caretPositionChanged(InputMethodEvent event){};// public void inputMethodTextChanged(InputMethodEvent event){// System.out.println(event);// }// }); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws HeadlessException { TestEditPanel testEditPanel = new TestEditPanel(); testEditPanel.setSize(400,400); testEditPanel.show(); } private void jbInit() throws Exception { jEditorPane1.setBounds(new Rectangle(34, 8, 324, 233)); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.getContentPane().setLayout(null); jButton1.setBounds(new Rectangle(35, 266, 151, 24)); jButton1.setText("GET VALUE"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jTextField1.setBounds(new Rectangle(198, 266, 161, 22)); this.getContentPane().add(jEditorPane1, null); this.getContentPane().add(jButton1, null); this.getContentPane().add(jTextField1, null); jEditorPane1.setPage(this.getClass().getResource("a.html")); } void jButton1_actionPerformed(ActionEvent e) { HTMLDocument doc = (HTMLDocument)jEditorPane1.getDocument(); //!!!!!!!!!!用户输入数据怎么可能获得不了。 Element ele = doc.getElement("id2"); System.out.println(ele.getAttributes().getAttribute(HTML.Attribute.VALUE)); }} <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE>test html</TITLE><META NAME="Generator" CONTENT="EditPlus"><META NAME="Author" CONTENT=""><META NAME="Keywords" CONTENT=""><META NAME="Description" CONTENT=""></HEAD><BODY><P id="id1">It is a test Html!</P><input type="text" name="aaa" value="bbb" id="id2"></BODY></HTML> package swingtest;import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.event.*;import java.util.*;import javax.swing.text.*;import javax.swing.text.html.*;public class TestEditPanel extends JFrame{ private JTextPane jTextPane1 = new JTextPane(); private JButton jButton1 = new JButton(); private JTextField jTextField1 = new JTextField(); public TestEditPanel() throws HeadlessException { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws HeadlessException { TestEditPanel testEditPanel = new TestEditPanel(); testEditPanel.setSize(400,400); testEditPanel.show(); } private void jbInit() throws Exception { jTextPane1.setBounds(new Rectangle(34, 8, 324, 233)); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.getContentPane().setLayout(null); jButton1.setBounds(new Rectangle(35, 266, 151, 24)); jButton1.setText("GET VALUE"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jTextField1.setBounds(new Rectangle(198, 266, 161, 22)); this.getContentPane().add(jTextPane1, null); this.getContentPane().add(jButton1, null); this.getContentPane().add(jTextField1, null); jTextPane1.setPage(this.getClass().getResource("a.html")); jTextPane1.setEditable(false); } void jButton1_actionPerformed(ActionEvent e) { //System.out.println(jEditorPane1.getText()); HTMLDocument doc = (HTMLDocument)jTextPane1.getDocument(); Element ele = doc.getElement("id2"); //System.out.println(ele.getAttributes().getAttribute(HTML.Attribute.VALUE)); //System.out.println(((PlainDocument)ele.getAttributes().getAttribute(StyleConstants.ModelAttribute))); AttributeSet atts = ele.getAttributes(); Object value = atts.getAttribute(HTML.Attribute.VALUE); PlainDocument model = (PlainDocument)atts.getAttribute(StyleConstants.ModelAttribute); model.addDocumentListener(new DocumentListener() { public void changedUpdate(DocumentEvent e) {System.out.println("A");} public void insertUpdate(DocumentEvent e) { try { String temp = e.getDocument().getText(0,e.getDocument().getLength()); System.out.println(temp); }catch(Exception e1) { e1.printStackTrace(); } } public void removeUpdate(DocumentEvent e) {System.out.println("C");} });// System.out.println(value);// System.out.println(model); }}这个应该可以了! java初学者编程 简单的“云系统”应该如何构架 如何linux下使用java获取word的批注 一个文件改名程序 JAVA技术群招人,希望有技术,想学习的同学来加入, [建议]删除一些显摆性质的垃圾贴子,或封其发贴人的帐号,或对此类垃圾不于置评,各位也来支持支持小弟的建议 用java能做个浏览器吗? 有一点C和C++的基础,非计算机专业出身!如何学Java java在文本框中输入中文数据不能加入oracle数据库,英文可以,是怎么回事??? 怎么样连接数据库阿!!!有没有好的教程或实例阿 有谁会用JDK中的native2ascii命令 高分求搜索引擎的网路搜索算法
一个 JEditorPane + HTML 然后 …… 哈哈
HTMLDocument中有如下方法:
public Element getElement(String id);
自己看看文档吧!哈哈!
有意思
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class TestEditPanel extends JFrame
{
private JEditorPane jEditorPane1 = new JEditorPane();
private JButton jButton1 = new JButton();
private JTextField jTextField1 = new JTextField(); public TestEditPanel() throws HeadlessException
{
try
{
jbInit();
// jEditorPane1.addInputMethodListener(new InputMethodListener(){
// public void caretPositionChanged(InputMethodEvent event){};
// public void inputMethodTextChanged(InputMethodEvent event){
// System.out.println(event);
// }
// });
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws HeadlessException
{
TestEditPanel testEditPanel = new TestEditPanel();
testEditPanel.setSize(400,400);
testEditPanel.show(); }
private void jbInit() throws Exception
{
jEditorPane1.setBounds(new Rectangle(34, 8, 324, 233));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
jButton1.setBounds(new Rectangle(35, 266, 151, 24));
jButton1.setText("GET VALUE");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
jTextField1.setBounds(new Rectangle(198, 266, 161, 22));
this.getContentPane().add(jEditorPane1, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jTextField1, null);
jEditorPane1.setPage(this.getClass().getResource("a.html"));
} void jButton1_actionPerformed(ActionEvent e)
{
HTMLDocument doc = (HTMLDocument)jEditorPane1.getDocument();
//!!!!!!!!!!用户输入数据怎么可能获得不了。
Element ele = doc.getElement("id2");
System.out.println(ele.getAttributes().getAttribute(HTML.Attribute.VALUE));
}
}
<HTML>
<HEAD>
<TITLE>test html</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD><BODY>
<P id="id1">
It is a test Html!
</P><input type="text" name="aaa" value="bbb" id="id2">
</BODY>
</HTML>
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class TestEditPanel extends JFrame
{
private JTextPane jTextPane1 = new JTextPane();
private JButton jButton1 = new JButton();
private JTextField jTextField1 = new JTextField(); public TestEditPanel() throws HeadlessException
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws HeadlessException
{
TestEditPanel testEditPanel = new TestEditPanel();
testEditPanel.setSize(400,400);
testEditPanel.show(); }
private void jbInit() throws Exception
{
jTextPane1.setBounds(new Rectangle(34, 8, 324, 233));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
jButton1.setBounds(new Rectangle(35, 266, 151, 24));
jButton1.setText("GET VALUE");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
jTextField1.setBounds(new Rectangle(198, 266, 161, 22));
this.getContentPane().add(jTextPane1, null);
this.getContentPane().add(jButton1, null);
this.getContentPane().add(jTextField1, null);
jTextPane1.setPage(this.getClass().getResource("a.html"));
jTextPane1.setEditable(false);
} void jButton1_actionPerformed(ActionEvent e)
{
//System.out.println(jEditorPane1.getText());
HTMLDocument doc = (HTMLDocument)jTextPane1.getDocument();
Element ele = doc.getElement("id2");
//System.out.println(ele.getAttributes().getAttribute(HTML.Attribute.VALUE));
//System.out.println(((PlainDocument)ele.getAttributes().getAttribute(StyleConstants.ModelAttribute)));
AttributeSet atts = ele.getAttributes();
Object value = atts.getAttribute(HTML.Attribute.VALUE);
PlainDocument model = (PlainDocument)atts.getAttribute(StyleConstants.ModelAttribute);
model.addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {System.out.println("A");}
public void insertUpdate(DocumentEvent e) {
try {
String temp = e.getDocument().getText(0,e.getDocument().getLength());
System.out.println(temp);
}catch(Exception e1) {
e1.printStackTrace();
} }
public void removeUpdate(DocumentEvent e) {System.out.println("C");}
});
// System.out.println(value);
// System.out.println(model);
}
}这个应该可以了!