經(jīng)典Java程序源代碼[專業(yè)研究]_第1頁
經(jīng)典Java程序源代碼[專業(yè)研究]_第2頁
經(jīng)典Java程序源代碼[專業(yè)研究]_第3頁
經(jīng)典Java程序源代碼[專業(yè)研究]_第4頁
已閱讀5頁,還剩131頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、經(jīng)典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(AdderFr

2、ame);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 ev

3、ent)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,BorderL

4、ayout.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 actio

5、nPerformed(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源文件由兩個(gè)類構(gòu)成,名稱為Notepad.java)import java.awt.*;import java.awt.e

6、vent.*;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 openf

7、ile=new JFileChooser(); JColorChooser opencolor=new JColorChooser(); JDialog about=new JDialog(this); JMenuBar menu=new JMenuBar(); /菜單欄的各個(gè)菜單項(xiàng)JMenu menus=new JMenunew JMenu(文件),new JMenu(編輯),new JMenu(關(guān)于); /文件菜單項(xiàng)的四個(gè)下拉菜單 /編輯菜單的四個(gè)下拉菜單 JMenuItem optionofmenu=new JMenuItemnew JMenuItem(新建),new JMenuItem

8、(打開),new JMenuItem(保存),new JMenuItem(退出), new JMenuItem(復(fù)制),new JMenuItem(剪切),new JMenuItem(粘貼),new JMenuItem(顏色), new JMenuItem(關(guān)于) ; void initMenu() for(int i=0;imenus.length;i+) menu.add(menusi); for(int j=0;joptionofmenui.length;j+) menusi.add(optionofmenuij); optionofmenuij.addActionListener( a

9、ction ); this.setJMenuBar(menu); ActionListener action=new ActionListener() /添加事件監(jiān)聽 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.setSe

