问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

用java设计一个电子记事本,要含保存,新建等基本功能,用EditPlus或Utredit,最好附带实验结果

发布网友 发布时间:2022-05-13 08:20

我来回答

2个回答

热心网友 时间:2024-02-22 07:52

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Notepad implements ActionListener{
JFrame win;
FileInputStream fis=null;
InputStreamReader isr=null;
BufferedReader br=null;
FileOutputStream fos=null;
OutputStreamWriter osr=null;
Exitfounction ef=new Exitfounction();
JMenuItem saveItem,othersave,gb,gb2,ut,is;
JButton save;
JTextArea area;
File file;
String codetype=null;
static Font f=new Font("",Font.BOLD,15);
public Notepad(){
win=new JFrame();
area=new JTextArea();
area.setText("");
area.setFont(f);
/**主面板*/
JPanel main=new JPanel();
main.setLayout(new BorderLayout());
/**菜单面板*/
JMenuBar menuBar=createmenu();
/**中间面板*/
JPanel centerBar=new JPanel();
centerBar.setLayout(new BorderLayout());
/**工具兰面板*/
JToolBar function=function();
/**文本面板*/
JScrollPane textPanel=new JScrollPane();
/**面板布局*/
main.add(BorderLayout.NORTH,menuBar);
main.add(BorderLayout.CENTER,centerBar);
centerBar.add(BorderLayout.NORTH,function);
centerBar.add(BorderLayout.CENTER,textPanel);
textPanel.getViewport().add(area);
win.setContentPane(main);

addEventHandle();
}
private JToolBar function() {
JToolBar function=new JToolBar();
JButton newfile=new JButton("新建");
newfile.addActionListener(this);
// newfile.setFont(f);
save=new JButton("保存");
JButton print=new JButton("打印");
JButton backout=new JButton("撤销");
JButton copy=new JButton("复制");
JButton paste=new JButton("粘贴");
JButton find=new JButton("查找");
JButton replace=new JButton("替换");
function.add(newfile);
function.add(save);
function.add(print);
function.add(backout);
function.add(copy);
function.add(paste);
function.add(find);
function.add(replace);
return function;
}

private JMenuBar createmenu() {
JMenuBar menuBar=new JMenuBar();
JMenu file=new JMenu("文件");
JMenu editor=new JMenu("编辑");
JMenu see=new JMenu("查看");
JMenu search=new JMenu("搜索");
JMenu tool=new JMenu("工具");
JMenu doc=new JMenu("文档");
JMenu help=new JMenu("帮助");
JMenuItem newfile=new JMenuItem("新建");newfile.addActionListener(this);
JMenuItem open=new JMenuItem("打开");open.addActionListener(this);
othersave=new JMenuItem("另存为");
saveItem=new JMenuItem("保存");
JMenuItem exit=new JMenuItem("退出");
menuBar.add(file);
menuBar.add(editor);
menuBar.add(see);
menuBar.add(search);
menuBar.add(tool);
menuBar.add(doc);
menuBar.add(help);
file.add(newfile);
file.add(open);
file.add(saveItem);
file.add(othersave);
file.add(exit);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
ef.exit();
}
});
JMenuItem item;
editor.add(item=new JMenuItem("查找"));//item.addActionListener(this);
editor.add(item=new JMenuItem("替换"));//item.addActionListener(this);
editor.addSeparator();
editor.add(item=new JMenuItem("复制"));//item.addActionListener(this);
editor.add(item=new JMenuItem("粘贴"));//item.addActionListener(this);
editor.addSeparator();
editor.add(item=new JMenuItem("剪切"));//item.addActionListener(this);
editor.add(item=new JMenuItem("全选"));//item.addActionListener(this);

see.add(item=new JCheckBoxMenuItem("工具栏"));//item.addActionListener(this);
see.add(item=new JCheckBoxMenuItem("状态栏"));//item.addActionListener(this);
see.add(item=new JCheckBoxMenuItem("测边栏"));//item.addActionListener(this);

search.add(item=new JMenuItem("查找"));
search.add(item=new JMenuItem("查找上一个"));
search.add(item=new JMenuItem("查找下一个"));
search.add(item=new JMenuItem("替换"));

