📄 htmltoolbar2.java
字号:
package com.cwq.batchmail.htmltoolbar;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.File;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
public class HtmlToolbar2 extends JPanel {
private static final long serialVersionUID = 1L;
private HtmlToolBar app;
/**
* This is the default constructor
*/
public HtmlToolbar2(HtmlToolBar app) {
super();
this.app = app;
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 200);
this.setLayout(null);
this.setBounds(new Rectangle(12, 6, 625, 22));
this.setBorder(null);
this.setBackground(app.color1);
this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
String[] s = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
String[] s1 = new String[s.length + 1];
s1[0] = "";
System.arraycopy(s, 0, s1, 1, s.length);
JComboBox jcb = new JComboBox(s1);
jcb.setBounds(0, 1, 100, 26);
jcb.setBackground(Color.white);
jcb.setBorder(null);
jcb.setFont(app.font1);
jcb.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent e) {
if(e.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
String s = e.getItem().toString();
if(s != null && s.trim().length() > 0) {
SimpleAttributeSet attrset = new SimpleAttributeSet();
StyleConstants.setFontFamily(attrset, s);
app.getJtp_html().setCharacterAttributes(attrset, false);
}
}
}
});
this.add(jcb);
// font size
final JPopupMenu jpm = new JPopupMenu();
jpm.setBackground(app.color1);
jpm.setBorder(app.border1);
JLabel jla = new ImageButton("tt.gif", null, "tt_1.gif") {
public void mouseClicked(MouseEvent arg0) {
jpm.show(this, 1, this.getHeight() + 1);
}
};
class FontSizeItem extends JMenuItem implements ActionListener {
private static final long serialVersionUID = 1L;
int fontSize;
FontSizeItem(final String label, final int size) {
super(label);
this.fontSize = size;
super.setBackground(app.color1);
super.setFont(new Font("Dialog", Font.BOLD, size));
super.addActionListener(this);
}
// font size Ϊ 7
public void actionPerformed(ActionEvent e) {
int s = (fontSize==12 ? 13 : (fontSize==10 ? 9 : (fontSize==11 ? 12 : (fontSize==13 ? 16 : (fontSize==14 ? 19 : 13)))));
SimpleAttributeSet attrset = new SimpleAttributeSet();
StyleConstants.setFontSize(attrset, s);
app.getJtp_html().setCharacterAttributes(attrset, false);
}
};
jpm.add(new FontSizeItem("Smallest", 10));
jpm.add(new FontSizeItem("Small", 11));
jpm.add(new FontSizeItem("Normal", 12));
jpm.add(new FontSizeItem("Large", 13));
jpm.add(new FontSizeItem("Largest", 14));
jla.add(jpm);
jla.setLocation(106, 1);
this.add(jla);
JLabel jlb = new ImageLabel("split.gif");
jlb.setLocation(jla.getLocation().x + jla.getWidth() - 2, 0);
this.add(jlb);
JLabel jl1 = new ImageButton("b.gif", null, "b_1.gif") {
public void mouseClicked(MouseEvent arg0) {
SimpleAttributeSet attrset = new SimpleAttributeSet();
StyleConstants.setBold(attrset, true);
app.getJtp_html().setCharacterAttributes(attrset, false);
}
};
jl1.setLocation(jlb.getLocation().x + jlb.getWidth() - 2, 1);
this.add(jl1);
JLabel jl2 = new ImageButton("i.gif", null, "i_1.gif") {
public void mouseClicked(MouseEvent arg0) {
SimpleAttributeSet attrset = new SimpleAttributeSet();
StyleConstants.setItalic(attrset, true);
app.getJtp_html().setCharacterAttributes(attrset, false);
}
};
jl2.setLocation(jl1.getLocation().x + jl2.getWidth() - 2, 1);
this.add(jl2);
JLabel jl3 = new ImageLabel("split.gif");
jl3.setLocation(jl2.getLocation().x + jl2.getWidth() - 2, 0);
this.add(jl3);
JLabel jlc = new ImageButton("t.gif", null, "t_1.gif") {
public void mouseClicked(MouseEvent arg0) {
JColorChooser jcc = new JColorChooser();
Color c = jcc.showDialog(app, "", Color.black);
if(c != null) {
SimpleAttributeSet attrset = new SimpleAttributeSet();
StyleConstants.setForeground(attrset, c);
app.getJtp_html().setCharacterAttributes(attrset, false);
}
}
};
jlc.setLocation(jl3.getLocation().x + jl3.getWidth() - 2, 1);
this.add(jlc);
JLabel jld = new ImageLabel("split.gif");
jld.setLocation(jlc.getLocation().x + jlc.getWidth() - 2, 0);
this.add(jld);
JLabel jl4 = new ImageButton("link.gif", null, "link_1.gif") {
public void mouseClicked(MouseEvent arg0) {
String link = javax.swing.JOptionPane.showInputDialog(app, "请输入链接的网址");
if(link == null || (link != null && link.trim().length() == 0))
return ;
HTMLEditorKit hek = (HTMLEditorKit) app.getJtp_html().getEditorKit();
try {
String t = app.getJtp_html().getSelectedText();
app.getJtp_html().getDocument().remove(app.getJtp_html().getCaretPosition(), t.length());
hek.insertHTML((HTMLDocument) app.getJtp_html().getDocument(), app.getJtp_html().getCaretPosition(), "<a href='".concat(link).concat("'>").concat(t).concat("</a>"), 0, 0, HTML.Tag.A);
} catch(Exception ex) {
ex.printStackTrace();
}
}
};
jl4.setLocation(jld.getLocation().x + jld.getWidth() - 2, 1);
this.add(jl4);
JLabel jl5 = new ImageLabel("split.gif");
jl5.setLocation(jl4.getLocation().x + jl4.getWidth() - 2, 0);
this.add(jl5);
JLabel jle = new ImageButton("left.gif", null, "left_1.gif") {
public void mouseClicked(MouseEvent arg0) {
StyleConstants.setAlignment(app.style, StyleConstants.ALIGN_LEFT);
app.insertStyle(app.style);
}
};
jle.setLocation(jl5.getLocation().x + jl5.getWidth() - 2, 1);
this.add(jle);
JLabel jlf = new ImageButton("center.gif", null, "center_1.gif") {
public void mouseClicked(MouseEvent arg0) {
StyleConstants.setAlignment(app.style, StyleConstants.ALIGN_CENTER);
app.insertStyle(app.style);
}
};
jlf.setLocation(jle.getLocation().x + jle.getWidth() - 2, 1);
this.add(jlf);
JLabel jlg = new ImageButton("right.gif", null, "right_1.gif") {
public void mouseClicked(MouseEvent arg0) {
StyleConstants.setAlignment(app.style, StyleConstants.ALIGN_RIGHT);
app.insertStyle(app.style);
}
};
jlg.setLocation(jlf.getLocation().x + jlf.getWidth() - 2, 1);
this.add(jlg);
JLabel jlh = new ImageButton("nor.gif", null, "nor_1.gif") {
public void mouseClicked(MouseEvent arg0) {
StyleConstants.setAlignment(app.style, StyleConstants.ALIGN_JUSTIFIED);
app.insertStyle(app.style);
}
};
jlh.setLocation(jlg.getLocation().x + jlg.getWidth() - 2, 1);
this.add(jlh);
JLabel jli = new ImageLabel("split.gif");
jli.setLocation(jlh.getLocation().x + jlh.getWidth() - 2, 0);
this.add(jli);
JLabel jlj = new ImageButton("123.gif", null, "123_1.gif") {
public void mouseClicked(MouseEvent arg0) {
// jTextPane����֧�� ol ��ǩ,�����style���˺�,��jTextPane�п�����4��html���� ol,����� ul
Style s1 = app.getJtp_html().getStyle("ol");
if(s1 == null)
s1 = app.getJtp_html().addStyle("ol", null);
Style s2 = app.getJtp_html().addStyle("li", s1);
app.insertStyle(s2);
}
};
jlj.setLocation(jli.getLocation().x + jli.getWidth() - 2, 1);
this.add(jlj);
JLabel jlk = new ImageButton("ddd.gif", null, "ddd_1.gif") {
public void mouseClicked(MouseEvent arg0) {
Style s1 = app.getJtp_html().getStyle("ul");
if(s1 == null)
s1 = app.getJtp_html().addStyle("ul", null);
Style s2 = app.getJtp_html().addStyle("li", s1);
app.insertStyle(s2);
}
};
jlk.setLocation(jlj.getLocation().x + jlj.getWidth() - 2, 1);
this.add(jlk);
JLabel jl6 = new ImageButton("ink.gif", null, "ink_1.gif") {
public void mouseClicked(MouseEvent arg0) {
Style s1 = app.getJtp_html().getStyle("blockquote");
if(s1 == null)
s1 = app.getJtp_html().addStyle("blockquote", null);
app.insertStyle(s1);
}
};
jl6.setLocation(jlk.getLocation().x + jlk.getWidth() - 2, 1);
this.add(jl6);
JLabel jl7 = new ImageLabel("split.gif");
jl7.setLocation(jl6.getLocation().x + jl6.getWidth() - 2, 0);
this.add(jl7);
JLabel jl8 = new ImageButton("abc.gif", null, "abc_1.gif") {
public void mouseClicked(MouseEvent arg0) {
//app.abcDocument();
}
};
jl8.setLocation(jl7.getLocation().x + jl7.getWidth() - 2, 1);
this.add(jl8);
JLabel jl9 = new ImageLabel("split.gif");
jl9.setLocation(jl8.getLocation().x + jl8.getWidth() - 2, 0);
this.add(jl9);
JLabel jl10 = new ImageButton("pic.gif", null, "pic_1.gif") {
public void mouseClicked(MouseEvent arg0) {
//app.getPictureUpload().setVisible(true, false);
String img = javax.swing.JOptionPane.showInputDialog(app, "请输入图片的网址");
if(img == null || (img != null && img.trim().length() == 0))
return ;
Style s1 = app.getJtp_html().addStyle("img src='" + img + "'", null);
try { app.getJtp_html().setParagraphAttributes(s1, false); }
catch(Exception ex) {
app.getJtp_html().setText(app.getJtp_html().getText());
}
}
};
jl10.setLocation(jl9.getLocation().x + jl9.getWidth() - 2, 1);
this.add(jl10);
JLabel jl11 = new ImageLabel("split.gif");
jl11.setLocation(jl10.getLocation().x + jl10.getWidth() - 2, 0);
this.add(jl11);
JLabel jll = new ImageButton("shua.gif", null, "shua_1.gif") {
public void mouseClicked(MouseEvent arg0) {
String t = app.getJtp_html().getSelectedText();
try { app.getJtp_html().getDocument().remove(app.getJtp_html().getSelectionStart(), t.length()); }
catch(Exception ex) {}
}
};
jll.setLocation(jl11.getLocation().x + jl11.getWidth() - 2, 1);
this.add(jll);
JLabel jlm = new ImageLabel("split.gif");
jlm.setLocation(jll.getLocation().x + jll.getWidth() - 2, 0);
this.add(jlm);
this.setVisible(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -