计算机程序源代码.docx
《计算机程序源代码.docx》由会员分享,可在线阅读,更多相关《计算机程序源代码.docx(10页珍藏版)》请在冰豆网上搜索。
![计算机程序源代码.docx](https://file1.bdocx.com/fileroot1/2022-11/15/aeffee7f-31dd-4cca-bd55-6975f38a9379/aeffee7f-31dd-4cca-bd55-6975f38a93791.gif)
计算机程序源代码
importjavax.swing.*;
importjava.awt.*;
importjavax.swing.border.*;
importjava.awt.event.*;
publicclassSX7{
JTextFieldfield;JFramef;
JButtonb;JLabellabel;
JPanelp1;
JButtonbb1,bb2,bb3;
ActionListenerh;
Colorcor1,cor2;
Containercon;
GridBagLayoutg;
GridBagConstraintsc;
intgridx,gridy,gridwidth,gridheight,anchor,fill,ipadx,ipady;
doubleweightx,weighty;
Insetsinset;
Fontt;
booleank1,/*控制字符的连续输出*/z,/*实现除零对其它按键的控制*/mh=true;/*保存控制显示*/
Stringop1,op2,r,ch,tf,str7,str11;
Doubler1,n1,n2,a,str8,str9,str10;
intk,/*控制运算*/counter1,/*控制操作符*/counter2;/*控制连续运算*/
SX7(){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exceptione){}
makeJFrame();//添加JFrame
addmakeBarMenu();//添加菜单栏
addButton();//添加组件
f.setVisible(true);
}
privatevoidaddButton(){
JPanelp=newJPanel();
p1=newJPanel();
p.setLayout(newGridLayout(1,4,3,6));
p1.setLayout(newGridLayout(4,6,3,6));
g=newGridBagLayout();
con.setLayout(g);
anchor=GridBagConstraints.CENTER;//当组件小于其显示区域时使用此字段。
它可以确定在显示区域中放置组件的位置。
fill=GridBagConstraints.BOTH;//当组件的显示区域大于它所请求的显示区域的大小时使用此字段。
它可以确定是否调整组件大小
weightx=1;//如果得到的布局在水平方向上比需要填充的区域小,则系统会将额外的空间按照其权重比例分布到每一列。
权重为零的列不会得到额外的空间。
weighty=1;
inset=newInsets(1,1,1,1);//指定组件的外部填充,即组件与其显示区域边缘之间间距的最小量
ipadx=1;//定组件的内部填充
ipady=1;
field=newJTextField("0.");//添加文本区
field.setBackground(Color.pink);
field.setHorizontalAlignment(JTextField.RIGHT);//右对齐
field.setEditable(false);//不可编辑
gridx=0;
gridy=0;
gridwidth=6;
gridheight=1;
c=newGridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,fill,inset,ipadx,ipady);
g.setConstraints(field,c);
con.add(field);
label=newJLabel("标签");//添加标签
label.setForeground(Color.blue);
label.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));//设置凹陷
Qingchuqc=newQingchu();
bb1=newJButton("Backspace");
bb1.setFont(newFont("serif",Font.PLAIN,7));
bb1.setForeground(Color.RED);
bb1.addActionListener(qc);
bb2=newJButton("CE");
bb2.setForeground(Color.RED);
bb2.addActionListener(qc);
bb3=newJButton("C");
bb3.setForeground(Color.RED);
bb3.addActionListener(qc);
p.add(label);p.add(bb1);p.add(bb2);p.add(bb3);
gridx=0;
gridy=1;
gridwidth=6;
gridheight=1;
c=newGridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,fill,inset,ipadx,ipady);
g.setConstraints(p,c);
con.add(p);
Shuzisz=newShuzi();
Yunsuanys=newYunsuan();
Dengyudy=newDengyu();
Storest=newStore();
Tesufuhaoh=newTesufuhao();
cor1=Color.red;
cor2=Color.blue;
makeButton("MC",0,2,1,1,cor1,t,st);
makeButton("MR",0,3,1,1,cor1,t,st);
makeButton("MS",0,4,1,1,cor1,t,st);
makeButton("M+",0,5,1,1,cor1,t,st);
makeButton("sqrt",5,2,1,1,cor2,t,h);
makeButton("%",5,3,1,1,cor2,t,h);
makeButton("1/x",5,4,1,1,cor2,t,h);
makeButton("+/-",2,5,1,1,cor2,t,h);
makeButton("/",4,2,1,1,cor1,t,ys);
makeButton("*",4,3,1,1,cor1,t,ys);
makeButton("*",4,3,1,1,cor1,t,ys);
makeButton("+",4,5,1,1,cor1,t,ys);
makeButton("7",1,2,1,1,cor2,t,sz);
makeButton("8",2,2,1,1,cor2,t,sz);
makeButton("9",3,2,1,1,cor2,t,sz);
makeButton("4",1,3,1,1,cor2,t,sz);
makeButton("5",2,3,1,1,cor2,t,sz);
makeButton("6",3,3,1,1,cor2,t,sz);
makeButton("1",1,4,1,1,cor2,t,sz);
makeButton("2",2,4,1,1,cor2,t,sz);
makeButton("3",3,4,1,1,cor2,t,sz);
makeButton("-",4,4,1,1,cor1,t,ys);
makeButton("0",1,5,1,1,cor2,t,sz);
makeButton(".",3,5,1,1,cor2,t,sz);
makeButton("=",5,5,1,1,cor1,t,dy);
}
privatevoidmakeButton(Stringnames,intgridx,intgridy,intgridwidth,intgridheight,Colorys,Fontzt,ActionListenerh){
b=newJButton(names);
b.setForeground(ys);
t=newFont("Serif",Font.PLAIN,11);
b.setFont(t);
b.addActionListener(h);//对按钮进行设置
c=newGridBagConstraints(gridx,gridy,gridwidth,gridheight,weightx,weighty,anchor,fill,inset,ipadx,ipady);
g.setConstraints(b,c);//构建限制条件
con.add(b);
}
privateclassShuziimplementsActionListener{
publicvoidactionPerformed(ActionEventevent){
tf=field.getText();//获取文本
Stringinput=event.getActionCommand();
if(z==false){
if(counter1==0){
if(k1==false){
field.setText("");field.setText(input);k1=true;}
else{field.setText(tf+input);}}
if(counter1>=1){
if(k1==false){
field.setText("");field.setText(input);k1=true;}
else{field.setText(tf+input);}
n2=Double.parseDouble(field.getText());}}
}
}
privateclassYunsuanimplementsActionListener{
publicvoidactionPerformed(ActionEventevent){
if(z==false){
if(event.getActionCommand()=="+"){
n1=Double.parseDouble(field.getText());k=1;}
if(event.getActionCommand()=="-"){
n1=Double.parseDouble(field.getText());k=2;}
if(event.getActionCommand()=="*"){
n1=Double.parseDouble(field.getText());k=3;}
if(event.getActionCommand()=="/"){
n1=Double.parseDouble(field.getText(