/**级联菜单*/
JMenu font=new JMenu("字体");//级联菜单
font.add(item=new JMenuItem("宋体"));//item.addActionListener(this);
font.add(item=new JMenuItem("楷体"));//item.addActionListener(this);
font.add(item=new JMenuItem("隶书"));//item.addActionListener(this);
font.add(item=new JMenuItem("黑体"));//item.addActionListener(this);
tool.add(font);
tool.add(item=new JMenuItem("颜色"));item.addActionListener(this);
tool.addSeparator();
ButtonGroup bg=new ButtonGroup();//按钮组 实现单选功能
tool.add(gb=new JRadioButtonMenuItem("GBK"));
bg.add(gb);gb.addActionListener(this);
tool.add(gb2=new JRadioButtonMenuItem("GB2312"));
bg.add(gb2);gb2.addActionListener(this);
tool.add(ut=new JRadioButtonMenuItem("UTF-8",true));
bg.add(ut);ut.addActionListener(this);
tool.add(is=new JRadioButtonMenuItem("ISO-8859-1"));
bg.add(is);is.addActionListener(this);

help.add(item=new JMenuItem("帮助....."));//item.addActionListener(this);
help.add(item=new JMenuItem("关于....."));//item.addActionListener(this);
return menuBar;
}
class Exitfounction{
public void exit(){
int ok=JOptionPane.showConfirmDialog(win, "Are you sure?","yes/no",JOptionPane.YES_NO_OPTION);
if(ok==JOptionPane.YES_NO_OPTION){
System.exit(0);
}else{win.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);}
}
}
private void addEventHandle(){
area.getDocument().addDocumentListener(new DocumentListener(){

public void changedUpdate(DocumentEvent e) {
saveItem.setEnabled(true);
save.setEnabled(true);
}
public void insertUpdate(DocumentEvent e) {
saveItem.setEnabled(true);
save.setEnabled(true);
}
public void removeUpdate(DocumentEvent e) {
saveItem.setEnabled(true);
save.setEnabled(true);
}});
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
saveItem.setEnabled(false);
save.setEnabled(false);
try {
fos=new FileOutputStream(file.getAbsolutePath());
osr=new OutputStreamWriter(fos);//通过自己手动设置编码来读取文件
osr.write(area.getText());
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(osr!=null)try{osr.close();}catch(Exception ee){}
if(fos!=null)try{fos.close();}catch(Exception ee){}
}

}});
saveItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
saveItem.setEnabled(false);
save.setEnabled(false);
try {
fos=new FileOutputStream(file.getAbsolutePath());
osr=new OutputStreamWriter(fos);//通过自己手动设置编码来读取文件
osr.write(area.getText());
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(osr!=null)try{osr.close();}catch(Exception ee){}
if(fos!=null)try{fos.close();}catch(Exception ee){}
}
}});
othersave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser();
int n=jfc.showSaveDialog(win);//弹出对话框
if(n==JFileChooser.APPROVE_OPTION){
file=jfc.getSelectedFile();//得到被选中文件对象
try {
fos=new FileOutputStream(file.getAbsolutePath());
osr=new OutputStreamWriter(fos,codetype);//通过自己手动设置编码来读取文件
osr.write(area.getText());
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(osr!=null)try{osr.close();}catch(Exception ee){}
if(fos!=null)try{fos.close();}catch(Exception ee){}
}
}

}});

}
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
if(str.equals("打开")){
area.setText("");
JFileChooser jfc=new JFileChooser();
int n=jfc.showOpenDialog(win);//弹出对话框
file=jfc.getSelectedFile();//得到被选中文件对象
if(file!=null){
try {
fis=new FileInputStream(file.getAbsolutePath());
isr=new InputStreamReader(fis);//可以通过自己手动设置编码来读取文件
br=new BufferedReader(isr);
String str1;
while((str1=br.readLine())!=null){//直接读取一行
area.append(str1+"\n");
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(Exception ee){}
if(isr!=null)try{isr.close();}catch(Exception ee){}
if(fis!=null)try{fis.close();}catch(Exception ee){}
}
}
}if(str.equals("颜色")){
Color c=JColorChooser.showDialog(win, "选择文字颜色", Color.BLACK);//颜色选择
area.setForeground(c);
} if(gb.isSelected()){
codetype="GBK";
area.setText("");
try {
fis=new FileInputStream(file.getAbsolutePath());
isr=new InputStreamReader(fis,"GBK");//可以通过自己手动设置编码来读取文件
br=new BufferedReader(isr);
String str1;
while((str1=br.readLine())!=null){//直接读取一行
area.append(str1+"\n");
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(Exception ee){}
if(isr!=null)try{isr.close();}catch(Exception ee){}
if(fis!=null)try{fis.close();}catch(Exception ee){}
}
}else if(gb2.isSelected()){
codetype="GB2312";
area.setText("");
try {
fis=new FileInputStream(file.getAbsolutePath());
isr=new InputStreamReader(fis,"GB2312");//可以通过自己手动设置编码来读取文件
br=new BufferedReader(isr);
String str1;
while((str1=br.readLine())!=null){//直接读取一行
area.append(str1+"\n");
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(Exception ee){}
if(isr!=null)try{isr.close();}catch(Exception ee){}
if(fis!=null)try{fis.close();}catch(Exception ee){}
}
}else if(ut.isSelected()){
codetype="UTF-8";
area.setText("");
try {
fis=new FileInputStream(file.getAbsolutePath());
isr=new InputStreamReader(fis,"UTF-8");//可以通过自己手动设置编码来读取文件
br=new BufferedReader(isr);
String str1;
while((str1=br.readLine())!=null){//直接读取一行
area.append(str1+"\n");
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(Exception ee){}
if(isr!=null)try{isr.close();}catch(Exception ee){}
if(fis!=null)try{fis.close();}catch(Exception ee){}
}
}else if(is.isSelected()){
codetype="ISO-8859-1";
area.setText("");
try {
fis=new FileInputStream(file.getAbsolutePath());
isr=new InputStreamReader(fis,"ISO-8859-1");//可以通过自己手动设置编码来读取文件
br=new BufferedReader(isr);
String str1;
while((str1=br.readLine())!=null){//直接读取一行
area.append(str1+"\n");
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(Exception ee){}
if(isr!=null)try{isr.close();}catch(Exception ee){}
if(fis!=null)try{fis.close();}catch(Exception ee){}
}
}
if(str.equals("新建")){
area.setText("");
}
}
public void showme(){
win.setSize(500,400);
win.setVisible(true);
win.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
ef.exit();
}
});
}
public static void main(String[] args) {
new Notepad().showme();
}
}

热心网友 时间:2024-02-22 07:53

百度知道真的不适合贴长长的代码~
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
怎样喷香水留香更持久?有哪些技巧? 有哪些方法让香水持久度更长一些? 怎样喷香水比较持久? 如何喷香水能使味道更持久? 败北是什么梗 湖南第一师范学院生活开销 赣州博沃新能源科技有限公司怎么样? 在湖南第一师范学院读书需要支出哪些费用 江西信丰恒隆麦饭石酒业有限公司公司荣誉 信丰县资源特点 户口不是镇江市的可以办老年公交卡吗? 如何用java一步步编出一个记事本程序 镇江市公交卡究竟怎么算钱? 镇江市丹徒区办老年公交卡在什么地方办?丹徒区老年公交卡在那里办,带上老年人的身份证代办可不可以?_百度问一问 镇江市丹徒区三山镇这边有没有充值公交卡的地方 java电子记事本的设计与实现 镇江的公交卡能在南京充值吗?是宁镇扬通用的那个江苏省公交一卡通 java记事本程序的设计与实现应该用什么设计模式写(两个及以上) 镇江丹徒新区公交IC卡充值点在什么地方? 镇江公交卡办理地点 镇江市公交卡在哪里办? mac 怎么将已经创建的分区删除回到最初的样子 住房公积金交了刚好一年,中间停了一年,今天重新交了,不过才交两个月。 住房公积金中间断了怎么办 住房公积金中断了怎么办 猴年的男宝宝姓余起什么名字好 有关新加坡绿卡问题 如何办理新加坡绿卡 少数民族是否可以办理新加坡绿卡?有什么条件? 在新加坡几年可以办绿卡? 简述索绪尔的结构主义语言观 用java实现记事本全部功能 对未学会言语符号儿童的训练是怎样的? 用java编写记事本程序,可以实现新建、打开、保存、退出、复制、粘贴、剪切、全选。 玫瑰花枸杞红枣泡茶,黑芝麻核桃枸杞能一起吃吗 黑芝麻糊里能放枸杞和红枣吗 普洱茶和黑芝麻枸杞红枣泡水可以? 黑芝麻红枣枸杞西洋参一起泡水喝有没有坏处 5年前有一个公司股份转让给了公司同事,转让完本人与该公司没任何关系。转让时? 股东股权转让后是否还承担公司债务 公司股权变更后,原股权人还与公司有关系吗? 公司转让出去后,原法人还对公司有什么责任么 我是公司的股东,我全部股份转让给法人 做了工商变更。请问之前以公司名义的贷款 变更后我还有责任吗? 淘米水泡白萝卜可以浇花? 萝卜叶子怎么做泡菜 能把windows装有u盘里吗 在EXCEL中将一个单元格(插入两条斜线)分为三部分,如何在这三部 电脑应用程序无法启动,因为应用程序的并行配置不正确 为什么我插入移动硬盘,但是在我的电脑里显示不出“可移动盘”啊 早上起来耳朵嗡嗡响,有回声是怎么回事