package cn.hncu.aa;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.MenuItem;
import java.awt.ScrollPane;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
public class TextCopy extends JFrame implements ActionListener, MouseListener {
JTextArea text;
JComboBox comboboxname,comboboxsize;
JCheckBox bold,xieti;//复选框,后面用到;
JPopupMenu popm;
JRadioButton radio[];
Color color;
public TextCopy(String str){
setTitle(str);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension dim=getToolkit().getScreenSize();//基本操作设置
this.setBounds(dim.width/4, dim.height/4,dim.width/2,dim.height/2);
text=new JTextArea("Welcome..");//文本区域编辑
text.setForeground(Color.red);
text.setFont(new Font("ux", Font.ITALIC, 10));
text.addMouseListener(this);//快捷菜单需要;
this.getContentPane().add(new JScrollPane(text));//添加带有文本滑轮的文本
JToolBar toolbar=new JToolBar();
this.getContentPane().add(toolbar,"North");
//复合框的达建;
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontname=ge.getAvailableFontFamilyNames();//将字体放到fontname中去;
comboboxname =new JComboBox(fontname);
//对复合框的名字设置
String size[]={"20" ,"30", "40", "50", "60"};
comboboxsize=new JComboBox(size);//对复合狂的大小设置
comboboxsize.setEditable(true);//设置 可编辑;
comboboxname.addActionListener(this);//监听
comboboxsize.addActionListener(this);//监听
toolbar.add(comboboxname);//添加到工具条
toolbar.add(comboboxsize);
bold =new JCheckBox("粗体");
xieti=new JCheckBox("斜体");
bold.addActionListener(this);
xieti.addActionListener(this);
toolbar.add(bold);
toolbar.add(xieti);
String color[] ={"红","绿","蓝"};
JRadioButton radio[]=new JRadioButton[color.length];
ButtonGroup gp=new ButtonGroup();
for(int i=0;i<radio.length;i++){
radio[i]=new JRadioButton(color[i]);
radio[i].addActionListener(this);
gp.add(radio[i]);
toolbar.add(radio[i]);
}
addMymenu();//把函数写到下面,为了代码简洁;
this.setVisible(true);
}
public static void main(String[] args) {
new TextCopy("文本编辑");
}
void addMymenu(){
JMenuBar menubar=new JMenuBar();
this.setJMenuBar(menubar);//把菜单栏添加到框架上;
String menu[]={"文件", "编辑", "帮助"};
JMenu men[]=new JMenu[menu.length];
for(int i=0;i<men.length;i++){
men[i]=new JMenu(menu[i]);
menubar.add(men[i]);//菜单项完成;
}//文件下面菜单;
men[0].add(new JMenuItem("打开"));
men[0].add(new JMenuItem("保存"));
men[0].addSeparator();//中间用横线隔开;
JMenuItem exit1=new JMenuItem("退出");
exit1.setActionCommand("exit");
men[0].add(exit1);
exit1.addActionListener(this);//文件 菜单完成;
JMenu menustyle = new JMenu("字形");
menustyle.add(new JCheckBox("斜体"));
menustyle.add(new JCheckBox("粗体"));
men[1].add(menustyle);//添加到编辑;
JMenu mencolor =new JMenu("颜色");
ButtonGroup bt=new ButtonGroup();
String colorstr[]={ "红", "绿", "蓝"};
JRadioButton jrcolo[]=new JRadioButton[colorstr.length];
for(int i=0;i<jrcolo.length;i++){
jrcolo[i]=new JRadioButton(colorstr[i]);
bt.add(jrcolo[i]);
mencolor.add(jrcolo[i]);
men[1].add(mencolor);
}
//开始写快捷菜单;
popm=new JPopupMenu();
String menuitem[]={ "剪切", "复制", "粘贴"};
JMenuItem popitem[]=new JMenuItem[menuitem.length];
for(int i=0;i<popitem.length;i++){
popitem[i]=new JMenuItem(menuitem[i]);
popm.add(popitem[i]);
}
text.add(popm);//添加快捷键完成;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("exit")){
System.exit(0);
}
if(e.getSource() instanceof JComboBox||
e.getSource() instanceof JCheckBox){
try{
String fontname =(String) comboboxname.getSelectedItem();
int size=Integer.parseInt((String) comboboxsize.getSelectedItem());
if(size<4||size>123){
throw new Exception("SizeException");
}
Font font =text.getFont();
int style =font.getStyle();
if(e.getSource() == bold){
style= style^1;
}
if(e.getSource()==xieti){
style=style^2;
}
text.setFont(new Font(fontname, style, size));
}
catch(NumberFormatException e1){
JOptionPane.showMessageDialog(this, "\""+(String)comboboxsize.getSelectedItem()+"\"不能转换成整数,请重新输入!");
}catch(Exception e2){
if(e2.getMessage().equalsIgnoreCase("SizeException")){
JOptionPane.showMessageDialog(this, "字体不适合");
}
}
}
if(e.getSource() instanceof JRadioButton){//颜色
if( e.getSource()== radio[0]){
color=new Color(255, 0, 0);
}
if( e.getSource()== radio[1]){
color=new Color(0, 255, 0);
}
if( e.getSource()== radio[2]){
color=new Color(0, 0, 255);
}
text.setForeground(color);
}
}
public void mouseClicked(MouseEvent e) {
if(e.getModifiers()==MouseEvent.BUTTON3_MASK);{
popm.show(text,e.getX(),e.getY());
}
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.MenuItem;
import java.awt.ScrollPane;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
public class TextCopy extends JFrame implements ActionListener, MouseListener {
JTextArea text;
JComboBox comboboxname,comboboxsize;
JCheckBox bold,xieti;//复选框,后面用到;
JPopupMenu popm;
JRadioButton radio[];
Color color;
public TextCopy(String str){
setTitle(str);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension dim=getToolkit().getScreenSize();//基本操作设置
this.setBounds(dim.width/4, dim.height/4,dim.width/2,dim.height/2);
text=new JTextArea("Welcome..");//文本区域编辑
text.setForeground(Color.red);
text.setFont(new Font("ux", Font.ITALIC, 10));
text.addMouseListener(this);//快捷菜单需要;
this.getContentPane().add(new JScrollPane(text));//添加带有文本滑轮的文本
JToolBar toolbar=new JToolBar();
this.getContentPane().add(toolbar,"North");
//复合框的达建;
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontname=ge.getAvailableFontFamilyNames();//将字体放到fontname中去;
comboboxname =new JComboBox(fontname);
//对复合框的名字设置
String size[]={"20" ,"30", "40", "50", "60"};
comboboxsize=new JComboBox(size);//对复合狂的大小设置
comboboxsize.setEditable(true);//设置 可编辑;
comboboxname.addActionListener(this);//监听
comboboxsize.addActionListener(this);//监听
toolbar.add(comboboxname);//添加到工具条
toolbar.add(comboboxsize);
bold =new JCheckBox("粗体");
xieti=new JCheckBox("斜体");
bold.addActionListener(this);
xieti.addActionListener(this);
toolbar.add(bold);
toolbar.add(xieti);
String color[] ={"红","绿","蓝"};
JRadioButton radio[]=new JRadioButton[color.length];
ButtonGroup gp=new ButtonGroup();
for(int i=0;i<radio.length;i++){
radio[i]=new JRadioButton(color[i]);
radio[i].addActionListener(this);
gp.add(radio[i]);
toolbar.add(radio[i]);
}
addMymenu();//把函数写到下面,为了代码简洁;
this.setVisible(true);
}
public static void main(String[] args) {
new TextCopy("文本编辑");
}
void addMymenu(){
JMenuBar menubar=new JMenuBar();
this.setJMenuBar(menubar);//把菜单栏添加到框架上;
String menu[]={"文件", "编辑", "帮助"};
JMenu men[]=new JMenu[menu.length];
for(int i=0;i<men.length;i++){
men[i]=new JMenu(menu[i]);
menubar.add(men[i]);//菜单项完成;
}//文件下面菜单;
men[0].add(new JMenuItem("打开"));
men[0].add(new JMenuItem("保存"));
men[0].addSeparator();//中间用横线隔开;
JMenuItem exit1=new JMenuItem("退出");
exit1.setActionCommand("exit");
men[0].add(exit1);
exit1.addActionListener(this);//文件 菜单完成;
JMenu menustyle = new JMenu("字形");
menustyle.add(new JCheckBox("斜体"));
menustyle.add(new JCheckBox("粗体"));
men[1].add(menustyle);//添加到编辑;
JMenu mencolor =new JMenu("颜色");
ButtonGroup bt=new ButtonGroup();
String colorstr[]={ "红", "绿", "蓝"};
JRadioButton jrcolo[]=new JRadioButton[colorstr.length];
for(int i=0;i<jrcolo.length;i++){
jrcolo[i]=new JRadioButton(colorstr[i]);
bt.add(jrcolo[i]);
mencolor.add(jrcolo[i]);
men[1].add(mencolor);
}
//开始写快捷菜单;
popm=new JPopupMenu();
String menuitem[]={ "剪切", "复制", "粘贴"};
JMenuItem popitem[]=new JMenuItem[menuitem.length];
for(int i=0;i<popitem.length;i++){
popitem[i]=new JMenuItem(menuitem[i]);
popm.add(popitem[i]);
}
text.add(popm);//添加快捷键完成;
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("exit")){
System.exit(0);
}
if(e.getSource() instanceof JComboBox||
e.getSource() instanceof JCheckBox){
try{
String fontname =(String) comboboxname.getSelectedItem();
int size=Integer.parseInt((String) comboboxsize.getSelectedItem());
if(size<4||size>123){
throw new Exception("SizeException");
}
Font font =text.getFont();
int style =font.getStyle();
if(e.getSource() == bold){
style= style^1;
}
if(e.getSource()==xieti){
style=style^2;
}
text.setFont(new Font(fontname, style, size));
}
catch(NumberFormatException e1){
JOptionPane.showMessageDialog(this, "\""+(String)comboboxsize.getSelectedItem()+"\"不能转换成整数,请重新输入!");
}catch(Exception e2){
if(e2.getMessage().equalsIgnoreCase("SizeException")){
JOptionPane.showMessageDialog(this, "字体不适合");
}
}
}
if(e.getSource() instanceof JRadioButton){//颜色
if( e.getSource()== radio[0]){
color=new Color(255, 0, 0);
}
if( e.getSource()== radio[1]){
color=new Color(0, 255, 0);
}
if( e.getSource()== radio[2]){
color=new Color(0, 0, 255);
}
text.setForeground(color);
}
}
public void mouseClicked(MouseEvent e) {
if(e.getModifiers()==MouseEvent.BUTTON3_MASK);{
popm.show(text,e.getX(),e.getY());
}
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}