Java程序源代码_第1页
Java程序源代码_第2页
Java程序源代码_第3页
Java程序源代码_第4页
Java程序源代码_第5页
已阅读5页,还剩63页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、经典Java程序源代码1 .加法器(该java源文件的名称是)import .*;import .*;public class Adder implements ActionListener(JFrame AdderFrame;JTextField TOprand1;JTextField TOprand2;JLabel LAdd,LSum;JButton BAdd,BClear;JPanel JP1,JP2;public Adder()(AdderFrame=new JFrame("AdderFrame");TOprand1=new JTextField("&qu

2、ot;);TOprand2=new JTextField("");LAdd=new JLabel("+");LSum=new JLabel("= ");BAdd=new JButton("Add");BClear=new JButton("Clear");JP1=new JPanel();JP2=new JPanel();(this);(new ActionListener()(public void actionPerformed(ActionEvent event) (""

3、;);("");("="););(JP1);(TOprandl);(LAdd);(TOprand2);(LSum);(JP2);(BAdd);(BCIear);().setLayout(new BorderLayout();().add(JP1,;().add(JP2,;(new WindowAdapterQpublic void windowClosing(WindowEvent event)(0););0;(true);(false);(250,100);)public void actionPerformed(ActionEvent event)d

4、ouble sum=(double)().doubleValue()+().doubleValue(); ("="+sum);)public static void main(String args)Adder adder=new Adder();2 .小型记事本该java源文件由两个类构成,importimportimportclass mynotepad extends JFrame (File file=null;Color color=;mynotepadQinitTextContent();initMenu();initAboutDialogQ;void init

5、TextContent()getContentPane().add(new JScrollPane(content); )JTextPane content=new JTextPane();JFileChooser openfile=new JFileChooser();JColorChooser opencolor=new JColorChooser();JDialog about=new JDialog(this);名称为JMenuBar menu=new JMenuBar();ength;j+)(menusi.add(optionofmenuij);optionofmenuij.addA

6、ctionListener( action);)(menu);)ActionListener action=new ActionListener()quals(name)("");file=null;)else if("翻开".equals(name)(if(file !=null)(file);int returnVal=;if(returnVal=file=();unfoldQ;)else if("保存".equals(name)(if(file!=null) (file);int returnVal=;if(returnVal=

7、 (file=();saving();)else if("退出".equals(name)(mynotepad f=new mynotepad();ints=(f,"退出? 退出】if(s=(0);)else if("剪切".equals(name)(0;)else if("复制".equals(name)0;)else if("粘贝占".equals(name)(0;)else if("颜色".equals(name)(color=,"",color);(colo

8、r);)else if("关于".equals(name)(300,150);0;)void saving()tryFileWriter Writef=new FileWriter(file);0);0;)catch(Exception e)(0;)void unfold()(try(FileReader Readf=new FileReader(file); int len=(int)();char buffer=new charlen;(buffer,0,len);0;(new String(buffer);)catch(Exception e)0;void initA

9、boutDialogQ(new GridLayout(3,1);O.setBackground;().add(new JLabel("我的记事本程序");dd(new JLabel("制 Fwx");().add(new JLabel("2007 年 12 月); (true);importclass simplecalculator (static String point=new String();static String Amal=new String();static String ONE=new String();static St

10、ring TWO=new String();static String THREE=new String();static String FOUR=new String();static String FIVE=new String();static String SIX=new String();static String SEVEN=new String();static String EIGHT=new String();static String NINE=new String();static String ZERO=new String();static String Result

11、State=new StringQ;static Double QF;static JButton zero=new JButton("0");static JButton one=new JButton("1");static JButton two=new JButton("2");static JButton three=new JButton("3");static JButton four=new JButton("4");static JButton five=new JButton

12、("5");static JButton six=new JButton("6");static JButton seven=new JButton("7");static JButton eight=new JButton("8");static JButton nine=new JButton("9");static JButton add=new JButton("+");static JButton sub=new JButton("-");sta

13、tic JButton mul=new JButton("*");static JButton div=new JButton("/");static JButton QuFan=new JButton("+/-");static JButton Dian=new JButton(".");static JButton equal=new JButton("=");static JButton clear=new JButton("C");static JButton Bai

14、Fen=new JButton("%");static JButton FenZhiYi=new JButton("1/x");static int i=0;static Double addNumber;static Double subNumber;static Double mulNumber;static Double divNumber;static Double equalNumber;static Double temp;static JTextArea result=new JTextArea(1,20);public static vo

15、id main(String args)(JFrame frame=new JFrame(“计算器");(false);("");ResultState="窗口空JPanel ForResult=new JPanel();JPanel ForButton7_clear=new JPanel();JPanel ForButton4_mul=new JPanel();JPanel ForButton1_sub=new JPanel();JPanel ForButton0_equaI=new JPanel();FlowLayout FLO=new FlowLa

16、yout();(result);(FLO);(seven);(eight);(nine);(div);(clear);(FLO);(four);(five);(six);(mul);(BaiFen);(FLO);(one);(two);(three);(sub);(FenZhiYi);(FLO);(zero);(QuFan);(Dian);(add);(equal);().setLayout(FLO);().add(ForResult);().add(ForButton7_clear);().add(ForButton4_mul);().add(ForButton1_sub);().add(F

17、orButtonO_equal);(250,250,245,245);(false);(true);(new ActionListener()(public void action Performed (Action Event e) ("");ZERO=""ONE=""TWO=""THREE*FOUR=""FIVE=""SIX=""SEVEN=""EIGHT*NINE=""ResultState="窗口空

18、point=""i=0;);(new ActionListener()(public void actionPerformed(ActionEvent e)(ZERO="已经点击ResultState="窗口不为空if(ONE="已经点击"|TWO="已经点击"|THREE="已经点击 已经点击"|EIGHT="已经点击"|NINE="已经点击")("0");)if(ResultState="窗口空)("0"

19、;););(new ActionListener()(public void actionPerformed(ActionEvent e)(ONE="已经点击ResultState="窗口不为空"|FOUR="已经点击"|FIVE="已经点击"|SIX="已经点击"|SEVEN="if(point="已经点击"|ZERO!="已经点击"|ONE="已经点击"|TWO="已经点击|THREE="已经点击|FIVE=&q

20、uot;已经点击"|SIX="已经点击 |SEVEN=" 已经点击"|EIGHT="已经点击"|NINE="已经点击"&&()!="0")if(ResultState="窗口空");(new ActionListenerQpublic void actionPerformed(ActionEvent e)TWO="已经点击Resultstate二"窗口不为空if(point="已经点击"|ZERO!="已经点击

21、"|ONE="已经点击"|TWO="已经点击|THREE="已经点击"|FIVE="已经点击"|SIX="已经点击 |SEVEN=" 已经点击"|EIGHT="已经点击"|NINE="已经点击"&&()!="0")if(ResultState="窗口空");(new ActionListener()(public void actionPerformed(ActionEvent e)(THR

22、EE="已经点击ResultState="窗口不为空if(point="已经点击"|ZERO!="已经点击"|ONE="已经点击"|TWO="已经点击"|THREE="已经点击"|FIVE="已经点击"|SIX="已经 点击 |SEVEN=" 已经点击"|EIGHT="已经点击"|NINE="已经点击"&&()!="0")("3")

23、;)if(ResultState="窗口空)("3"););(new ActionListener()(public void actionPerformed(ActionEvent e)(FOUR="已经点击ResultState="窗口不为空if(point="已经点击"|ZERO!="已经点击"|ONE="已经点击"|TWO="已经点击"|THREE="已经点击"|FIVE="已经点击"|SIX="已经点击|S

24、EVEN="已经点击"|EIGHT="已经点击"|NINE="已经点击"&&()!="0") ("4");) if(ResultState="窗口 空") ("4"););(new ActionListener()(public void actionPerformed(ActionEvent e)(FIVE="已经点击"ResultState="窗口不为空"if(point="已经点击&q

25、uot;|ZERO!="已经点击"|ONE="已经点击"|TWO="已经点击"|THREE="已经点击"|FIVE="已经点击"|SIX="已经 点击|SEVEN="已经点击"|EIGHT="已经点击"|NINE="已经点击"&&()!="0")("5");if(ResultState="窗口 空")("6"););(new Act

26、ionListener()(public void actionPerformed(ActionEvent e)(SIX="已经点击ResultState="窗口不为空if(point="已经点击"|ZERO!="已经点击"|ONE="已经点击"|TWO="已经点击"|THREE="已经点击"|FIVE="已经点击"|SIX="已经 点击"|SEVEN="已经点击"|EIGHT="已经点击"|NI

27、NE="已经点击"&&()!="0")("6");)if(ResultState="窗口空)("6"););(new ActionListener()(public void actionPerformed(ActionEvent e)(SEVEN="已经点击ResultState="窗口不为空if(point="已经点击"|ZERO!="已经点击"|ONE="已经点击"|TWO="已经点击"

28、;|THREE="已经点击"|FIVE="已经点击"|SIX="已经 点击 |SEVEN=" 已经点击"|EIGHT="已经点击"|NINE="已经点击"&&()!="0") if(ResultState="窗口 空") ("7"););(new ActionListener()(public void actionPerformed(ActionEvent e)(EIGHT="已经点击"R

29、esultState="窗口不为空"if(point="已经点击"|ZERO!="已经点击"|ONE="已经点击"|TWO="已经点击"|THREE="已经点击"|FIVE="已经点击"|SIX="已经 点击|SEVEN="已经点击"|EIGHT="已经点击"|NINE="已经点击&&()!="0")("8"); if(ResultState

30、="窗口 空") ("8"););(new ActionListenerQpublic void actionPerformed(ActionEvent e)(NINE="已经点击ResultState="窗口不为空if(point="已经点击"|ZERO!="已经点击"|ONE="已经点击"|TWO="已经点击"|THREE="已经点击"|FIVE="已经点击"|SIX="已经 点击 |SEVEN=&qu

31、ot; 已经点击"|EIGHT="已经点击"|NINE="已经点击"&&()!="0")("9");)if(ResultState="窗口空)( ("9"););(new ActionListenerQ(public void actionPerformed(ActionEvent e)(point="已经点击i=i+1;if(ResultState="窗口不为空"&&i=1) );(new ActionListe

32、ner()(public void action Performed (Action Event e) (Amal="已经选择加号addNumber=().doubleValue();("");i=0;);(new ActionListener()(public void action Performed (Action Event e) (Amal="已经选择减号subNumber=().doubleV alue();("");i=0;);(new ActionListener()(public void action Perfor

33、med (Action Event e) (Amal="已经选择乘号mulNumber=().doubleValue();("");i=0;);(new ActionListener()(public void action Performed (Action Event e) (Amal="已经选择除号divNumber=().doubleValue();i=0;);(new ActionListenerQpublic void action Performed (Action Event e)QF=new Double().doubleV alue(

34、);QF=QF*(-1);0););(new ActionListener()(public void action Performed (Action Event e)(equalNumber=().doubleV alue(); if(Amal="已经选择加号") (temp=addNumber+equalNumber;0);)if(Amal="已经选择减号")(temp=subNumber-equalNumber;0);)if(Amal="已经选择乘号")(temp=mulNumber*equalNumber;0);)if(Am

35、al="已经选择除号")(temp=divNumber/equalNumber;0););(new ActionListener()(public void action Performed (Action Event e) (if(ResultState="窗口不为空")(temp=().doubleValue()/100;0););(new ActionListener()(public void action Performed (Action Event e)temp=1/().doubleValue();0););)3 .图形化写字板(该jav

36、a源文件的名称是)Jimportimportimport class JNotePadUI extends JPanel (etSystemClipboard();String text =etPath();if (file = null) (return;) etPath();if (savefile = null)return;)else(String docToSave =();if (docToSave != null)(FileOutputStream fstrm = null;BufferedOutputStream ostrm = null;try(fstrm = new Fil

37、eOutputStream(savefile);ostrm = new BufferedOutputStream(fstrm);byte bytes = null;try(bytes =();)catch (Exception e1)(0;)(bytes);)catch (lOException io) else(return;) etVisible(true);"lOException:"+ ();)finally(try(0;0;0;)catch (lOException ioe)"lOException:"+ ();(0);dd(applet,;(

38、new appCloseL();(800, 500);(500,170);(true);0;(false);)4 .数字化的连连看(该java源文件的名称是)importimportimportpublic class lianliankan implements ActionListener(JFrame mainFrame; ddActionListener(this);(diamondsButtoncolsrows);)exitButton=new JButton("退出");(this);resetButton=new JButton("重歹卜')

39、;(this);newlyButton=new JButton("再来局");(this);(exitButton);(resetButton);(newly Button);();(fraction Lable);(280,100,500,450);(true);)public void randomBuild()(int randoms,cols,rows;for(int twins=1 ;twins<=15;twins+)(randoms=(int)()*25+1);for(int alike=1 ;alike<=2;alike+) (cols=(int)

40、()*6+1);rows=(int)()*5+1);while(gridcolsrows!=0) (cols=(int)()*6+1);rows=(int)()*5+1);)colsrows=randoms;)public void fraction()(0)+100);)public void reload()(int save = new int30;int n=0,cols,rows;int grid= new int87;for(int i=0;i<=6;i+)(for(int j=0;j<=5;j+)(ifij!=O)(saven=ij;n+;)n=n-1;=grid;w

41、hile(n>=0)(cols=(int)()*6+1);rows=(int)()*5+1);while(gridcolsrows!=0)(cols=(int)()*6+1);rows=(int)()*5+1);)colsrows=saven;n-;)(false);presslnformation=false; etVisible(false);),J Button bz)public void estimateEven(int placeXJnt placeY (if(presslnformation=false)(x=placeX;y=placeY;secondMsg=gridxy

42、;secondButton=bz; presslnformation=true;) else (xO=x;yO=y;fristMsg=secondMsg;firstButton=secondButton;x=placeX;y=placeY;secondMsg=gridxy;secondButton=bz;if(fristMsg=secondMsg && second Button !=firstButton) (xiao();)public void xiao() ;import importimport MenuDemo extends JPanel implements A

43、ctionListener(JTextArea JTA=new JTextArea(13,22);JFrame frame=new JFrame("The Editor");JScrollPane JSP=new JScrollPane(JTA);JFileChooser JFC=new JFileChooser();Clipboard clipbd = getToolkit().getSystemClipboard();int JFC_ReturnValue;String selection;MenuDemo()(JMenuBar jmb=new JMenuBar();J

44、Menu file= new JMenu("File");JMenu edit= new JMenu("Edit");JMenu search= new JMenu("Search");JMenu configure= new JMenu("Configure");JMenu help= new JMenu("Help");(file);(edit);(search);(configure);(help);JMenultem NEW=new JMenultem("New");

45、JMenultem OPEN=new JMenultem("Open");JMenultem SAVE=new JMenultem("Save As");JMenultem EXIT=new JMenultem("Exit");(NEW);(this);(OPEN);(this);(SAVE);(this);(EXIT);(this);JMenultem CUT=new JMenultem("Cut");JMenultem COPY=new JMenultem("Copy");JMenultem

46、 PASTE=new JMenultem("Paste");JMenultem DELETE=new JMenultem("Delete");(CUT);(this);(COPY);(this);(PASTE);(this);(DELETE);(this);JMenultem FIND=new JMenultem("Find");JMenultem REPLACE=new JMenultem("Replace");JMenultem BOOK_MARKS=new JMenultem("Bookmarks&

47、quot;);(FIND);(REPLACE);(BOOK_MARKS);JMenu SET_COLOR=new JMenu("Set Color");JMenuItem SET_BACK_GROUND_COLOR=new JMenuItem("SetBackGroundColor");JMenuItem SET_FONT_COLOR=new JMenuItem("SetFontColor");(SET_BACK_GROUND_COLOR);(this);(SET_FONT_COLOR);(this);JMenu SET_FONT=n

48、ew JMenu("Set Font");JMenuItem SET_FONT_SIZE=new JMenuItem("SetFontSize");JMenuItem SET_FONT_BLOCK=new JMenuItem("SetFontBlock");(SET_FONT_SIZE);(SET_FONT_BLOCK);(SET_COLOR);(SET_FONT);JMenuItem HELP_CONTENTS=new JMenuItem("HelpContents");JMenuItem ABOUT_THE_P

49、ROGRAM=new JMenuItem("AboutTheProgram");(HELP_CONTENTS);(ABOUT_THE_PROGRAM);(this);(true);(true);().setLayout(new FlowLayout();().add(jmb);().add(JSP);();(252,300);(400,250);(false);J)public static void main(String args)(MenuDemo MD=new MenuDemo();)public void actionPerformed(ActionEvent e

50、)(if()="New")(int s=(frame,"Are you sure to build new one?n","New",; if(s=("");)if()="Open")JFC_ReturnValue=(frame);if(JFC_ReturnValue=String file=().getPath();if(file=null)(return;)try(Reader in = new FileReader(file);char buff = new char4096;int nc

51、h;while (nch = (buff, 0,) != -1) (new PlainDocumentQ);(new String(buff, 0, nch);)catch (lOException io)("lOException:" + ();)else(return;if()="Save As")(JFC_ReturnValue=(frame);if(JFC_ReturnValue= (String savefile = ().getPath();if (savefile = null) (return;) else(String docToSav

52、e =();if (docToSave != null) (FileOutputStream fstrm = null;BufferedOutputStream ostrm = null;try(fstrm = new FileOutputStream(savefile);ostrm = new BufferedOutputStream(fstrm); byte bytes = null;trybytes =();)catch (Exception e1)(0;)(bytes);)catch (lOException io)("lOException:"+ ();) fin

53、ally(try(0;0;0;)catch (lOException ioe)"lOException:"+ ();)else(return;)if()="Exit")(int s=(frame,"Are you sure to exit?","Exit",;if(s=(0);)if()="Cut")(selection=();StringSelection clipString = new StringSelection(selection);(clipString, clipString);

54、("),();) if()="Copy")selection=();StringSelection clipString = new StringSelection(selection);(clipString, clipString);if()="Paste") (frame);(String);Transferable cl ip Data = try (String clipString = (clipString,(),();) catch (Exception ex)if()="Delete") (,(),();i

55、f()="SetBackGroundColor")Color color = (this, "ChooseBackGroundColor",; if (color != null) (color);)elsereturn;)if()="SetFontColor")(Color color = (this, "ChooseBackGroundColor",; if (color != null) (color);)elsereturn;)if()="HelpContents")() if()=&q

56、uot;AboutTheProgram")(JTA,"Hello World!","AboutTheProgram",;5.小型抽奖系统导入 号和姓名该 java源文件的名称是在源代码之前告诉大家一声,导入的文件格式是叶尔凯西-革命-热依古丽-杰格尔-小叶-大爷-小花-系统最少要输入7名用户,三等奖3名,二等奖2名,txt文本格式,文件的内容可以有上千百万个用户和 号构成,格式如下:等奖1名,特等奖1名;下面是Java源代码import .*; import import .*; import .*;*public class ChooseAward extends JFrame (/*/new JMenuItem("保存"),new JMenuItem("退出");JTextField t_identNumber = new JTextField(); ddActionListener(new ActionListener() (ddActionListener(new ActionListener() (."); .(awardThread = new ChooseThread(this);();("将产生:特(1名),一 (1名),

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论