用JAVA编个简单的记事本程序
发布时间:2025-05-21 13:47:30 发布人:远客网络
一、用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编程
安装后需要设置环境变量,在我的电脑--》属性--->高级--->环境变量
加入:JAVA_HOME= C:\Program Files\Java\jdk1.5.0_06
然后把JAVA_HOME加入path中就可以了
PATH=F:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.3.1\bin;C:\Program Files\Oracle\jre\1.1.8\bin;%JAVA_HOME%\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\PROGRA~1\ULTRAE~1;D:\soft\StyleReportEE\bin
3:工具初学者可以用记事本,(也可以用eclipse,netbeans)
在DOS窗口下找到你保存的JAVA文件目录,执行:javac*.java
在dos窗口下找到你编译的文件目录执行: java*
这样你就可以看到你写的程序的执行结果。
刚开始学Java,用“JDK+记事本”就行了,JDK提供Java开发工具包和运行环境。先下载一个JDK()装上(还有设置环境变量的问题,这里先不说),用记事本编辑Java代码,在命令行提示窗口用javac命令编译,用java命令执行就可以了。
当你对java程序有个整体感官的认识后,接下来就可以用一个IDE了,即集成开发环境,网上有很多IDE,JBuilder、JCreater以及著名的eclipse,推荐用eclipse,在www.eclipse.org下载,然后自己往里面安装——你需要但还没安装的“插件”(因为,本身已有很多插件,一般情况可以满足你的需要),最好不要用通过第三方公司进行过集成的 eclipse,比如Lomboz(它的内核也是eclipse,如果你需要Lomboz,则可以把它作为一个插件安装到eclipse中),这样,可以直接感觉eclipse的架构和魅力。
最后,要说的就是,在第一个阶段不要停留太久,要在适当的时候过渡到eclipse;所有上面是一个过程。
三、java的一个简单记事本程序设计
是记事本的样式吗?就要这些要求吗?
下面这些代码的功能挺全的,可以参考一下,不用的可以删掉。
public class NoteBook extends JFrame implements ActionListener,ItemListener,WindowListener,MouseListener
{ Container c= this.getContentPane();
JMenuBar jmb= new JMenuBar();
JColorChooser jcl= new JColorChooser();
JToolBar jToolBar= new JToolBar();
JButton jButton1= new JButton();
JButton jButton2= new JButton();
JButton jButton3= new JButton();
JButton jButton4= new JButton();
JMenu jm1= new JMenu("文件(F)");
JMenu jm2= new JMenu("编辑(E)");
JMenu jm4= new JMenu("格式(O)");
JMenu jm5= new JMenu("查看(V)");
JMenu jm3= new JMenu("帮助(H)");
JMenuItem jmi1= new JMenuItem("新建(N)");
JMenuItem jmi2= new JMenuItem("打开(O)");
JMenuItem jmi3= new JMenuItem("保存(S)");
JMenuItem jmi4= new JMenuItem("退出(X)");
JMenu jmd4=new JMenu("颜色");
JMenu jmd0=new JMenu("字体");
JMenuItem aa=new JMenuItem("复制(C)");
JMenuItem bb=new JMenuItem("剪切(X)");
JMenuItem cc=new JMenuItem("粘贴(V)");
JCheckBoxMenuItem jcuti1= new JCheckBoxMenuItem("粗体");
JCheckBoxMenuItem jxieti1= new JCheckBoxMenuItem("斜体");
JMenuItem jmp1= new JMenuItem("日期和时间");
JMenuItem jmp2= new JMenuItem("背景颜色(H)");
JMenuItem jmi5= new JMenuItem("删除内容(S)");
JMenuItem jmi7=new JMenuItem("关于自己");
JTextArea jta= new JTextArea(23,15);
JScrollPane jsp= new JScrollPane(jta);
JMenuItem jmdb1=new JMenuItem("黄色");//定义各种颜色
JMenuItem jmdb2=new JMenuItem("红色");
JMenuItem jmdb3=new JMenuItem("绿色");
JMenuItem jmdb4=new JMenuItem("蓝色");
JMenuItem jmdb5=new JMenuItem("黑色");
{ this.addWindowListener(this);
c.add(jmb,BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jm5.setMnemonic('v');//查看
jmi1.addActionListener(this);//新建
jmi1.setAccelerator(KeyStroke.getKeyStroke('N',Event.CTRL_MASK,false));
jmi2.setAccelerator(KeyStroke.getKeyStroke('O',Event.CTRL_MASK,false));
jmi2.addActionListener(this);//打开
jmi3.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK,false));
jmi3.addActionListener(this);//保存
jmi1.setMnemonic('n');
jmi2.setMnemonic('o');
jmi3.setMnemonic('s');
jmi4.setMnemonic('x');
jmi4.addActionListener(this);//退出
jmd4.addActionListener(this);//添加监视器
jmdb1.addActionListener(this);
jmdb2.addActionListener(this);
jmdb3.addActionListener(this);
jmdb4.addActionListener(this);
jmdb5.addActionListener(this);
jmp1.addActionListener(this);//日期时间
jmp2.addActionListener(this);//背景颜色
aa.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
bb.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
cc.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
aa.addActionListener(this);//复制
bb.addActionListener(this);//剪切
cc.addActionListener(this);//粘贴
jcuti1.addItemListener(this);//粗体
jxieti1.addItemListener(this);//斜体
jmi7.addActionListener(this);//关于自己
c.add(jToolBar,BorderLayout.CENTER);//工具条
jButton2.setIcon(new ImageIcon("image\\new.JPG"));//新建
jButton2.setToolTipText("新建");
jButton2.addActionListener(this);
jButton1.setIcon(new ImageIcon("image\\open.JPG"));//打开
jButton1.setToolTipText("打开");
jButton1.addActionListener(this);
jButton3.setIcon(new ImageIcon("image\\save.JPG"));//保存
jButton3.setToolTipText("保存");
jButton3.addActionListener(this);
jButton4.setIcon(new ImageIcon("image\\qq.JPG"));
jButton4.addActionListener(this);//退出
jButton4.setToolTipText("退出");
c.add(jsp,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jmdb1.addActionListener(new ActionListener()//黄色
{ public void actionPerformed(ActionEvent e)
{ jta.setForeground(Color.yellow);
jmdb2.addActionListener(new ActionListener()//红色
{ public void actionPerformed(ActionEvent e)
{ jta.setForeground(Color.red);
jmdb3.addActionListener(new ActionListener()//绿色
{ public void actionPerformed(ActionEvent e)
{ jta.setForeground(Color.green);
jmdb4.addActionListener(new ActionListener()//蓝色
public void actionPerformed(ActionEvent e)
jta.setForeground(Color.blue);
jmdb5.addActionListener(new ActionListener()//黑色
public void actionPerformed(ActionEvent e)
jta.setForeground(Color.black);
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==jmi1)//新建
this.setTitle("无标题-记事本");
JFileChooser jfc1= new JFileChooser();
int num1= jfc1.showOpenDialog(this);
if(num1==JFileChooser.APPROVE_OPTION)
FileReader fr= new FileReader(f1);
BufferedReader br= new BufferedReader(fr);
while((str= br.readLine())!=null)
catch(FileNotFoundException e1)
catch(IOException e2){e2.printStackTrace();}
JFileChooser jfc2= new JFileChooser();
int num2= jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
FileWriter fw= new FileWriter(f2);
BufferedWriter bw= new BufferedWriter(fw);
int a= JOptionPane.showConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
JFileChooser jfc2= new JFileChooser();
int num2= jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
FileWriter fw= new FileWriter(f2);
BufferedWriter bw= new BufferedWriter(fw);
if(e.getSource()==jmp1)//日期时间
Calendar c1=Calendar.getInstance();
int m= c1.get(Calendar.MONTH);
int m1= c1.get(Calendar.MINUTE);
jta.setText(y+"年"+m2+"月"+d+"日"+h+":"+m1);
if(e.getSource()==jmp2)//背景色
{ Color ccc= JColorChooser.showDialog(this,"color",Color.BLACK);
jta.setSelectedTextColor(ccc);
if(e.getSource()==jButton1)//打开
JFileChooser jfc1= new JFileChooser();
int num1= jfc1.showOpenDialog(this);
if(num1==JFileChooser.APPROVE_OPTION)
FileReader fr= new FileReader(f1);
BufferedReader br= new BufferedReader(fr);
while((str= br.readLine())!=null)
catch(FileNotFoundException e1)
catch(IOException e2){e2.printStackTrace();}
jta.replaceRange("",jta.getSelectionStart(),jta.getSelectionEnd());//删除
JFileChooser jfc2= new JFileChooser();
int num2= jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
FileWriter fw= new FileWriter(f2);
BufferedWriter bw= new BufferedWriter(fw);
else if(e.getSource()==bb)//剪切
else if(e.getSource()==cc)//粘贴
{int a= JOptionPane.showConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
JFileChooser jfc2= new JFileChooser();
int num2= jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
FileWriter fw= new FileWriter(f2);
BufferedWriter bw= new BufferedWriter(fw);
//if(e.getSource()==jmi7)//关于我
//{About1 a=new About1();//实现跳转
public void itemStateChanged(ItemEvent e)//字体
{Font c=new Font("Serif",Font.PLAIN,14);
if(e.getItemSelectable()==jcuti1)
{ Font f=new Font("Serif",Font.BOLD,20);//粗体
else if(e.getItemSelectable()==jxieti1)
{ Font b=new Font("Serif",Font.ITALIC,20);//斜体
public static void main(String[] args)//入口
public void windowOpened(WindowEvent arg0){}
public void windowClosing(WindowEvent arg0){
int a= JOptionPane.showConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
JFileChooser jfc2= new JFileChooser();
int num2= jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
FileWriter fw= new FileWriter(f2);
BufferedWriter bw= new BufferedWriter(fw);
public void windowClosed(WindowEvent arg0){}
public void windowIconified(WindowEvent arg0){}
public void windowDeiconified(WindowEvent arg0){}
public void windowActivated(WindowEvent arg0){}
public void windowDeactivated(WindowEvent arg0){}
public void mouseClicked(MouseEvent arg0){}
public void mousePressed(MouseEvent arg0){}
public void mouseReleased(MouseEvent arg0){}
public void mouseEntered(MouseEvent arg0){}
public void mouseExited(MouseEvent arg0){}