Java计算器实验报告材料Word文档下载推荐.docx

上传人:b****5 文档编号:19252539 上传时间:2023-01-04 格式:DOCX 页数:23 大小:169.40KB
下载 相关 举报
Java计算器实验报告材料Word文档下载推荐.docx_第1页
第1页 / 共23页
Java计算器实验报告材料Word文档下载推荐.docx_第2页
第2页 / 共23页
Java计算器实验报告材料Word文档下载推荐.docx_第3页
第3页 / 共23页
Java计算器实验报告材料Word文档下载推荐.docx_第4页
第4页 / 共23页
Java计算器实验报告材料Word文档下载推荐.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

Java计算器实验报告材料Word文档下载推荐.docx

《Java计算器实验报告材料Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Java计算器实验报告材料Word文档下载推荐.docx(23页珍藏版)》请在冰豆网上搜索。

Java计算器实验报告材料Word文档下载推荐.docx

importjavax.swing.JMenu;

importjavax.swing.JMenuBar;

importjavax.swing.JMenuItem;

importjavax.swing.JPanel;

importjavax.swing.JScrollPane;

importjavax.swing.JTextArea;

importjavax.swing.JTextField;

publicclassjsqimplementsActionListener{//导入动作监听接口

//设计面板中的单位

JFrameframe;

//窗口

JTextFieldtextAnswer;

//文本

JPanelpanel,panel1,panel2,panel3;

//面版

JMenuBarmainMenu;

//菜单

JTextFieldtextMemory;

JLabellabelMemSpace;

//labelMemSpace单纯做摆设,控制面板的形状

JButtonbuttonBk,buttonCe,buttonC;

JButtonbutton[];

//按钮数组

JButtonbuttonMC,buttonMR,buttonMS,buttonMAdd;

//清除,使用,储存,添加

JButtonbuttonDot,buttonAddAndSub,buttonAdd,buttonSub,buttonMul,

buttonDiv,buttonMod;

JButtonbuttonSqrt,buttonDao,buttonEqual;

JMenueditMenu,viewMenu,helpMenu;

JMenuItemcopyItem,pasteItem,tItem,sItem,numberGroup,topHelp,

aboutCal;

DecimalFormatdf;

//设置数据输出精度

booleanclickable;

//控制当前能否按键

doublememoryd;

//使用内存中存储的数字

intmemoryi;

doublevard,answerd;

//用来保存double型数据的中间值(vard)和最后结果(answerd)

shortkey=-1,prekey=-1;

//key用来保存当前进行何种运算,prekey用来保存前次进行何种运算

Stringcopy;

//做复制用

JTextAreahelp;

//帮助

JScrollPanescrollHelp;

//构造函数

publicjsq(){

clickable=true;

answerd=0;

frame=newJFrame("

计算器"

);

df=newDecimalFormat("

0.##############"

//设置数据输出精度(对于double型值)

textAnswer=newJTextField(15);

textAnswer.setText("

"

textAnswer.setEditable(false);

textAnswer.setBackground(newColor(255,255,255));

panel=newJPanel();

frame.getContentPane().add(panel);

panel1=newJPanel();

panel2=newJPanel();

panel.setLayout(newBorderLayout());

//设计整个面板

mainMenu=newJMenuBar();

editMenu=newJMenu("

编辑(E)"

editMenu.setMnemonic(KeyEvent.VK_E);

viewMenu=newJMenu("

查看(V)"

viewMenu.setMnemonic(KeyEvent.VK_V);

helpMenu=newJMenu("

帮助(H)"

helpMenu.setMnemonic(KeyEvent.VK_H);

mainMenu.add(editMenu);

mainMenu.add(viewMenu);

mainMenu.add(helpMenu);

panel.add(mainMenu,BorderLayout.NORTH);

panel.add(textAnswer,BorderLayout.CENTER);

panel.add(panel1,BorderLayout.SOUTH);

panel1.setLayout(newBorderLayout());

textMemory=newJTextField(3);

textMemory.setEditable(false);

textMemory.setBackground(newColor(217,217,217));

labelMemSpace=newJLabel("

"

buttonBk=newJButton("

Backspace"

buttonBk.setForeground(newColor(255,0,0));

buttonCe=newJButton("

CE"

buttonCe.setForeground(newColor(255,0,0));

buttonC=newJButton("

C"

buttonC.setForeground(newColor(255,0,0));

buttonBk.addActionListener(this);

buttonCe.addActionListener(this);

buttonC.addActionListener(this);

panel1.add(panel2,BorderLayout.NORTH);

panel2.setLayout(newFlowLayout(FlowLayout.RIGHT));

panel2.add(textMemory);

panel2.add(labelMemSpace);

panel2.add(buttonBk);

panel2.add(buttonCe);

panel2.add(buttonC);

panel3=newJPanel();

panel1.add(panel3,BorderLayout.CENTER);

button=newJButton[10];

for(inti=0;

i<

button.length;

i++){

button[i]=newJButton(Integer.toString(i));

button[i].setForeground(newColor(0,0,255));

}

//构造按钮

buttonMC=newJButton("

MC"

buttonMC.setForeground(newColor(255,0,0));

buttonMR=newJButton("

MR"

buttonMR.setForeground(newColor(255,0,0));

buttonMS=newJButton("

MS"

buttonMS.setForeground(newColor(255,0,0));

buttonMAdd=newJButton("

M+"

buttonMAdd.setForeground(newColor(255,0,0));

buttonDot=newJButton("

."

buttonDot.setForeground(newColor(0,0,255));

buttonAddAndSub=newJButton("

+/-"

buttonAddAndSub.setForeground(newColor(0,0,255));

buttonAdd=newJButton("

+"

buttonAdd.setForeground(newColor(255,0,0));

buttonSub=newJButton("

-"

buttonSub.setForeground(newColor(255,0,0));

buttonMul=newJButton("

*"

buttonMul.setForeground(newColor(255,0,0));

buttonDiv=newJButton("

/"

buttonDiv.setForeground(newColor(255,0,0));

buttonMod=newJButton("

%"

buttonMod.setForeground(newColor(0,0,255));

buttonSqrt=newJButton("

sqrt"

buttonSqrt.setForeground(newColor(0,0,255));

buttonDao=newJButton("

1/x"

buttonDao.setForeground(newColor(0,0,255));

buttonEqual=newJButton("

="

buttonEqual.setForeground(newColor(255,0,0));

//将所有行为与监听绑定

panel3.setLayout(newGridLayout(4,6));

panel3.add(buttonMC);

buttonMC.addActionListener(this);

panel3.add(button[7]);

button[7].addActionListener(this);

panel3.add(button[8]);

button[8].addActionListener(this);

panel3.add(button[9]);

button[9].addActionListener(this);

panel3.add(buttonDiv);

buttonDiv.addActionListener(this);

panel3.add(buttonSqrt);

buttonSqrt.addActionListener(this);

panel3.add(buttonMR);

buttonMR.addActionListener(this);

panel3.add(button[4]);

button[4].addActionListener(this);

panel3.add(button[5]);

button[5].addActionListener(this);

panel3.add(button[6]);

button[6].addActionListener(this);

panel3.add(buttonMul);

buttonMul.addActionListener(this);

panel3.add(buttonMod);

buttonMod.addActionListener(this);

panel3.add(buttonMS);

buttonMS.addActionListener(this);

panel3.add(button[1]);

button[1].addActionListener(this);

panel3.add(button[2]);

button[2].addActionListener(this);

panel3.add(button[3]);

button[3].addActionListener(this);

panel3.add(buttonSub);

buttonSub.addActionListener(this);

panel3.add(buttonDao);

buttonDao.addActionListener(this);

panel3.add(buttonMAdd);

buttonMAdd.addActionListener(this);

panel3.add(button[0]);

button[0].addActionListener(this);

panel3.add(buttonAddAndSub);

buttonAddAndSub.addActionListener(this);

panel3.add(buttonDot);

buttonDot.addActionListener(this);

panel3.add(buttonAdd);

buttonAdd.addActionListener(this);

panel3.add(buttonEqual);

buttonEqual.addActionListener(this);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.show();

//设置各个按钮行为

publicvoidactionPerformed(ActionEventevent){

booleansign=false;

//判断是否是double型数参与运算,是为true,不是为false

Objecttemp=event.getSource();

try{

//如果按下数据按钮,将按下的按钮代表的数据插入的当前文本框字符串之后

=9;

i++)

if(temp==button[i]&

&

clickable==true)

textAnswer.setText(textAnswer.getText()

+Integer.toString(i));

//按下"

按钮时,判断当前文本框内字符串中含不含"

,如果已含,则不允许再插入"

if(temp==buttonDot&

clickable==true){

booleanisDot=false;

if(textAnswer.getText().length()==0)

isDot=true;

textAnswer.getText().length();

if('

.'

==textAnswer.getText().charAt(i)){

break;

if(isDot==false)

textAnswer.setText(textAnswer.getText()+"

if((temp==buttonAdd||temp==buttonSub||temp==buttonMul||temp==buttonDiv)

&

//"

操作

if(temp==buttonAdd){

switch(prekey){

case0:

answerd+=Double.parseDouble(textAnswer.getText());

case1:

answerd-=Double.parseDouble(textAnswer.getText());

case2:

answerd*=Double.parseDouble(textAnswer.getText());

case3:

if(Double.parseDouble(textAnswer.getText())==0){

//textAnswer="

aa"

;

除数不能为零"

clickable=false;

}else

answerd/=Double.parseDouble(textAnswer.getText());

default:

answerd=Double.parseDouble(textAnswer.getText());

prekey=key=0;

if(temp==buttonSub){

prekey=key=1;

if(temp==buttonMul){

//textAnser="

prekey=key=2;

if(temp==buttonDiv){

prekey=key=3;

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

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1