📄 myeditorpane.java
字号:
import javax.swing.*;
import javax.swing.text.Document;
import java.io.*;
import java.net.MalformedURLException;
import java.awt.*;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.text.SimpleDateFormat;
import java.util.*;
public class MyEditorPane extends JEditorPane{
private RandomAccessFile fileP;
private File htmlFile;
private String htmlFName,myNickName,talkerNickName;
private Document doc;
public MyEditorPane(String myNickName,String talkerNickName){
this.myNickName = myNickName;
this.talkerNickName = talkerNickName;
htmlFName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
htmlFile = new File("HTML/"+htmlFName+".html");
try{
fileP = new RandomAccessFile(htmlFile,"rw");
fileP.writeBytes("<html></html>");
}catch(Exception e)
{}
this.setSize(1000,800);
this.setEditable(false);
try {
this.setPage(htmlFile.toURL());
} catch (Exception e)
{}
}
public void appendText(String msg,MyFont f,boolean isMyself){
this.insertToFile("<img src=\"../GIF/"+
(isMyself?"32":"44")+".gif\""+"align = middle>"+
(isMyself?myNickName:talkerNickName)+" : "+
new SimpleDateFormat("HH:mm:ss").format(new Date())+"<br>");
if(f.style == Font.ITALIC){
msg = "<i>"+msg+"</i>";
}
else if(f.style == Font.BOLD){
msg = "<b>"+msg+"</b>";
}
else if(f.style == (Font.BOLD|Font.ITALIC)){
msg = "<i><b>"+msg+"</i></b>";
}
if(f.isUnderLine){
msg = "<u>"+msg+"</u>";
}
insertToFile("<font color = "+f.color+
" size= "+f.size+
" face= \""+f.fontName+"\">"+
msg+"</font><br>");
doc = this.getDocument();
doc.putProperty(Document.StreamDescriptionProperty, null);
try {
this.setPage(htmlFile.toURL());
} catch (Exception e)
{}
}
public void appendImage(String index,boolean isMyself){
//自己和对方的图标不一样
this.insertToFile("<img src=\"../GIF/"+
(isMyself?"32":"44")+".gif\""+"align = middle>"+
(isMyself?myNickName:talkerNickName)+" : "+
new SimpleDateFormat("HH:mm:ss").format(new Date())+"<br>");
this.insertToFile("<img src=\"../GIF/"+
index+".gif\"><br>");
doc = this.getDocument();
doc.putProperty(Document.StreamDescriptionProperty, null);
try {
this.setPage(htmlFile.toURL());
} catch (Exception e)
{}
}
private void insertToFile(String s){
try{
/**
* 找到</html>前面的Index
*/
fileP.seek(htmlFile.length()-7);
/**
* 覆盖</html>
*/
fileP.write(s.getBytes());
/**
* 重新写入</html>
*/
fileP.writeBytes("</html>");
}catch(Exception e)
{}
this.repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -