用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
百度知道真的不适合贴长长的代码~