|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
但是对于JAVA技术类的学习,我觉得大课堂反而会影响自身独立思考的过程,因为上课的时候,老师讲课的速度很快为了不遗漏要点,通常会仔细的听,程序|源代码//frame版程序源代码以下,疏漏的地方,看品评斧正。
//数字分组没有编写,迷信型盘算器没有编写,其他已完美。
importjava.awt.*;
importjava.lang.*;
importjavax.swing.*;
importjavax.swing.event.*;
importjava.awt.event.*;
importjava.text.DecimalFormat;
publicclassCalculator
implementsActionListener{//导进举措监听接口
//计划面板中的单元
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;
//机关函数
publicCalculator(){
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)");
viewMenu=newJMenu("检察(V)");
helpMenu=newJMenu("匡助(H)");
copyItem=newJMenuItem("复制(C)Ctrl+C");
copyItem.addActionListener(this);
pasteItem=newJMenuItem("粘贴(V)Ctrl+V");
pasteItem.addActionListener(this);
editMenu.add(copyItem);
editMenu.add(pasteItem);
tItem=newJMenuItem("●尺度型(T)");
tItem.addActionListener(this);
sItem=newJMenuItem("迷信型(S)");
sItem.addActionListener(this);
numberGroup=newJMenuItem("数字分组(I)");
numberGroup.addActionListener(this);
viewMenu.add(tItem);
viewMenu.add(sItem);
viewMenu.add(numberGroup);
topHelp=newJMenuItem("匡助主题(H)");
topHelp.addActionListener(this);
help=newJTextArea(5,20);
scrollHelp=newJScrollPane(help);
help.setEditable(false);
help.append("实行复杂盘算
");
help.append("1.键进盘算的第一个数字。
");
help.append("2.单击“+”实行加、“-”实行减、“*”实行乘或“/”实行除。
");
help.append("3.键进盘算的下一个数字。
");
help.append("4.输出一切残剩的运算符和数字。
");
help.append("5.单击“=”。
");
aboutCal=newJMenuItem("关于盘算器(A)");
aboutCal.addActionListener(this);
helpMenu.add(topHelp);
helpMenu.add(aboutCal);
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(frame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
//设置各个按钮举动
publicvoidactionPerformed(ActionEventevent){
booleansign=false;//判别是不是是double型数介入运算,是为true,不是为false
Objecttemp=event.getSource();
try{
//假如按下数据按钮,将按下的按钮代表的数据拔出确当前文本框字符串以后
for(inti=0;i<=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;
for(inti=0;i<textAnswer.getText().length();i++)
if(.==textAnswer.getText().charAt(i)){
isDot=true;
break;
}
if(isDot==false)
textAnswer.setText(textAnswer.getText()+".");
}
if((temp==buttonAdd||temp==buttonSub||temp==buttonMul||
temp==buttonDiv)&&clickable==true){
//+操纵
if(temp==buttonAdd){
switch(prekey){
case0:
answerd+=Double.parseDouble(textAnswer.getText());
break;
case1:
answerd-=Double.parseDouble(textAnswer.getText());
break;
case2:
answerd*=Double.parseDouble(textAnswer.getText());
break;
case3:
if(Double.parseDouble(textAnswer.getText())==0){
textAnswer.setText("除数不克不及为零");
clickable=false;
}
else
answerd/=Double.parseDouble(textAnswer.getText());
break;
default:
answerd=Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey=key=0;
}
//-操纵
if(temp==buttonSub){
switch(prekey){
case0:
answerd+=Double.parseDouble(textAnswer.getText());
break;
case1:
answerd-=Double.parseDouble(textAnswer.getText());
break;
case2:
answerd*=Double.parseDouble(textAnswer.getText());
break;
case3:
if(Double.parseDouble(textAnswer.getText())==0){
textAnswer.setText("除数不克不及为零");
clickable=false;
}
else
answerd/=Double.parseDouble(textAnswer.getText());
break;
default:
answerd=Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey=key=1;
}
//*操纵
if(temp==buttonMul){
switch(prekey){
case0:
answerd+=Double.parseDouble(textAnswer.getText());
break;
case1:
answerd-=Double.parseDouble(textAnswer.getText());
break;
case2:
answerd*=Double.parseDouble(textAnswer.getText());
break;
case3:
if(Double.parseDouble(textAnswer.getText())==0){
textAnswer.setText("除数不克不及为零");
clickable=false;
}
else
answerd/=Double.parseDouble(textAnswer.getText());
break;
default:
answerd=Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey=key=2;
}
///操纵
if(temp==buttonDiv){
switch(prekey){
case0:
answerd+=Double.parseDouble(textAnswer.getText());
break;
case1:
answerd-=Double.parseDouble(textAnswer.getText());
break;
case2:
answerd*=Double.parseDouble(textAnswer.getText());
break;
case3:
if(Double.parseDouble(textAnswer.getText())==0){
textAnswer.setText("除数不克不及为零");
clickable=false;
}
else
answerd/=Double.parseDouble(textAnswer.getText());
break;
default:
answerd=Double.parseDouble(textAnswer.getText());
}
textAnswer.setText("");
prekey=key=3;
}
}
//=操纵
if(temp==buttonEqual&&clickable==true){
//假如一连按=,则举行一连运算
if(prekey==5){
if(key==0){
answerd+=vard;
textAnswer.setText(df.format(answerd));
}
if(key==1){
answerd-=vard;
textAnswer.setText(df.format(answerd));
}
if(key==2){
answerd*=vard;
textAnswer.setText(df.format(answerd));
}
if(key==3){
if(Double.parseDouble(textAnswer.getText())==0){
textAnswer.setText("除数不克不及为零");
clickable=false;
}
else{
answerd/=vard;
textAnswer.setText(df.format(answerd));
}
}
}
else{
vard=Double.parseDouble(textAnswer.getText());
if(key==0){
prekey=-1;
answerd+=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if(key==1){
prekey=-1;
answerd-=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if(key==2){
prekey=-1;
answerd*=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
if(key==3){
prekey=-1;
if(Double.parseDouble(textAnswer.getText())==0){
textAnswer.setText("除数不克不及为零");
clickable=false;
}
else{
answerd/=Double.parseDouble(textAnswer.getText());
textAnswer.setText(df.format(answerd));
}
}
}
prekey=5;
}
//%操纵,对第二个操纵数除以100
if(temp==buttonMod&&clickable==true){
if(answerd==0){
Strings=textAnswer.getText();
textAnswer.setText(s);
}
else{
booleanisDot=false;
for(inti=0;i<textAnswer.getText().length();i++)
if(.==textAnswer.getText().charAt(i)){
isDot=true;
break;
}
//假如是double数,除100
if(isDot==true){
doubledtemp=Double.parseDouble(textAnswer.getText());
dtemp=dtemp/100.0;
textAnswer.setText(Double.toString(dtemp));
}
else{
//假如是int数但能被100整除,则往失落开端两个零
if(Integer.parseInt(textAnswer.getText())%100==0){
intitemp=Integer.parseInt(textAnswer.getText());
itemp/=100;
textAnswer.setText(Integer.toString(itemp));
}
//假如是int数,但不克不及被100整除,则按double数处置
else{
doubledtemp=Double.parseDouble(textAnswer.getText());
dtemp=dtemp/100.0;
textAnswer.setText(Double.toString(dtemp));
}
}
}
}
//开根号运算
if(temp==buttonSqrt&&clickable==true){
Strings=textAnswer.getText();
if(s.charAt(0)==-){
textAnswer.setText("正数不克不及开根号");
clickable=false;
}
else
textAnswer.setText(Double.toString(java.lang.Math.sqrt(Double.
parseDouble(textAnswer.getText()))));
}
//倒数运算
if(temp==buttonDao&&clickable==true){
if(textAnswer.getText().charAt(0)==0&&
textAnswer.getText().length()==1){
textAnswer.setText("零不克不及求倒数");
clickable=false;
}
else{
booleanisDec=true;
inti,j,k;
Strings=Double.toString(1/Double.parseDouble(textAnswer.getText()));
for(i=0;i<s.length();i++)
if(s.charAt(i)==.)
break;
for(j=i+1;j<s.length();j++)
if(s.charAt(j)!=0){
isDec=false;
break;
}
if(isDec==true){
Stringstemp="";
for(k=0;k<i;k++)
stemp+=s.charAt(k);
textAnswer.setText(stemp);
}
else
textAnswer.setText(s);
}
}
//按下+/-按钮时处置
if(temp==buttonAddAndSub&&clickable==true){
booleanisNumber=true;
Strings=textAnswer.getText();
for(inti=0;i<s.length();i++)
if(!(s.charAt(i)>=0&&s.charAt(i)<=9||s.charAt(i)==.||
s.charAt(i)==-)){
isNumber=false;
break;
}
if(isNumber==true){
//假如以后字符串首字母有-号,代体现在是个正数,再按下时,则将首标记往失落
if(s.charAt(0)==-){
textAnswer.setText("");
for(inti=1;i<s.length();i++){
chara=s.charAt(i);
textAnswer.setText(textAnswer.getText()+a);
}
}
//假如以后字符串第一个字符不是标记,则增加一个标记在首字母处
else
textAnswer.setText(-+s);
}
}
//盘算器有关内存操纵
//MC的操纵,将内存清0
if(temp==buttonMC&&clickable==true){
memoryd=memoryi=0;
textMemory.setText("");
}
//MS的操纵,将以后文本框内容保留进内存,显现M
if(temp==buttonMS&&clickable==true){
booleanisDot=false;
textMemory.setText("M");
for(inti=0;i<textAnswer.getText().length();i++)
if(.==textAnswer.getText().charAt(i)){
isDot=true;
break;
}
//假如是double,则存进memoryd(double存储器)
if(isDot==true){
memoryd=Double.parseDouble(textAnswer.getText());
memoryi=0;//包管存储器中寄存最新的值
}
//假如是int,则存进memoryi(int存储器)
else{
memoryi=Integer.parseInt(textAnswer.getText());
memoryd=0;//包管存储器中寄存最新的值
}
}
//MR的操纵,将存储器中的信息输入
if(temp==buttonMR&&clickable==true){
if(memoryd!=0)
textAnswer.setText(Double.toString(memoryd));
if(memoryi!=0)
textAnswer.setText(Integer.toString(memoryi));
}
//M+的功效,将以后文本框里的数据和存储器中数据相加后,再存进存储器
if(temp==buttonMAdd&&clickable==true){
booleanisDot=false;
for(inti=0;i<textAnswer.getText().length();i++)
if(.==textAnswer.getText().charAt(i)){
isDot=true;
break;
}
if(memoryi!=0){//存储中是一个int型数
if(isDot==false)//被加数是一个int型数
memoryi+=Integer.parseInt(textAnswer.getText());
else{//被加数是一个double型数,则将int存储器中数传进double存储器与以后数相加,int存储器清零
memoryd=memoryi+Double.parseDouble(textAnswer.getText());
memoryi=0;
}
}
else
memoryd+=Double.parseDouble(textAnswer.getText());
}
//按下Backspace键,使用轮回将以后字符串中的最初一个字母删除
if(temp==buttonBk&&clickable==true){
Strings=textAnswer.getText();
textAnswer.setText("");
for(inti=0;i<s.length()-1;i++){
chara=s.charAt(i);
textAnswer.setText(textAnswer.getText()+a);
}
}
//按下CE按钮,将以后文本框内数据扫除
if(temp==buttonCe){
textAnswer.setText("");
clickable=true;
}
//按下C按钮,文本框内数据扫除,同时var,answer清0
if(temp==buttonC){
vard=answerd=0;
textAnswer.setText("");
clickable=true;
}
//按下复制菜单栏
if(temp==copyItem){
copy=textAnswer.getText();
}
//按下粘贴菜单栏
if(temp==pasteItem){
textAnswer.setText(copy);
}
if(temp==sItem){
JOptionPane.showMessageDialog(panel,"以后是尺度型盘算器,
迷信型盘算器有待更新。");
}
//按下匡助主题菜单栏
if(temp==topHelp){
JOptionPane.showMessageDialog(panel,scrollHelp);
}
//按下数字分组菜单栏
if(temp==numberGroup){
if(numberGroup.getText().compareTo("数字分组(I)")==0)
numberGroup.setText("√数字分组(I)");
else
numberGroup.setText("数字分组(I)");
}
//按下关于菜单栏
if(temp==aboutCal){
JOptionPane.showMessageDialog(panel,"盘算器1.00版
开辟者:楼竞");
}
}
//输出中假如有操纵不法,好比按下两次+,捕捉非常
catch(Exceptione){
textAnswer.setText("操纵不法");
clickable=false;
}
}
//主函数
publicstaticvoidmain(Stringargs[]){
newCalculator();
}
}
再举这样一个例子:如果你想对一个数字取绝对值,你会怎么做呢?java的做法是intc=Math.abs(-166);而ruby的做法是:c=-166.abs。呵呵,这就看出了java与ruby的区别。 |
|