📄 htmltoolbar.java
字号:
package com.cwq.htmltoolbar;
import java.awt.Color;
import java.awt.Font;
import java.awt.Label;
import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.LineBorder;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTMLEditorKit;
public class HtmlToolBar extends JPanel {
private static final long serialVersionUID = 1L;
static String imagepath = "";
public static final Color color1 = new Color(225, 212, 192);
public static final Color color2 = new Color(238, 238, 238);
public static final Font font1 = new Font("Dialog", Font.PLAIN, 12);
public static final LineBorder border1 = new LineBorder(new Color(204, 204, 204));
public HtmlToolBar(String imagepath) {
super();
this.imagepath = imagepath;
initialize();
}
public String getHtml() {
return this.getJtp_html().getText();
}
public String getTitle() {
return this.getJtf_title().getText();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setBackground(Color.white);
this.setBorder(null);
this.setLayout(null);
this.setBounds(new Rectangle(0, 0, 600, 282));
final Label jl_normal = new Label();
final Label jl_normal1 = new Label();
final Label jl_html = new Label();
final Label jl_html1 = new Label();
jl_normal.setText("撰写");
jl_normal.setBounds(new Rectangle(502, 0, 73, 37));
jl_normal.setVisible(false);
jl_normal.setBackground(color1);
jl_normal.setAlignment(Label.CENTER);
jl_html.setText("HTML");
//jl_html.setVisible(false);
jl_html.setBounds(new Rectangle(428, 0, 73, 37));
jl_html.setAlignment(Label.CENTER);
jl_html.setBackground(color1);
jl_normal1.setText("撰写");
jl_normal1.setBounds(new Rectangle(507, 2, 60, 29));
jl_normal1.setForeground(Color.blue);
jl_normal1.setBackground(color2);
//jl_normal1.setVisible(false);
jl_normal1.setAlignment(Label.CENTER);
jl_normal1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jl_normal1.setVisible(false);
jl_html.setVisible(false);
jl_html1.setVisible(true);
jl_normal.setVisible(true);
getJPanel1().setVisible(false);
getJPanel2().setVisible(true);
String text = getJtp_html().getText();//.replaceAll("\r\n", "<br>");
getJtp_html().setEditorKit(sek);
getJtp_html().setContentType("text/html");
getJtp_html().setText(text);
//getJScrollPane().setViewportView(getJtp_html());
getJtp_html().requestFocus();
}
});
jl_html1.setText("HTML");
jl_html1.setBounds(new Rectangle(430, 2, 66, 29));
jl_html1.setAlignment(Label.CENTER);
jl_html1.setBackground(color2);
jl_html1.setForeground(Color.blue);
jl_html1.setVisible(false);
jl_html1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jl_html1.setVisible(false);
jl_normal.setVisible(false);
jl_normal1.setVisible(true);
jl_html.setVisible(true);
getJPanel2().setVisible(false);
getJPanel1().setVisible(true);
String text = getJtp_html().getText().trim();
int i1 = text.indexOf("<body>") + 7;
int i2 = text.lastIndexOf("</body>") - 1;
text = text.substring(i1, i2);
getJtp_html().setEditorKit(sek1);
getJtp_html().setContentType("text/plain");
getJtp_html().setText(text);
getJtp_html().requestFocus();
}
});
final JLabel title = new JLabel("标题");
title.setBounds(6, 6, 42, 23);
this.add(jl_html, null);
this.add(jl_html1, null);
this.add(jl_normal1, null);
this.add(jl_normal, null);
this.add(title, null);
this.add(getJtf_title(), null);
this.add(getJPanel(), null);
}
final Style style = getJtp_html().addStyle("div", null);
JPanel jPanel = null;
JPanel jPanel1 = null;
JPanel jPanel2 = null;
JTextField jtf_title = null;
JTextPane jtp_html = null;
StyledEditorKit sek1 = new StyledEditorKit();
HTMLEditorKit sek = new HTMLEditorKit();
void insertTag(String tagStart, String tagEnd) {
int s = getJtp_html().getSelectionStart();
int e = getJtp_html().getSelectionEnd();
try {
getJtp_html().getDocument().insertString(e, tagEnd, null);
getJtp_html().getDocument().insertString(s, tagStart, null);
//int p = s + tagStart.length();
int p = getJtp_html().getSelectionStart();
getJtp_html().select(p, p);
} catch(Exception ex) {
ex.printStackTrace();
}
}
void insertStyle(MutableAttributeSet tmpStyle) {
//StyleConstants.setAlignment(tmpStyle, StyleConstants.ALIGN_LEFT);
try { getJtp_html().setParagraphAttributes(tmpStyle, false); }
catch(Exception ex) {
getJtp_html().setText(getJtp_html().getText());
}
}
JTextPane getJtp_html() {
if (jtp_html == null) {
jtp_html = new JTextPane();
jtp_html.setContentType("text/plain");
jtp_html.setBorder(new LineBorder(Color.gray));
jtp_html.setEditorKit(new StyledEditorKit());
//HTMLEditorKit hek = new HTMLEditorKit();
//jtp_html.setEditorKit(hek);
}
return jtp_html;
}
JTextField getJtf_title() {
if(jtf_title == null) {
jtf_title = new JTextField();
jtf_title.setBounds(48, 4, 300, 23);
}
return jtf_title;
}
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setBounds(new Rectangle(0, 32, 600, 250));
jPanel.setBackground(color1);
jPanel.setBorder(border1);
jPanel.add(getJPanel1(), null);
jPanel.add(getJPanel2(), null);
JScrollPane jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(4, 36, 592, 210));
jScrollPane.setViewportView(getJtp_html());
jScrollPane.setBorder(null);
jPanel.add(jScrollPane, null);
}
return jPanel;
}
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new HtmlToolbar1(this);
}
return jPanel1;
}
private JPanel getJPanel2() {
if (jPanel2 == null) {
jPanel2 = new HtmlToolbar2(this);
}
return jPanel2;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -