但这个文本框里的值是在显示了这个html文件后才输进去的,不是本来就有的,就是说:在JEditorPane里显示了html文件后,用户在一个文本框里填写了东西,我可以得到他填写的东西吗?/

解决方案 »

  1.   

    如果可以,以后就不用做界面了。
    一个 JEditorPane + HTML 然后 …… 哈哈
      

  2.   

    HTMLDocument doc = (HTMLDocument)pane.getDocument();
    HTMLDocument中有如下方法:
    public Element getElement(String id);
    自己看看文档吧!哈哈!
      

  3.   

    pengji(彭乃超) 
    有意思
      

  4.   

    to:lucumu(沸腾的咖啡) 什么意思!?
      

  5.   

    to:pengji(彭乃超),谢谢你的回答,你的方法我之前已经试过了,像<input id="a">的html我最多只能get到input这个字符窜,而input框里的值我怎么也get不到,你能告诉我到底能不能get到吗?你试过吗?
      

  6.   

    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));
      }
    }
      

  7.   

    <!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>
      

  8.   

    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);
      }
    }这个应该可以了!