1.java编译文本编辑器
java编译文本编辑器
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EditorJFrame extends JFrame implements ActionListener, ItemListener, MouseListener
{
private JTextField text_size; //字号文本行
private JCheckBox checkbox_bold, checkbox_italic; //粗体、斜体复选框
private JButton button_cut,编辑本编 button_copy, button_paste; //剪切、复制、器源fir vhd源码粘贴按钮
private JTextArea textarea; //文本区
private JPopupMenu popupmenu; //快捷菜单
private JDialog dialog; //出错提示对话框
private JLabel label_dialog; //对话框中的辑器标签
public EditorJFrame()
{
super("文本编辑器"); //默认BorderLayout布局
this.setSize(,);
this.setLocation(,);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //单击窗口关闭按钮时,结束程序运行
textarea = new JTextArea("TextArea");
textarea.addMouseListener(this); //为文本区注册鼠标事件监听器
this.add(textarea); //文本区添加到框架的源码vue修改element源码中部
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //面板为流布局,左对齐
this.add(panel,文本a文im即时客服源码"North"); //面板添加到框架的北部
text_size = new JTextField("",);
panel.add(text_size);
text_size.addActionListener(this); //注册文本行的单击事件监听器
checkbox_bold = new JCheckBox("粗体"); //复选框
panel.add(checkbox_bold);
checkbox_bold.addItemListener(this); //注册复选框的选择事件监听器
checkbox_italic = new JCheckBox("斜体");
panel.add(checkbox_italic);
checkbox_italic.addItemListener(this);
this.addmyMenu(); //调用自定义方法,添加菜单
this.setVisible(true);
}
private void addmyMenu() //添加主菜单、编辑本编快捷菜单、器源对话框
{
JMenuBar menubar = new JMenuBar(); //菜单栏
this.setJMenuBar(menubar); //框架上添加菜单栏
JMenu menu_file = new JMenu("文件"); //菜单
menubar.add(menu_file); //菜单栏中加入菜单
menu_file.add(new JMenuItem("打开")); //生成菜单项并加入到菜单
menu_file.add(new JMenuItem("保存"));
menu_file.addSeparator(); //加分隔线
JMenuItem menuitem_exit = new JMenuItem("退出");
menu_file.add(menuitem_exit);
menuitem_exit.addActionListener(this); //为菜单项注册单击事件监听器
JMenu menu_edit = new JMenu("编辑");
menubar.add(menu_edit);
JMenu menu_style = new JMenu("字形");
menu_style.add(new JCheckBoxMenuItem("粗体")); //复选菜单项
menu_style.add(new JCheckBoxMenuItem("斜体"));
menu_edit.add(menu_style); //菜单加入到菜单中成为二级菜单
JMenu menu_color = new JMenu("颜色");
menu_edit.add(menu_color);
ButtonGroup buttongroup = new ButtonGroup(); //按钮组
JRadioButtonMenuItem rbmi_red = new JRadioButtonMenuItem("红",辑器true); //单选菜单项
buttongroup.add(rbmi_red); //单选菜单项添加到按钮组
menu_color.add(rbmi_red); //单选菜单项添加到菜单
JRadioButtonMenuItem rbmi_green = new JRadioButtonMenuItem("绿",true);
buttongroup.add(rbmi_green);
menu_color.add(rbmi_green);
JRadioButtonMenuItem rbmi_blue = new JRadioButtonMenuItem("蓝",true);
buttongroup.add(rbmi_blue);
menu_color.add(rbmi_blue);
menubar.add(new JMenu("帮助"));
popupmenu = new JPopupMenu(); //弹出式菜单对象
JMenuItem menuitem_cut = new JMenuItem("剪切");
menuitem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));//设置快捷键Ctrl+X
popupmenu.add(menuitem_cut); //加入剪切菜单项
menuitem_cut.addActionListener(this);
JMenuItem menuitem_copy = new JMenuItem("复制");
menuitem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));//设置快捷键Ctrl+C
popupmenu.add(menuitem_copy);
menuitem_copy.addActionListener(this);
JMenuItem menuitem_paste = new JMenuItem("粘贴");
menuitem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));//设置快捷键Ctrl+V
popupmenu.add(menuitem_paste);
menuitem_paste.addActionListener(this);
textarea.add(popupmenu); //文本区添加快捷菜单
dialog = new JDialog(this,"提示");
dialog.setSize(,);
label_dialog = new JLabel("",JLabel.CENTER);
dialog.add(label_dialog);
dialog.setDefaultCloseOperation(HIDE_ON_CLOSE); //单击对话框的关闭按钮时,隐藏对话框而不结束程序运行
}
public void actionPerformed(ActionEvent e) //单击事件处理程序
{
if(e.getActionCommand()=="退出") //不能用switch(int)语句
System.exit(0); //单击菜单项时结束程序
if(e.getActionCommand()=="剪切")
textarea.cut(); //将选中文本剪切送系统剪贴板
if(e.getActionCommand()=="复制")
textarea.copy();
if(e.getActionCommand()=="粘贴")
textarea.paste();
if(e.getSource()==text_size) //单击文本行时,源码改变字号
{
int size=0;
try
{
size = Integer.parseInt(text_size.getText());
if (size<=0 || size>)
throw new Exception("SizeException"); //抛出异常对象
java.awt.Font font = textarea.getFont();
textarea.setFont(new Font(font.getName(),文本a文font.getStyle(),size));
}
catch(NumberFormatException nfe)
{
label_dialog.setText("\""+text_size.getText()+"\" 不能转换成整数,请重新输入!");
dialog.setLocation(this.getX()+,编辑本编this.getY()+);
dialog.setVisible(true);
}
catch(Exception ex)
{
if (ex.getMessage()=="SizeException") //捕获自己抛出的异常对象
{
label_dialog.setText(size+" 字号不合适,请重新输入!");
dialog.setLocation(this.getX()+,器源迪奥口红源码网站this.getY()+);
dialog.setVisible(true);
}
}
finally{ }
}
}
public void itemStateChanged(ItemEvent e) //复选框选择事件处理程序
{ //实现ItemListener接口中的方法
Font font = textarea.getFont();
int style = font.getStyle();
if (e.getSource()==checkbox_bold)
style = style ^ 1; //整数的位运算,异或^
if (e.getSource()==checkbox_italic)
style = style ^ 2;
textarea.setFont(new Font(font.getName(),辑器style,font.getSize()));
}
public void mouseClicked(MouseEvent mec) //单击鼠标时触发
{ //实现MouseListener接口中的方法
if (mec.getModifiers()==mec.BUTTON3_MASK) //单击的是鼠标右键
popupmenu.show(textarea,mec.getX(),mec.getY());//在鼠标单击处显示快捷菜单
}
public void mousePressed(MouseEvent mep) { }
public void mouseReleased(MouseEvent mer) { }
public void mouseEntered(MouseEvent mee) { }
public void mouseExited(MouseEvent mex) { }
public void mouseDragged(MouseEvent med) { }
public static void main(String arg[])
{
new EditorJFrame();
}
}