10、lectedFile(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=JFi

11、leChooser.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(復(fù)制.equals(nam

12、e) content.copy(); else if(粘貼.equals(name) content.paste(); else if(顏色.equals(name) color=JColorChooser.showDialog(mynotepad.this,color); content.setForeground(color); else if(關(guān)于.equals(name) about.setSize(300,150); about.show(); ; void saving() try FileWriter Writef=new FileWriter(file); Writef.wri

13、te(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.printStackTrac

14、e(); void initAboutDialog() about.setLayout(new GridLayout(3,1); about.getContentPane().setBackground(Color.white); about.getContentPane().add(new JLabel(我的記事本程序);/對(duì)話框內(nèi)容 about.getContentPane().add(new JLabel(制作者:Fwx); about.getContentPane().add(new JLabel(2007年12月); about.setModal(true); /設(shè)置對(duì)話框前端顯示

15、about.setSize(100,100); about.setLocation(250,170); /設(shè)置對(duì)話框顯示位置 ; public class Notepadpublic static void main(String args) /入口main函數(shù)mynotepad noted=new mynotepad(); noted.addWindowListener(new WindowAdapter() ); noted.setTitle(我的記事本程序); /記事本標(biāo)題noted.setSize(640,320); /設(shè)置記事本大小noted.show(); noted.setLoc

16、ation(150,100); /設(shè)置記事本顯示位置3. 簡(jiǎn)單計(jì)算器(該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 THRE

17、E=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;stat

18、ic 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

19、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 JB

20、utton 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

21、,20);public static void main(String args)JFrame frame=new JFrame(計(jì)算器);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

22、();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(

23、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);For

24、Button0_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(For

25、Button1_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

26、=;ONE=;TWO=;THREE=;FOUR=;FIVE=;SIX=;SEVEN=;EIGHT=;NINE=;ResultState=窗口空;point=;i=0;);zero.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)ZERO=已經(jīng)點(diǎn)擊;ResultState=窗口不為空;if(ONE=已經(jīng)點(diǎn)擊|TWO=已經(jīng)點(diǎn)擊|THREE=已經(jīng)點(diǎn)擊|FOUR=已經(jīng)點(diǎn)擊|FIVE=已經(jīng)點(diǎn)擊|SIX=已經(jīng)點(diǎn)擊|SEVEN=已經(jīng)點(diǎn)擊|EIGHT=已經(jīng)點(diǎn)擊|NINE=已經(jīng)點(diǎn)擊)result.a

27、ppend(0);if(ResultState=窗口空)result.setText(0););one.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)ONE=已經(jīng)點(diǎn)擊;ResultState=窗口不為空;if(point=已經(jīng)點(diǎn)擊|ZERO!=已經(jīng)點(diǎn)擊|ONE=已經(jīng)點(diǎn)擊|TWO=已經(jīng)點(diǎn)擊|THREE=已經(jīng)點(diǎn)擊|FIVE=已經(jīng)點(diǎn)擊|SIX=已經(jīng)點(diǎn)擊|SEVEN=已經(jīng)點(diǎn)擊|EIGHT=已經(jīng)點(diǎn)擊|NINE=已經(jīng)點(diǎn)擊&result.getText()!=0)result.append(1

28、);if(ResultState=窗口空)result.setText(1););two.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)TWO=已經(jīng)點(diǎn)擊;ResultState=窗口不為空;if(point=已經(jīng)點(diǎn)擊|ZERO!=已經(jīng)點(diǎn)擊|ONE=已經(jīng)點(diǎn)擊|TWO=已經(jīng)點(diǎn)擊|THREE=已經(jīng)點(diǎn)擊|FIVE=已經(jīng)點(diǎn)擊|SIX=已經(jīng)點(diǎn)擊|SEVEN=已經(jīng)點(diǎn)擊|EIGHT=已經(jīng)點(diǎn)擊|NINE=已經(jīng)點(diǎn)擊&result.getText()!=0)result.append(2);if(Re

29、sultState=窗口空)result.setText(2););three.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)THREE=已經(jīng)點(diǎn)擊;ResultState=窗口不為空;if(point=已經(jīng)點(diǎn)擊|ZERO!=已經(jīng)點(diǎn)擊|ONE=已經(jīng)點(diǎn)擊|TWO=已經(jīng)點(diǎn)擊|THREE=已經(jīng)點(diǎn)擊|FIVE=已經(jīng)點(diǎn)擊|SIX=已經(jīng)點(diǎn)擊|SEVEN=已經(jīng)點(diǎn)擊|EIGHT=已經(jīng)點(diǎn)擊|NINE=已經(jīng)點(diǎn)擊&result.getText()!=0)result.append(3);if(Resul

30、tState=窗口空)result.setText(3); );four.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)FOUR=已經(jīng)點(diǎn)擊;ResultState=窗口不為空;if(point=已經(jīng)點(diǎn)擊|ZERO!=已經(jīng)點(diǎn)擊|ONE=已經(jīng)點(diǎn)擊|TWO=已經(jīng)點(diǎn)擊|THREE=已經(jīng)點(diǎn)擊|FIVE=已經(jīng)點(diǎn)擊|SIX=已經(jīng)點(diǎn)擊|SEVEN=已經(jīng)點(diǎn)擊|EIGHT=已經(jīng)點(diǎn)擊|NINE=已經(jīng)點(diǎn)擊&result.getText()!=0)result.append(4);if(ResultSta

31、te=窗口空)result.setText(4););five.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)FIVE=已經(jīng)點(diǎn)擊;ResultState=窗口不為空;if(point=已經(jīng)點(diǎn)擊|ZERO!=已經(jīng)點(diǎn)擊|ONE=已經(jīng)點(diǎn)擊|TWO=已經(jīng)點(diǎn)擊|THREE=已經(jīng)點(diǎn)擊|FIVE=已經(jīng)點(diǎn)擊|SIX=已經(jīng)點(diǎn)擊|SEVEN=已經(jīng)點(diǎn)擊|EIGHT=已經(jīng)點(diǎn)擊|NINE=已經(jīng)點(diǎn)擊&result.getText()!=0)result.append(5);if(ResultState=窗口

32、空)result.setText(6););six.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)SIX=已經(jīng)點(diǎn)擊;ResultState=窗口不為空;if(point=已經(jīng)點(diǎn)擊|ZERO!=已經(jīng)點(diǎn)擊|ONE=已經(jīng)點(diǎn)擊|TWO=已經(jīng)點(diǎn)擊|THREE=已經(jīng)點(diǎn)擊|FIVE=已經(jīng)點(diǎn)擊|SIX=已經(jīng)點(diǎn)擊|SEVEN=已經(jīng)點(diǎn)擊|EIGHT=已經(jīng)點(diǎn)擊|NINE=已經(jīng)點(diǎn)擊&result.getText()!=0)result.append(6);if(ResultState=窗口空)resul

