📄 styletoolbar.java
字号:
import javax.swing.*;
import java.awt.*;
import javax.swing.text.*;
import java.awt.event.*;
/**
* StyleToolBar.java
*
* 常用工具栏
* 字体工具栏
*
* @author 戚荣波
*
* @version 1.0
* @since J2SE 1.6
*/
public class StyleToolBar extends JToolBar
{
public static JLabel showLabel = null;
public StyleToolBar()
{
super("StyleToolBar");
setBackground(ShowWindow.myColor);
setBorderPainted(true);
setFloatable(false);
setRollover(false);
ImageIcon []ic = {
new ImageIcon("icon\\font.gif"),new ImageIcon("icon\\face.gif"),new ImageIcon("icon\\file.gif"),
new ImageIcon("icon\\picture.gif"),new ImageIcon("icon\\shock.gif"),new ImageIcon("icon\\music.gif"),
new ImageIcon("icon\\paneColor.gif")
};
JButton b_font = new SButton(ic[0]);
JButton b_face = new SButton(ic[1]);
JButton b_file = new SButton(ic[2]);
JButton b_picture = new SButton(ic[3]);
JButton b_shock = new SButton(ic[4]);
JButton b_music = new SButton(ic[5]);
JButton b_color = new SButton(ic[6]);
showLabel = new JLabel("");
add(b_font);
//add(b_face);
addSeparator();
add(b_file);
add(b_picture);
add(b_shock);
addSeparator();
add(b_music);
add(b_color);
addSeparator();
add(showLabel);
showLabel.setVisible(false);
b_font.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(SomethingStored.fontBar.isVisible() == true)
{
SomethingStored.fontBar.setVisible(false);
}
else
{
Rectangle r = getBounds();
r.y = r.y - 22;
r.height = 25;
SomethingStored.fontBar.setBounds(r);
SomethingStored.fontBar.setVisible(true);
}
}
}
);//b_font
b_font.setToolTipText("Change your font and color");
b_file.addActionListener(new fileListener());
b_file.setToolTipText("Send a file to your friend");
b_picture.addActionListener(new pictureListener());
b_picture.setToolTipText("Display a picture for your friend");
b_shock.addActionListener(new shockListener());
b_shock.setToolTipText("Send a \"nudge\" to your friend");
b_music.addActionListener(new musicListener());
b_music.setToolTipText("how about some music");
b_color.addActionListener(new colorListener());
b_color.setToolTipText("choose a color for your message window");
}
}
/**
* 字体工具栏
*
* @author 戚荣波
*
* @version 1.0
* @since J2SE 1.6
*/
class FontToolBar extends JToolBar
{
JComboBox fontName;
JComboBox fontSize;
SButton fontColor;
JCheckBox []fontStyle = new JCheckBox[3];
public FontToolBar()
{
super("font");
setBackground(ShowWindow.myColor);
setBorderPainted(false);
setFloatable(false);
setRollover(false);
setVisible(false);
String[] str_name = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
String[] str_Size = { "8","9","10","11","12", "14","16", "18","20", "22", "30", "40" };
String[] str_Style = { "斜体", "粗体"};
fontName = new JComboBox(str_name); // 字体名称
fontSize = new JComboBox(str_Size); // 字号
fontColor = new SButton(new ImageIcon("icon\\color.gif"));
fontName.setSelectedItem("宋体");
fontSize.setSelectedItem("12");
for( int i = 0;i < str_Style.length; i++)
{
fontStyle[i] = new JCheckBox(str_Style[i]);
}
class tbListener implements ItemListener,ActionListener
{
Font f = null;
String name = "宋体";
int size = 12;
int style = 0;
Color fc = Color.black;
public void itemStateChanged(ItemEvent e)
{
style = 0;
name = (String) fontName.getSelectedItem();
size = Integer.parseInt((String) fontSize.getSelectedItem());
if(fontStyle[0].isSelected())style = style|Font.ITALIC;
if(fontStyle[1].isSelected())style = style|Font.BOLD;
f = new Font(name,style,size);
SomethingStored.myAttributeSet = getAttributeSet(f,fc);
SomethingStored.tp_write.setFont(f);
SomethingStored.tp_write.updateUI();
}
public void actionPerformed(ActionEvent e)
{
if(f == null)f = SomethingStored.tp_write.getFont();
fc = JColorChooser.showDialog(ShowWindow.app,"Choose a color",SomethingStored.fontColor);
if(fc == null)fc = SomethingStored.fontColor;
SomethingStored.fontColor = fc;
SomethingStored.myAttributeSet = getAttributeSet(f,fc);
SomethingStored.tp_write.setForeground(fc);
SomethingStored.tp_write.updateUI();
}
}
add(fontName);
add(fontSize);
add(fontColor);
tbListener l = new tbListener();
for( int i = 0;i < str_Style.length; i++)
{
add(fontStyle[i]);
fontStyle[i].addItemListener(l);
}
fontColor.addActionListener(l);
fontName.setPrototypeDisplayValue("xxxxxxxxxx");
fontName.addItemListener(l);
fontSize.addItemListener(l);
}
/**
*通过Font和Color获得对应的SimpleAttributeSet
*
*@param f 字体
*@param c 文字颜色
*@return 对应的SimpleAttributeSet
*/
public SimpleAttributeSet getAttributeSet(Font f,Color c)
{
SimpleAttributeSet temp_att = new SimpleAttributeSet();
StyleConstants.setBold(temp_att,f.isBold());
StyleConstants.setItalic(temp_att,f.isItalic());
StyleConstants.setFontSize(temp_att,f.getSize());
StyleConstants.setForeground(temp_att,c);
StyleConstants.setFontFamily(temp_att,f.getFamily());
return temp_att;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -