您当前的位置:首页 > 互联网教程

求个用JAVA编写的记事本程序!先谢谢了!!

发布时间:2025-05-19 21:24:57    发布人:远客网络

求个用JAVA编写的记事本程序!先谢谢了!!

一、求个用JAVA编写的记事本程序!先谢谢了!!

JFrame mainWindow=new JFrame("文本编辑器");

JMenuBar menuBar=new JMenuBar();

JMenu fileMenu=new JMenu("文件");

JMenu helpMenu=new JMenu("帮助");

JMenuItem openItem=new JMenuItem("打开");

JMenuItem saveItem=new JMenuItem("保存");

JMenuItem resaveItem=new JMenuItem("另存为");

JMenuItem exitItem=new JMenuItem("退出");

JMenuItem helpItem=new JMenuItem("关于文本编辑器");

JTextArea textArea=new JTextArea(15,30);

JScrollPane js= new JScrollPane();

JLabel labelInfo=new JLabel("状态栏");

Listener listen=new Listener();

JFileChooser openDialog=new JFileChooser("F:\\");

openItem.addActionListener(listen);

saveItem.addActionListener(listen);

resaveItem.addActionListener(listen);

exitItem.addActionListener(listen);

helpItem.addActionListener(listen);

js.getViewport().setView(textArea);

mainWindow.setJMenuBar(menuBar);

mainWindow.getContentPane().add(js,BorderLayout.CENTER);

mainWindow.getContentPane().add(labelInfo,BorderLayout.SOUTH);

//mainWindow.setBounds(0, 0, 300, 300);

mainWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

//窗口监听事件,当关闭窗口时触发

mainWindow.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

int len=JOptionPane.showConfirmDialog(mainWindow,"文件未保存,是否保存");

if(len==JOptionPane.NO_OPTION) System.exit(0);

if(len==JOptionPane.YES_OPTION){

len=openDialog.showSaveDialog(mainWindow);

if(len==JFileChooser.APPROVE_OPTION){

File exitFile=openDialog.getSelectedFile();

if(exitFile.exists()) labelInfo.setText(exitFile.getPath()+"保存成功");

else labelInfo.setText("返回原程序");

else labelInfo.setText("返回原程序");

labelInfo.setText("退出程序");

int len=JOptionPane.showConfirmDialog(mainWindow,"文件未保存,是否保存");

if(len==JOptionPane.NO_OPTION) System.exit(0);

if(len==JOptionPane.YES_OPTION){

len=len=openDialog.showSaveDialog(mainWindow);

File exitFile=openDialog.getSelectedFile();

if(exitFile.exists()) labelInfo.setText(exitFile.getPath()+"保存成功");

else labelInfo.setText("返回原程序");

//按适合的大小显示窗体于屏幕中央

Dimension screenSize= Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize= mainWindow.getPreferredSize();

if(frameSize.height> screenSize.height){

frameSize.height= screenSize.height;

if(frameSize.width> screenSize.width){

frameSize.width= screenSize.width;

mainWindow.setLocation(((screenSize.width- frameSize.width)/ 2),

((screenSize.height- frameSize.height)/ 2));

mainWindow.setSize(frameSize);

class Listener implements ActionListener{

public void actionPerformed(ActionEvent e){

String command=e.getActionCommand();

int len=openDialog.showOpenDialog(mainWindow);

File openFile=openDialog.getSelectedFile();

if(len==JFileChooser.APPROVE_OPTION){

souTime=openFile.lastModified();

int len=openDialog.showSaveDialog(mainWindow);

if(len==JFileChooser.APPROVE_OPTION){

File resaveFile=openDialog.getSelectedFile();

if(resaveFile.exists()) labelInfo.setText(resaveFile.getPath()+"保存成功");

File saveFile=openDialog.getSelectedFile();

int len=openDialog.showSaveDialog(mainWindow);

if(len==JFileChooser.APPROVE_OPTION){

File resaveFile=openDialog.getSelectedFile();

if(resaveFile.exists()) labelInfo.setText(resaveFile.getPath()+"保存成功");

else labelInfo.setText("返回原程序");

int len=JOptionPane.showConfirmDialog(mainWindow,"文件未保存,是否保存");

if(len==JOptionPane.NO_OPTION) System.exit(0);

if(len==JOptionPane.YES_OPTION){

len=openDialog.showSaveDialog(mainWindow);

if(len==JFileChooser.APPROVE_OPTION){

File exitFile=openDialog.getSelectedFile();

labelInfo.setText(exitFile.getPath()+"保存成功");

else labelInfo.setText("返回原程序");

else labelInfo.setText("返回原程序");

labelInfo.setText("退出程序");

int len=JOptionPane.showConfirmDialog(mainWindow,"文件未保存,是否保存");

if(len==JOptionPane.NO_OPTION) System.exit(0);

if(len==JOptionPane.YES_OPTION){

len=len=openDialog.showSaveDialog(mainWindow);

File exitFile=openDialog.getSelectedFile();

if(exitFile.exists()) labelInfo.setText(exitFile.getPath()+"保存成功");

else labelInfo.setText("返回原程序");

//“关于文本编辑器”菜单项处理动作

if(command.equals("关于文本编辑器")){

JOptionPane.showMessageDialog(null,"文本编辑器Version 0.7");

labelInfo.setText("返回原程序");

//String filePath=file.getParent()+"\\"+file.getName();

FileReader in=new FileReader(file);

textArea.setText(new String(buffer,0,len));

labelInfo.setText("打开"+file.getPath());

FileWriter out=new FileWriter(file);

if(desTime!=souTime&&file.exists()){

labelInfo.setText(file.getPath()+"保存成功");

public static void main(String[] args){

二、用JAVA编个简单的记事本程序

class Editor extends Frame implements ActionListener,ItemListener

int n1;String str="";int k=0;String st1,st2;

JTextArea ta=new JTextArea(50,50);

Button btn1,btn2,btn3;Panel p1=new Panel();

Panel p2=new Panel();Panel p3=new Panel();Panel p4=new Panel();

ImageIcon YouImg= new ImageIcon("iconimage.jpg");

setIconImage(YouImg.getImage());

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

Menu file=new Menu("文件(F)");Menu edit=new Menu("编辑(E)");Menu help=new Menu("帮助(H)");

Menu pattern=new Menu("格式(P)");Menu time=new Menu("时间(T)");

MenuItem exit=new MenuItem("退出",new MenuShortcut(KeyEvent.VK_Q));

MenuItem tdate=new MenuItem("日期/时间",new MenuShortcut(KeyEvent.VK_D));

MenuItem renew=new MenuItem("新建", new MenuShortcut(KeyEvent.VK_N));

MenuItem save=new MenuItem("保存", new MenuShortcut(KeyEvent.VK_S));

MenuItem open=new MenuItem("打开",new MenuShortcut(KeyEvent.VK_O));

MenuItem copy=new MenuItem("复制",new MenuShortcut(KeyEvent.VK_C));

MenuItem paste=new MenuItem("粘贴",new MenuShortcut(KeyEvent.VK_V));

MenuItem cut=new MenuItem("剪切",new MenuShortcut(KeyEvent.VK_X));

MenuItem helptopic=new MenuItem("主题",new MenuShortcut(KeyEvent.VK_T));

helptopic.addActionListener(this);

dialog=new Dialog(this,"字体设置");dialog.setSize(300,300);

dialog1=new Dialog(this,"帮助主题");dialog1.setBounds(250,100,300,400);

dialog.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

dialog1.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

//dialog.setBackground(Color.WHITE);

dialog.setLayout(null);dialog.setBounds(170,150,450,300);

Label L1=new Label("字体(F)");Label L2=new Label("字形(Y)");

Label L3=new Label("大小(S)");TextArea ta1=new TextArea(5,5);

ta1=new TextArea("至于记事本的主题嘛,因为本人的水平有限,所有不能为大家作过多的指导,望大家见谅!");//在这里我想加一个文本框用来放帮助信息。

ce1=new Choice();ce2=new Choice();

btn1=new Button("确定");btn2=new Button("取消");btn1.addActionListener(this);btn2.addActionListener(this);

btn3.addActionListener(this);

ce1.add("华文行楷");ce1.add("华文中宋");ce1.add("华文新魏");ce1.add("华文细黑");ce1.add("宋体");ce1.add("方正姚体");

ce1.add("幼圆");ce1.add("隶书");ce1.add("楷体-GB2312");ce1.add("华文行楷");ce1.add("华文彩云");ce1.add("仿宋-GB2312");

ce2.add("粗体");ce2.add("斜体");ce2.add("常规");

for(int n1=0;n1<=100;n1++)

dialog.add(p1);p1.setBounds(5,30,440,35);p1.setLayout( null);//p1.setBackground(Color.red);

dialog.add(p2);p2.setBounds(5,65,440,27);dialog.add(p3);p3.setBounds(5,90,440,40);//p4.setBounds(5,120,440,138);

p1.add(L1);L1.setBounds(5,15,50,25);p1.add(L2);L2.setBounds(150,15,50,25);

p1.add(L3);L3.setBounds(250,15,50,25);p2.setLayout( null);//p2.setBackground(Color.yellow);

p2.add(ce1);ce1.setBounds(5,0,130,25);p2.add(ce2);ce2.setBounds(160,0,90,25);p2.add(ce3);ce3.setBounds(260,0,75,25);

p2.add(btn1);btn1.setBounds(360,0,75,25);

p3.setLayout( null);p3.add(btn2);btn2.setBounds(360,10,75,25);p3.add(btn3);btn3.setBounds(160,10,75,25);

dialog.add(p4);p4.setBackground(Color.yellow);p4.setBounds(5,120,440,138);

MenuItem Font=new MenuItem("字体");//给字体加监听器

Font.addActionListener(this);

MenuItem Replace=new MenuItem("替换"); MenuItem Seek=new MenuItem("查找");

mb.add(file);mb.add(edit);mb.add(pattern);mb.add(time);mb.add(help);

file.add(renew);file.addSeparator();file.add(open);time.add(tdate);

file.addSeparator(); file.add(exit);file.addSeparator();file.add(save);

edit.add(copy); edit.addSeparator();edit.add(paste);edit.addSeparator();edit.add(cut);

pattern.add(Font); pattern.add(Replace);

pattern.add(Seek);help.add(helptopic);

renew.addActionListener( this);tdate.addActionListener(this);

open.addActionListener(this); save.addActionListener(this);exit.addActionListener(this);

cut.addActionListener(this);copy.addActionListener(this);paste.addActionListener(this);

ce1.addItemListener(this);ce2.addItemListener(this);ce3.addItemListener(this);

add(ta,"Center");ta.setBackground(new Color(249,255,234));

public void itemStateChanged(ItemEvent e){

if(ce2.getSelectedItem().equals("粗体"))

{ k=1;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}

if(ce2.getSelectedItem().equals("斜体"))

{ k=2;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}

if(ce2.getSelectedItem().equals("常规"))

{ k=3;st1=ce1.getSelectedItem();st2=ce3.getSelectedItem();}

public void actionPerformed(ActionEvent e)

if(e.getActionCommand().equals("新建"))

if(e.getActionCommand().equals("退出"))

if(e.getActionCommand().equals("打开"))

if(e.getActionCommand().equals("保存"))

if(e.getActionCommand().equals("复制"))

if(e.getActionCommand().equals("粘贴"))

{ ta.insert(str,ta.getCaretPosition());}

if(e.getActionCommand().equals("剪切"))

{str=ta.getSelectedText();ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());

if(e.getActionCommand().equals("字体"))

if(e.getActionCommand().equals("主题"))

if(e.getActionCommand().equals("颜色"))

Color clr=JColorChooser.showDialog(this,"颜色对话框",null);

{ ta.setFont(new Font(st1,Font.BOLD,Integer.parseInt(st2)));}

{ ta.setFont(new Font(st1,Font.ITALIC,Integer.parseInt(st2)));}

{ ta.setFont(new Font(st1,Font.PLAIN,Integer.parseInt(st2)));}

//if(e.getActionCommand().equals("日期/时间"))

//ta.setText(ta.getText()+""+date);

if(e.getActionCommand().equals("日期/时间"))

Calendar c1=Calendar.getInstance();

int y= c1.get(Calendar.YEAR);

int m= c1.get(Calendar.MONTH);

int d= c1.get(Calendar.DATE);

int h= c1.get(Calendar.HOUR);

int m1= c1.get(Calendar.MINUTE);

ta.setText(ta.getText()+""+(y+"年"+m2+"月"+d+"日"+h+":"+m1));

public void openText() throws IOException

FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);

fd.setVisible(true);FileInputStream fis=new FileInputStream(fd.getDirectory()+fd.getFile());

public void saveText() throws IOException

FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.SAVE);

FileOutputStream out=new FileOutputStream(fd.getDirectory()+fd.getFile()+".txt");

for(int n=0;n<str.length();n++)

out.write((byte)str.charAt(n));

public static void main(String[] args)

Editor f=new Editor("记事本程序");

/*Calendar now=Calendar.getInstance();

int year=now.get(Calendar.YEAR);

int month=now.get(Calendar.MONTH)+1;

int day=now.get(Calendar.DATE);

System.out.print(year+"年"+month+"月"+day+"日");

int week=now.get(Calendar.DAY_OF_WEEK);

String str="日一二三四五六";//星期1-7

System.out.println("星期"+str.substring(i,i+2));//对应中文的下标*/

class Mycanvas extends Canvas

img=tk.getImage("photo.jpg");

public void paint(Graphics g)

g.drawImage(img,5,120,440,138,this);

三、如何用记事本编写JAVA程序

1、我也是新手。。。。刚好把我的经验传授给你。、

2、先下个JDK。。。然后单击我的电脑属性,在高级里面设计环境变量。。

3、然后在计事本中写程序,如果是运行程序那么类名要和文件名一样。文件名的拓展名为.java。然后在命令提示符里输入你java文件的目录位置,运行javac XXX.java

4、然后运行java XXX就可以运行了。。至于记事本中怎么写你自己看着办。。