package com.rgy.entity;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;
import com.rgy.Test.JColorChooserDemo;
@SuppressWarnings("serial")
public class TextEditBox extends JFrame {
// 添加属性
private JComboBox combox_name, combox_size;// 字体、字号组合框
private JButton button_larger,button_smaller,button_color;//字体变大变小和颜色选择器
private JCheckBox checkb_bold, checkb_italic;// 粗体、斜体复选框
private JPopupMenu popupmenu;
private JTextArea ta = new JTextArea();
private JScrollPane sp = new JScrollPane(ta);
public TextEditBox(String str) {
super(str);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Dimension dim = getToolkit().getScreenSize(); // 获得屏幕分辨率
this.setBounds(dim.width / 4, dim.height / 4, 700, 480);
JToolBar toolbar = new JToolBar(); // 创建工具栏
this.add(toolbar, BorderLayout.NORTH); // 工具栏添加到窗格北部
this.add(sp);
ta.setLineWrap(true);// 换行
//字体
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontsName = ge.getAvailableFontFamilyNames(); // 获得系统字体
combox_name = new JComboBox(fontsName);
toolbar.add(combox_name);
combox_name.addActionListener(new ActionListener() {// 字号
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize();
ta.setFont(new Font(fontname, style, size));
}
});
/字号
String sizestr[] = { "20", "30", "40", "50", "60", "70" ,"80","90","100"};
combox_size = new JComboBox(sizestr);
combox_size.setEditable(true);
toolbar.add(combox_size);
combox_size.addActionListener(new ActionListener() {// 字号
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//获得字体名
int size = Integer.parseInt((String)combox_size.getSelectedItem());
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
ta.setFont(new Font(fontname, style, size));
}
});
字号加减按钮
button_larger=new JButton("A+");
toolbar.add(button_larger);
button_larger.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize()+5;
ta.setFont(new Font(fontname, style, size));
}
});
button_smaller=new JButton("A-");
toolbar.add(button_smaller);
button_smaller.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize()-5;
ta.setFont(new Font(fontname, style, size));
}
});
/
/粗体和斜体
checkb_bold = new JCheckBox("粗体"); //字形复选框
toolbar.add(checkb_bold);
checkb_bold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize();
style = style ^ 1;
ta.setFont(new Font(fontname, style, size));
}
});
checkb_italic = new JCheckBox("斜体");
toolbar.add(checkb_italic);
checkb_italic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String fontname = (String)combox_name.getSelectedItem();//获得字体名
Font font = ta.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
int size = font.getSize();
style = style ^ 2;
ta.setFont(new Font(fontname, style, size));
}
});
JRadioButton radiob_color[];
String colorstr[]={"红","绿","蓝"};
ButtonGroup bgroup_color = new ButtonGroup(); //按钮组
radiob_color = new JRadioButton[colorstr.length]; //颜色单选按钮数组
for (int i=0; i