同步操作将从 华南农业大学-吴春胤/java2021spring 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package java2020spring;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Calendar;import javax.swing.*;import javax.swing.event.CaretEvent;import javax.swing.event.CaretListener;import javax.swing.event.UndoableEditListener;public class Plzy extends JFrame{JCheckBoxMenuItem Item1;JLabel label1, label2;JToolBar state;int row = 1;int column = 1;int length = 0;JTextArea workArea = new JTextArea(); //创建多行文本框public Plzy(){super("简易文本编辑器"); //调用父类的构造方法//创建菜单栏(JMenuBar)对象JMenuBar mBar = new JMenuBar(); //在JFrame等容器中设置菜单栏对象this.setJMenuBar(mBar);//创建菜单JMenu mfile = new JMenu("文件");JMenu medit = new JMenu("编辑");JMenu mform = new JMenu("格式");JMenu mview = new JMenu("查看");JMenu mhelp = new JMenu("帮助");JMenu mcount = new JMenu("统计");//将菜单添加到菜单栏中mBar.add(mfile);mBar.add(medit);mBar.add(mform);mBar.add(mview);mBar.add(mhelp);mBar.add(mcount);JScrollPane imgScrollPane = new JScrollPane(workArea); //创建一个空视图,组件内容超过视图大小显示水平和垂直滚动条add(imgScrollPane,BorderLayout.CENTER); //将当前类的对象实例加到frame的中间位置//定义打开和保存对话框FileDialog openDia;FileDialog saveDia;//默认模式为 FileDialog.LOADopenDia = new FileDialog(this,"打开",FileDialog.LOAD);saveDia = new FileDialog(this,"另存为",FileDialog.SAVE);//新建JMenuItem item1_1 = new JMenuItem("新建"); //生成一个对象item1_1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ //一个监听器,点击时就会触发监听函数里面的内容workArea.setText(""); //清空文本}});//打开JMenuItem item1_2 = new JMenuItem("打开");item1_2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ //一个监听器,点击时就会触发监听函数里面的内容openDia.setVisible(true); //打开文件对话框String dirPath = openDia.getDirectory(); //获取打开文件路径并保存String fileName = openDia.getFile(); //获取文件名称并保存if(dirPath == null || fileName == null){return ;}workArea.setText(""); //清空文本File fileO = new File(dirPath,fileName);try{BufferedReader bufr = new BufferedReader(new FileReader(fileO)); //尝试从文件中读取内容String line = null; //变量字符串初始化为空while((line = bufr.readLine()) != null){workArea.append(line + "\r\n"); //显示每行内容}bufr.close(); //关闭文本}catch(IOException er1){throw new RuntimeException("文件读取失败!");}}});//保存JMenuItem item1_3 = new JMenuItem("保存");item1_3.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ //加一个监听器,点击时就会触发监听函数里面的内容File fileS = null;if(fileS == null){saveDia.setVisible(true); //显示保存文件对话框String dirPath = saveDia.getDirectory(); //获取保存文件路径并保存到字符串中String fileName = saveDia.getFile(); //获取保存文件名称并保存到字符串中if(dirPath == null || fileName == null) //判断路径和文件是否为空return; //返回空值fileS = new File(dirPath,fileName); //文件不为空,新建一个路径和名称}try{BufferedWriter bufw = new BufferedWriter(new FileWriter(fileS)); //尝试从文件中读取内容String text = workArea.getText(); //获取文本内容bufw.write(text); //将获取文本内容写入到字符输出流bufw.close(); //关闭文件}catch(IOException er){throw new RuntimeException("文件保存失败!");}}});//另存为JMenuItem item1_4 = new JMenuItem("另存为");item1_4.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ //一个监听器,点击时就会触发监听函数里面的内容File fileS = null;if(fileS == null){saveDia.setVisible(true); //显示另存为文件对话框String dirPath = saveDia.getDirectory(); //获取保存文件路径并保存到字符串中String fileName = saveDia.getFile(); //获取保存文件名称并保存到字符串中if(dirPath == null || fileName == null) //判断路径和文件是否为空return ; //返回空值fileS = new File(dirPath,fileName); //文件不为空,新建一个路径和名称}try{BufferedWriter bufw = new BufferedWriter(new FileWriter(fileS)); //尝试从文件中读取内容String text = workArea.getText(); //获取文本内容bufw.write(text); //将获取文本内容写入到字符输出流bufw.close(); //关闭文件}catch(IOException er){throw new RuntimeException("文件保存失败!");}}});//退出JMenuItem item1_5 = new JMenuItem("退出");item1_5.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ //一个监听器,点击时就会触发监听函数里面的内容System.exit(0);}});//撤销JMenuItem item2_1 = new JMenuItem("撤销");item2_1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String t1 = "";workArea.setText(t1);}});//删除JMenuItem item2_2 = new JMenuItem("剪切");item2_2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){workArea.cut();}});//复制JMenuItem item2_3 = new JMenuItem("复制");item2_3.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){workArea.copy();}});//粘贴JMenuItem item2_4= new JMenuItem("粘贴");item2_4.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){workArea.paste();}});//删除JMenuItem item2_5 = new JMenuItem("删除");item2_5.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){/*删除其实就是用一个空字符串代替你所写的String t1 = "";workArea.setText(t1);*/}});//自动换行JRadioButtonMenuItem item3_1 = new JRadioButtonMenuItem("自动换行",false);item3_1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ //添加一个监听器,点击时就会触发监听函数里面的内容Object source = e.getSource();if(source == item3_1)workArea.setLineWrap(true); //自动换行else if(source != item3_1)workArea.setLineWrap(false);}});//字体设置JMenuItem item3_2=new JMenuItem("字体设置");item3_2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){/*删除其实就是用一个空字符串代替你所写的String t1 = "";workArea.setText(t1);*/}});//状态栏JMenuItem item4_1 = new JMenuItem("菜单状态栏");item4_1 = new JCheckBoxMenuItem("状态栏");//创建状态栏item4_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (Item1.getState())state.setVisible(true);elsestate.setVisible(false);}});state = new JToolBar();state.setSize(workArea.getSize().width, 10);label1 = new JLabel(" 第" + row + " 行,第" + column + " 列");state.add(label1);state.addSeparator();label2 = new JLabel(" 共" + length + " 字 ");state.add(label2);//创建状态栏计算行列监视器workArea.addCaretListener(new CaretListener() {public void caretUpdate(CaretEvent e) {JTextArea eArea = (JTextArea) e.getSource();try { int position = eArea.getCaretPosition();row = eArea.getLineOfOffset(position);column = position - workArea.getLineStartOffset(row);row += 1;label1.setText(" 第" + row + " 行,第" + (column + 1) + " 列");length = Plzy.this.workArea.getText().toString().length();label2.setText(" 共" + length + " 字 ");} catch (Exception ex) {}}});//添加状态栏workArea.add(state, BorderLayout.SOUTH);state.setVisible(false);state.setFloatable(false);//添加时间JMenuItem item4_2=new JMenuItem("时间");item4_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {Calendar cal = Calendar.getInstance(); //与时间相关的Calendar,获取系统的年份int y = cal.get(Calendar.YEAR); //获取当前系统的月份int m = cal.get(Calendar.MONTH);int d = cal.get(Calendar.DATE);int h = cal.get(Calendar.HOUR_OF_DAY);int mi = cal.get(Calendar.MINUTE);int s = cal.get(Calendar.SECOND);String t = "现在时刻是"+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分钟"+s+"秒";workArea.setText(t);}});//帮助JMenuItem item5_1 = new JMenuItem("帮助");item5_1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){ //添加一个监听器,点击时就会触发监听函数里面的内容new Help();}});JMenuItem item5_2 = new JMenuItem("关于");item5_2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){ //添加一个监听器,点击时就会触发监听函数里面的内容new About ();}});//统计JMenuItem item6_1 = new JMenuItem("统计");item6_1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){ //添加一个监听器,点击时就会触发监听函数里面的内容//try代码块,当发生异常时会转到catch代码块中String a = workArea.getText();int c = 0,h = 0,ll = 0,l = 0,n = 0,o = 0,sum = 0;//定义整型变量,用于统计字符数for(int i = 0;i<a.length();i++){String s = a.substring(i,i+1);if (s.matches("[\\u4e00-\\u9fa5]")) {//if语句的条件,判断是否为汉字h++; //若为汉字则c1自增} else if(s.matches("[A-Z]")){ //if语句的条件,判断是否为大写字母ll++; //若为大写字母则c2自增} else if(s.matches("[a-z]")){//if语句的条件,判断是否为小写字母l++; //若为小写字母则c3自增} else if(s.matches("[0-9]")){//if语句的条件,判断是否为数字n++; //若为数字则c4自增} else { //否则可判断为其他字符o++; //若为其他字符则c5自增}}sum = h + ll + l + n + o ;//统计总字符数JOptionPane.showMessageDialog(Plzy.this, "字数统计:\n汉字:"+h+"\n大写字母:"+ll+"\n小写字母:"+l+"\n数字:"+n+"\n其他字符:"+o+"\n共计"+sum);}});//在菜单中添加菜单项mfile.add(item1_1);mfile.add(item1_2);mfile.add(item1_3);mfile.add(item1_4);mfile.add(item1_5);medit.add(item2_1);medit.add(item2_2);medit.add(item2_3);medit.add(item2_4);medit.add(item2_5);mform.add(item3_1);mview.add(item4_1);mview.add(item4_2);mhelp.add(item5_1);mhelp.add(item5_2);mcount.add(item6_1);}//构造方法结束public static void main(String args[]){Plzy app = new Plzy();app.setSize(800, 600); //设置窗口大小,宽度800,高度600app.setLocation(200,200); //设置窗口位置为距离屏幕左边水平方向200,上方垂直方向200app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //点击关闭按钮是直接退出app.setVisible(true); //设置窗体可见}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。