经典Java程序源代码.docx

上传人:b****3 文档编号:6241324 上传时间:2023-05-09 格式:DOCX 页数:114 大小:48.59KB
下载 相关 举报
经典Java程序源代码.docx_第1页
第1页 / 共114页
经典Java程序源代码.docx_第2页
第2页 / 共114页
经典Java程序源代码.docx_第3页
第3页 / 共114页
经典Java程序源代码.docx_第4页
第4页 / 共114页
经典Java程序源代码.docx_第5页
第5页 / 共114页
经典Java程序源代码.docx_第6页
第6页 / 共114页
经典Java程序源代码.docx_第7页
第7页 / 共114页
经典Java程序源代码.docx_第8页
第8页 / 共114页
经典Java程序源代码.docx_第9页
第9页 / 共114页
经典Java程序源代码.docx_第10页
第10页 / 共114页
经典Java程序源代码.docx_第11页
第11页 / 共114页
经典Java程序源代码.docx_第12页
第12页 / 共114页
经典Java程序源代码.docx_第13页
第13页 / 共114页
经典Java程序源代码.docx_第14页
第14页 / 共114页
经典Java程序源代码.docx_第15页
第15页 / 共114页
经典Java程序源代码.docx_第16页
第16页 / 共114页
经典Java程序源代码.docx_第17页
第17页 / 共114页
经典Java程序源代码.docx_第18页
第18页 / 共114页
经典Java程序源代码.docx_第19页
第19页 / 共114页
经典Java程序源代码.docx_第20页
第20页 / 共114页
亲,该文档总共114页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

经典Java程序源代码.docx

《经典Java程序源代码.docx》由会员分享,可在线阅读,更多相关《经典Java程序源代码.docx(114页珍藏版)》请在冰点文库上搜索。

经典Java程序源代码.docx

经典Java程序源代码

经典Java程序源代码

1.加法器(该java源文件的名称是Adder.java)

importjava.awt.*;

import;

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.*;

import;

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;i

{

menu.add(menus[i]);

for(intj=0;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.*;

import;

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("0");

staticJButtonone=newJButton("1");

staticJButtontwo=newJButton("2");

staticJButtonthree=newJButton("3");

staticJButtonfour=newJButton("4");

staticJButtonfive=newJButton("5");

staticJButtonsix=newJButton("6");

staticJButtonseven=newJButton("7");

staticJButtoneight=newJButton("8");

staticJButtonnine=newJButton("9");

staticJButtonadd=newJButton("+");

staticJButtonsub=newJButton("-");

staticJButtonmul=newJButton("*");

staticJButtondiv=newJButton("/");

staticJButtonQuFan=newJButton("+/-");

staticJButtonDian=newJButton(".");

staticJButtonequal=newJButton("=");

staticJButtonclear=newJButton("C");

staticJButtonBaiFen=newJButton("%");

staticJButtonFenZhiYi=newJButton("1/x");

staticinti=0;

staticDoubleaddNumber;

staticDoublesubNumber;

staticDoublemulNumber;

staticDoubledivNumber;

staticDoubleequalNumber;

staticDoubletemp;

staticJTextArearesult=newJTextArea(1,20);

publicstaticvoidmain(String[]args)

{

JFrameframe=newJFrame("计算器");

result.setEditable(false);

result.setText("");

ResultState="窗口空";

JPanelForResult=newJPanel();

JPanelForButton7_clear=newJPanel();

JPanelForButton4_mul=newJPanel();

JPanelForButton1_sub=newJPanel();

JPanelForButton0_equal=newJPanel();

FlowLayoutFLO=newFlowLayout();

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(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

result.setText("");

ZERO="";

ONE="";

TWO="";

THREE="";

FOUR="";

FIVE="";

SIX="";

SEVEN="";

EIGHT="";

NINE="";

ResultState="窗口空";

point="";

i=0;

}

});

zero.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

ZERO="已经点击";

ResultState="窗口不为空";

if(ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FOUR=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击")

{

result.append("0");

}

if(ResultState=="窗口空")

{

result.setText("0");

}

}

});

one.addActionListener(newActionListener()

{

publicvoidactionPerformed(ActionEvente)

{

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.addAct

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2