33、t.setText(6););seven.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)SEVEN=已經(jīng)點(diǎn)擊;ResultState=窗口不為空;if(point=已經(jīng)點(diǎn)擊|ZERO!=已經(jīng)點(diǎn)擊|ONE=已經(jīng)點(diǎn)擊|TWO=已經(jīng)點(diǎn)擊|THREE=已經(jīng)點(diǎn)擊|FIVE=已經(jīng)點(diǎn)擊|SIX=已經(jīng)點(diǎn)擊|SEVEN=已經(jīng)點(diǎn)擊|EIGHT=已經(jīng)點(diǎn)擊|NINE=已經(jīng)點(diǎn)擊&result.getText()!=0)result.append(7);if(ResultState=窗口空)result.s

34、etText(7););eight.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)EIGHT=已經(jīng)點(diǎn)擊;ResultState=窗口不為空;if(point=已經(jīng)點(diǎn)擊|ZERO!=已經(jīng)點(diǎn)擊|ONE=已經(jīng)點(diǎn)擊|TWO=已經(jīng)點(diǎn)擊|THREE=已經(jīng)點(diǎn)擊|FIVE=已經(jīng)點(diǎn)擊|SIX=已經(jīng)點(diǎn)擊|SEVEN=已經(jīng)點(diǎn)擊|EIGHT=已經(jīng)點(diǎn)擊|NINE=已經(jīng)點(diǎn)擊&result.getText()!=0)result.append(8);if(ResultState=窗口空)result.setT

35、ext(8););nine.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)NINE=已經(jīng)點(diǎn)擊;ResultState=窗口不為空;if(point=已經(jīng)點(diǎn)擊|ZERO!=已經(jīng)點(diǎn)擊|ONE=已經(jīng)點(diǎn)擊|TWO=已經(jīng)點(diǎn)擊|THREE=已經(jīng)點(diǎn)擊|FIVE=已經(jīng)點(diǎn)擊|SIX=已經(jīng)點(diǎn)擊|SEVEN=已經(jīng)點(diǎn)擊|EIGHT=已經(jīng)點(diǎn)擊|NINE=已經(jīng)點(diǎn)擊&result.getText()!=0)result.append(9);if(ResultState=窗口空)result.setText(9

36、););Dian.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)point=已經(jīng)點(diǎn)擊;i=i+1;if(ResultState=窗口不為空&i=1)result.append(.););add.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已經(jīng)選擇加號(hào);addNumber=Double.valueOf(result.getText().doubleValu

37、e();result.setText();i=0;);sub.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已經(jīng)選擇減號(hào);subNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);mul.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已經(jīng)選擇乘號(hào);

38、mulNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);div.addActionListener( new ActionListener()public void actionPerformed(ActionEvent e)Amal=已經(jīng)選擇除號(hào);divNumber=Double.valueOf(result.getText().doubleValue();result.setText();i=0;);QuFan.addActionListener( new ActionListener()

39、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=已經(jīng)選

40、擇加號(hào))temp=addNumber+equalNumber;result.setText(temp.toString();if(Amal=已經(jīng)選擇減號(hào))temp=subNumber-equalNumber;result.setText(temp.toString();if(Amal=已經(jīng)選擇乘號(hào))temp=mulNumber*equalNumber;result.setText(temp.toString();if(Amal=已經(jīng)選擇除號(hào))temp=divNumber/equalNumber;result.setText(temp.toString(););BaiFen.addActionL

41、istener( 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.

42、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 / 變量定義JTextAr

43、ea 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();/ 菜單內(nèi)容JMenuItem jmi;/ 實(shí)例化剪切板Clipboard clipbd = getToolkit().ge

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論