[問題] 如何取得JTextPane的內容?

看板java作者 (......)時間17年前 (2008/04/20 23:47), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串1/1
我想要寫一個網路聊天室 想要類似MSN的功能 就是可以修改設定字型顏色大小等等的功能 所以我用了一個JEditorPane來當文字編輯區域 使用者修改完整個樣式之後 我想把JEditorPane的內容(用HTML的tag表示) 放到一個String後 傳到遠端server 下面是我測試的程式碼 JEditorPane editPane=new JEditorPane(); editorPane.setContentType("text/html"); 然後用SimpleAttributeSet來設定文字樣式 SimpleAttributeSet attr=new SimpleAttributeSet(); StyleConstants.setBold(attr, true); StyleConstants.setForeground(attr, Color.RED); 最後將文字放入JEditorPane中 doc=(HTMLDocument)displayPane.getDocument(); doc.insertString(doc.getLength(), "hello\n" , attr); 接著我在螢幕上可以看到JEditorPane中確實已經加入了 一段紅色的hello 然後我把editPane.getText()的內容拿出來HTML的tag應該正確 但就是沒看到我的hello這幾個字,tag中間是空的 <html> <head> </head> <body> <p style="margin-top: 0"> <b><font color="#ff0000"><p> </font></b> </p> </body> </html> 如果我不用attr來設定樣式 doc.insertString(doc.getLength(), "hello\n" , null); 則editPane.getText()內容是正確的 <html> <head> </head> <body> <p style="margin-top: 0"> hello </p> </body> </html> 請問我哪裡弄錯了嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.94.236 ※ 編輯: KOCN 來自: 140.113.94.236 (04/20 23:47) ※ 編輯: KOCN 來自: 140.113.94.236 (04/20 23:48)

04/21 02:38, , 1F
高等java啊 XDDD
04/21 02:38, 1F

04/23 12:20, , 2F
昨天剛Demo完XD
04/23 12:20, 2F
文章代碼(AID): #182sMPtH (java)