经典Java程序源代码.doc_第1页
经典Java程序源代码.doc_第2页
经典Java程序源代码.doc_第3页
经典Java程序源代码.doc_第4页
经典Java程序源代码.doc_第5页
已阅读5页,还剩131页未读 继续免费阅读

下载本文档

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

文档简介

经典Java程序源代码1.加法器(该java源文件的名称是Adder.java)import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Adder implements ActionListenerJFrame AdderFrame;JTextField TOprand1;JTextField TOprand2;JLabel LAdd,LSum;JButton BAdd,BClear;JPanel JP1,JP2;public Adder()AdderFrame=new JFrame(AdderFrame);TOprand1=new JTextField(0.0);TOprand2=new JTextField(0.0);LAdd=new JLabel(+);LSum=new JLabel(= );BAdd=new JButton(Add);BClear=new JButton(Clear);JP1=new JPanel();JP2=new JPanel();BAdd.addActionListener(this);BClear.addActionListener(new ActionListener()public void actionPerformed(ActionEvent event)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(new BorderLayout();AdderFrame.getContentPane().add(JP1,BorderLayout.NORTH);AdderFrame.getContentPane().add(JP2,BorderLayout.SOUTH);AdderFrame.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent event)System.exit(0););AdderFrame.pack();AdderFrame.setVisible(true);AdderFrame.setResizable(false);AdderFrame.setSize(250,100);public void actionPerformed(ActionEvent event)double sum=(double)(Double.valueOf(TOprand1.getText().doubleValue()+Double.valueOf(TOprand2.getText().doubleValue();LSum.setText(=+sum);public static void main(String args)Adder adder=new Adder();2. 小型记事本(该java源文件由两个类构成,名称为Notepad.java)import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;class mynotepad extends JFrame File file=null; Color color=Color.red; mynotepad() initTextContent(); initMenu(); initAboutDialog(); void initTextContent() 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(); /菜单栏的各个菜单项JMenu menus=new JMenunew JMenu(文件),new JMenu(编辑),new JMenu(关于); /文件菜单项的四个下拉菜单 /编辑菜单的四个下拉菜单 JMenuItem optionofmenu=new JMenuItemnew JMenuItem(新建),new JMenuItem(打开),new JMenuItem(保存),new JMenuItem(退出), new JMenuItem(复制),new JMenuItem(剪切),new JMenuItem(粘贴),new JMenuItem(颜色), new JMenuItem(关于) ; void initMenu() for(int i=0;imenus.length;i+) menu.add(menusi); for(int j=0;joptionofmenui.length;j+) menusi.add(optionofmenuij); optionofmenuij.addActionListener( action ); this.setJMenuBar(menu); ActionListener action=new ActionListener() /添加事件监听 public void actionPerformed(ActionEvent e) String name = e.getActionCommand();JMenuItem MI=(JMenuItem)e.getSource();if(新建.equals(name)content.setText();file=null;else if(打开.equals(name) if(file !=null) openfile.setSelectedFile(file); int returnVal=openfile.showOpenDialog(mynotepad.this); if(returnVal=JFileChooser.APPROVE_OPTION) file=openfile.getSelectedFile(); unfold(); else if(保存.equals(name) if(file!=null) openfile.setSelectedFile(file); int returnVal=openfile.showSaveDialog(mynotepad.this); if(returnVal=JFileChooser.APPROVE_OPTION) file=openfile.getSelectedFile(); saving(); else if(退出.equals(name) mynotepad f=new mynotepad(); int s=JOptionPane.showConfirmDialog(f,退出?,退出,JOptionPane.YES_NO_OPTION); if(s=JOptionPane.YES_OPTION) System.exit(0); else if(剪切.equals(name) content.cut(); else if(复制.equals(name) content.copy(); else if(粘贴.equals(name) content.paste(); else if(颜色.equals(name) color=JColorChooser.showDialog(mynotepad.this,color); content.setForeground(color); else if(关于.equals(name) about.setSize(300,150); about.show(); ; void saving() try FileWriter Writef=new FileWriter(file); Writef.write(content.getText(); Writef.close(); catch(Exception e) e.printStackTrace(); void unfold() try FileReader Readf=new FileReader(file); int len=(int)file.length(); char buffer=new charlen; Readf.read(buffer,0,len); Readf.close(); content.setText(new String(buffer); catch(Exception e) e.printStackTrace(); void initAboutDialog() about.setLayout(new GridLayout(3,1); about.getContentPane().setBackground(Color.white); about.getContentPane().add(new JLabel(我的记事本程序);/对话框内容 about.getContentPane().add(new JLabel(制作者:Fwx); about.getContentPane().add(new JLabel(2007年12月); about.setModal(true); /设置对话框前端显示 about.setSize(100,100); about.setLocation(250,170); /设置对话框显示位置 ; public class Notepadpublic static void main(String args) /入口main函数mynotepad noted=new mynotepad(); noted.addWindowListener(new WindowAdapter() ); noted.setTitle(我的记事本程序); /记事本标题noted.setSize(640,320); /设置记事本大小noted.show(); noted.setLocation(150,100); /设置记事本显示位置3. 简单计算器(该java源文件的名称是simplecalculator.java)import java.awt.*;import java.awt.event.*;import javax.swing.*;class simplecalculator static String point=new String();static String Amal=new String();static String ONE=new String();static String 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 ResultState=new String();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(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(-);static 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 BaiFen=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 void main(String args)JFrame frame=new JFrame(计算器);result.setEditable(false);result.setText();ResultState=窗口空;JPanel ForResult=new JPanel();JPanel ForButton7_clear=new JPanel();JPanel ForButton4_mul=new JPanel();JPanel ForButton1_sub=new JPanel();JPanel ForButton0_equal=new JPanel();FlowLayout FLO=new FlowLayout();ForResult.add(result);ForButton7_clear.setLayout(FLO);ForButton7_clear.add(seven);ForButton7_clear.add(eight);ForButton7_clear.add(nine);ForButton7_clear.add(div);ForButton7_clear.add(clear);ForButton4_mul.setLayout(FLO);ForButton4_mul.add(four);ForButton4_mul.add(five);ForButton4_mul.add(six);ForButton4_mul.add(mul);ForButton4_mul.add(BaiFen);ForButton1_sub.setLayout(FLO);ForButton1_sub.add(one);ForButton1_sub.add(two);ForButton1_sub.add(three);ForButton1_sub.add(sub);ForButton1_sub.add(FenZhiYi);ForButton0_equal.setLayout(FLO);ForButton0_equal.add(zero);ForButton0_equal.add(QuFan);ForButton0_equal.add(Dian);ForButton0_equal.add(add);ForButton0_equal.add(equal);frame.getContentPane().setLayout(FLO);frame.getContentPane().add(ForResult);frame.getContentPane().add(ForButton7_clear);frame.getContentPane().add(ForButton4_mul);frame.getContentPane().add(ForButton1_sub);frame.getContentPane().add(ForButton0_equal);frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);frame.setBounds(250,250,245,245);frame.setResizable(false);frame.setVisible(true);clear.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)result.setText();ZERO=;ONE=;TWO=;THREE=;FOUR=;FIVE=;SIX=;SEVEN=;EIGHT=;NINE=;ResultState=窗口空;point=;i=0;);zero.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)ZERO=已经点击;ResultState=窗口不为空;if(ONE=已经点击|TWO=已经点击|THREE=已经点击|FOUR=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击)result.append(0);if(ResultState=窗口空)result.setText(0););one.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)ONE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(1);if(ResultState=窗口空)result.setText(1););two.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)TWO=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(2);if(ResultState=窗口空)result.setText(2););three.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)THREE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(3);if(ResultState=窗口空)result.setText(3); );four.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)FOUR=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(4);if(ResultState=窗口空)result.setText(4););five.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)FIVE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(5);if(ResultState=窗口空)result.setText(6););six.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)SIX=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(6);if(ResultState=窗口空)result.setText(6););seven.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)SEVEN=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(7);if(ResultState=窗口空)result.setText(7););eight.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)EIGHT=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(8);if(ResultState=窗口空)result.setText(8););nine.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)NINE=已经点击;ResultState=窗口不为空;if(point=已经点击|ZERO!=已经点击|ONE=已经点击|TWO=已经点击|THREE=已经点击|FIVE=已经点击|SIX=已经点击|SEVEN=已经点击|EIGHT=已经点击|NINE=已经点击&result.getText()!=0)result.append(9);if(ResultState=窗口空)result.setText(9););Dian.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)point=已经点击;i=i+1;if(ResultState=窗口不为空&i=1)result.append(.););add.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择加号;addNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);sub.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择减号;subNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);mul.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择乘号;mulNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);div.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已经选择除号;divNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);QuFan.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)QF=new Double(Double.valueOf(result.getText().doubleValue();QF=QF*(-1);result.setText(QF.toString(););equal.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)equalNumber=Double.valueOf(result.getText().doubleValue();if(Amal=已经选择加号)temp=addNumber+equalNumber;result.setText(temp.toString();if(Amal=已经选择减号)temp=subNumber-equalNumber;result.setText(temp.toString();if(Amal=已经选择乘号)temp=mulNumber*equalNumber;result.setText(temp.toString();if(Amal=已经选择除号)temp=divNumber/equalNumber;result.setText(temp.toString(););BaiFen.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)if(ResultState=窗口不为空)temp=Double.valueOf(result.getText().doubleValue()/100;result.setText(temp.toString(););FenZhiYi.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)temp=1/(Double.valueOf(result.getText().doubleValue();result.setText(temp.toString(););4. 图形化写字板(该java源文件的名称是JNotePadUI.java)/JNotePadUI.javaimport java.awt.*;import java.awt.event.*;import java.awt.datatransfer.*;import java.io.*;import javax.swing.*;import javax.swing.text.*;public class JNotePadUI extends JPanel / 变量定义JTextArea jta = new JTextArea(, 24, 40);JScrollPane jsp = new JScrollPane(jta);/ 菜单条JMenuBar jmb = new JMenuBar();JMenu file = new JMenu(文件(F), true);JMenu edit = new JMenu(编辑(E), true);JMenu help = new JMenu(帮助(H), true);/ 工具条JToolBar toolBar = new JToolBar();/ 菜单内容JMenuItem jmi;/ 实例化剪切板Clipboard clipbd = getToolkit().getS

温馨提示

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

评论

0/150

提交评论