开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
Plzy.java 12.26 KB
一键复制 编辑 原始数据 按行查看 历史
刘芷瑜 提交于 2021年06月11日 13:13 +08:00 . 第一次提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
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.LOAD
openDia = 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);
else
state.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,高度600
app.setLocation(200,200); //设置窗口位置为距离屏幕左边水平方向200,上方垂直方向200
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //点击关闭按钮是直接退出
app.setVisible(true); //设置窗体可见
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LuckyYu233/java2021spring.git
git@gitee.com:LuckyYu233/java2021spring.git
LuckyYu233
java2021spring
java2021spring
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /