经典Java程序源代码1.加法器(该java源文件的名称是Adder.java)importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassAdderimplementsActionListener{JFrameAdderFrame;JTextFieldTOprand1;JTextFieldTOprand2;JLabelLAdd,LSum;JButtonBAdd,BClear;JPanelJP1,JP2;publicAdder(){AdderFrame=newJFrame(AdderFrame);TOprand1=newJTextField(0.0);TOprand2=newJTextField(0.0);LAdd=newJLabel(+);LSum=newJLabel(=);BAdd=newJButton(Add);BClear=newJButton(Clear);JP1=newJPanel();JP2=newJPanel();BAdd.addActionListener(this);BClear.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){TOprand1.setText(0.0);TOprand2.setText(0.0);LSum.setText(=);}});AdderFrame.add(JP1);JP1.add(TOprand1);JP1.add(LAdd);JP1.add(TOprand2);JP1.add(LSum);AdderFrame.add(JP2);JP2.add(BAdd);JP2.add(BClear);AdderFrame.getContentPane().setLayout(newBorderLayout());AdderFrame.getContentPane().add(JP1,BorderLayout.NORTH);AdderFrame.getContentPane().add(JP2,BorderLayout.SOUTH);AdderFrame.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventevent){System.exit(0);}});AdderFrame.pack();AdderFrame.setVisible(true);AdderFrame.setResizable(false);AdderFrame.setSize(250,100);}publicvoidactionPerformed(ActionEventevent){doublesum=(double)(Double.valueOf(TOprand1.getText()).doubleValue()+Double.valueOf(TOprand2.getText()).doubleValue());LSum.setText(=+sum);}publicstaticvoidmain(String[]args){Adderadder=newAdder();}}2.小型记事本(该java源文件由两个类构成,名称为Notepad.java)importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.io.*;classmynotepadextendsJFrame{Filefile=null;Colorcolor=Color.red;mynotepad(){initTextContent();initMenu();initAboutDialog();}voidinitTextContent(){getContentPane().add(newJScrollPane(content));}JTextPanecontent=newJTextPane();JFileChooseropenfile=newJFileChooser();JColorChooseropencolor=newJColorChooser();JDialogabout=newJDialog(this);JMenuBarmenu=newJMenuBar();//菜单栏的各个菜单项JMenu[]menus=newJMenu[]{newJMenu(文件),newJMenu(编辑),newJMenu(关于)};//文件菜单项的四个下拉菜单//编辑菜单的四个下拉菜单JMenuItemoptionofmenu[][]=newJMenuItem[][]{{newJMenuItem(新建),newJMenuItem(打开),newJMenuItem(保存),newJMenuItem(退出)},{newJMenuItem(复制),newJMenuItem(剪切),newJMenuItem(粘贴),newJMenuItem(颜色)},{newJMenuItem(关于)}};voidinitMenu(){for(inti=0;imenus.length;i++){menu.add(menus[i]);for(intj=0;joptionofmenu[i].length;j++){menus[i].add(optionofmenu[i][j]);optionofmenu[i][j].addActionListener(action);}}this.setJMenuBar(menu);}ActionListeneraction=newActionListener(){//添加事件监听publicvoidactionPerformed(ActionEvente){Stringname=e.getActionCommand();JMenuItemMI=(JMenuItem)e.getSource();if(新建.equals(name)){content.setText();file=null;}elseif(打开.equals(name)){if(file!=null)openfile.setSelectedFile(file);intreturnVal=openfile.showOpenDialog(mynotepad.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=openfile.getSelectedFile();unfold();}}elseif(保存.equals(name)){if(file!=null)openfile.setSelectedFile(file);intreturnVal=openfile.showSaveDialog(mynotepad.this);if(returnVal==JFileChooser.APPROVE_OPTION){file=openfile.getSelectedFile();saving();}}elseif(退出.equals(name)){mynotepadf=newmynotepad();ints=JOptionPane.showConfirmDialog(f,退出?,退出,JOptionPane.YES_NO_OPTION);if(s==JOptionPane.YES_OPTION)System.exit(0);}elseif(剪切.equals(name)){content.cut();}elseif(复制.equals(name)){content.copy();}elseif(粘贴.equals(name)){content.paste();}elseif(颜色.equals(name)){color=JColorChooser.showDialog(mynotepad.this,,color);content.setForeground(color);}elseif(关于.equals(name)){about.setSize(300,150);about.show();}}};voidsaving(){try{FileWriterWritef=newFileWriter(file);Writef.write(content.getText());Writef.close();}catch(Exceptione){e.printStackTrace();}}voidunfold(){try{FileReaderReadf=newFileReader(file);intlen=(int)file.length();char[]buffer=newchar[len];Readf.read(buffer,0,len);Readf.close();content.setText(newString(buffer));}catch(Exceptione){e.printStackTrace();}}voidinitAboutDialog(){about.setLayout(newGridLayout(3,1));about.getContentPane().setBackground(Color.white);about.getContentPane().add(newJLabel(我的记事本程序));//对话框内容about.getContentPane().add(newJLabel(制作者:Fwx));about.getContentPane().add(newJLabel(2007年12月));about.setModal(true);//设置对话框前端显示about.setSize(100,100);about.setLocation(250,170);//设置对话框显示位置};}publicclassNotepad{publicstaticvoidmain(Stringargs[]){//入口main函数mynotepadnoted=newmynotepad();noted.addWindowListener(newWindowAdapter(){});noted.setTitle(我的记事本程序);//记事本标题noted.setSize(640,320);//设置记事本大小noted.show();noted.setLocation(150,100);//设置记事本显示位置}}3.简单计算器(该java源文件的名称是simplecalculator.java)importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;classsimplecalculator{staticStringpoint=newString();staticStringAmal=newString();staticStringONE=newString();staticStringTWO=newString();staticStringTHREE=newString();staticStringFOUR=newString();staticStringFIVE=newString();staticStringSIX=newString();staticStringSEVEN=newString();staticStringEIGHT=newString();staticStringNINE=newString();staticStringZERO=newString();staticStringResultState=newString();staticDoubleQF;staticJButtonzero=newJButton