JAVA计算机源代码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
panNum.add(buttons[i]); }
this.setLayout(null);//将 JFrame 窗体的布局清空 this.add(msg);//将文本输入框放入窗体里 this.add(labelInfo);//将标签放入窗体里 this.add(panSave); this.add(panBack); this.add(panNum); }
panSave.add(buttons[i]); }
//panClear---面板的组装 panBack.setLocation(45,30); panBack.setSize(200,26); panBack.setLayout(new FlowLayout(FlowLayout.CENTER,3,0)); for(int i=24;i<27;i++){
package com.tarena.main;
import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.AbstractAction; import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.BevelBorder;
//-----此方法组装剩余控件(文本输入框、Label 和 button)----public void initAdd(){
//msg------文本输入框的组装 msg.setSize(240,20); msg.setLocation(7, 4); msg.setBackground(Color.WHITE);//背景颜色白色 msg.setEditable(false);//文本输入框不能编辑 //将文本输入框的内容居右显示 msg.setHorizontalAlignment(JTextField.RIGHT);
} //------此将计算器上的所有 button 创建出来----public void initButton(){
//将监听绑定到 button 上 Buttonlistener listener=new Buttonlistener(); buttons[0]=new MyButton(listener,"7",Color.BLUE); buttons[1]=new MyButton(listener,"8",Color.BLUE); buttons[2]=new MyButton(listener,"9",Color.BLUE); buttons[3]=new MyButton(listener,"/",Color.RED); buttons[4]=new MyButton(listener,"sqrt",Color.BLUE); buttons[5]=new MyButton(listener,"4",Color.BLUE); buttons[6]=new MyButton(listener,"5",Color.BLUE); buttons[7]=new MyButton(listener,"6",Color.BLUE); buttons[8]=new MyButton(listener,"*",Color.RED); buttons[9]=new MyButton(listener,"%",Color.BLUE);
public Calculator(){ initMenu(); initButton(); initAdd(); this.setResizable(false); this.setLocation(200,200); this.setSize(260,240); this.setVisible(true); this.setTitle("计算器"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public class Calculator extends JFrame implements ActionListener{ //----------------------菜单栏定义开始-------------------------------------// public JMenuBar menuBar=new JMenuBar(); public JMenu editMenu=new JMenu("编辑(E)"); public JMenuItem menuItemCopy=new JMenuItem("复制(C)"); public JMenuItem menuItemPause=new JMenuItem("粘贴(P)"); public JMenu viewMenu=new JMenu("查看(V)"); public JMenuItem menuItemStand=new JMenuItem("标准型(T)"); public JMenuItem menuItemScience=new JMenuItem("科学型(S)"); public JMenuItem menuItemGroup=new JMenuItem("数学分组(I)"); public JMenu helpMenu=new JMenu("帮助(H)"); public JMenuItem menuItemHelp=new JMenuItem("帮组主题(H)"); public JMenuItem menuItemAbout=new JMenuItem("关于计算器(A)"); //--------------------------菜单栏定义结束-------------------------------------------------// //文本输入框 public JTextField msg=new JTextField("0"); //标签 public JLabel labelInfo=new JLabel(); //定义一个 panel 放 MC----M+按钮 public JPanel panSave=new JPanel(); //定义一个 panel 放 Baskspace CE C 按钮 public JPanel panBack=new JPanel(); //定义一个 panel 放 0-9 + - */等剩余按钮 public JPanel panNum=new JPanel();
panBack.add(buttons[i]); }
//panBtn---面板的组装 panNum.setSize(200,150); panNum.setLocation(45,60); panNum.setLayout(new FlowLayout(FlowLayout.CENTER,4,3)); for(int i=0;i<20;i++){
buttons[20]=new MyButton(listener,"MC",Color.RED); buttons[21]=new MyButton(listener,"MR",Color.RED); buttons[22]=new MyButton(listener,"MS",Color.RED); buttons[23]=new MyButton(listener,"M+",Color.RED); buttons[24]=new MyButton(listener,"Bascspace",62,25,Color.RED); buttons[25]=new MyButton(listener,"CE",62,25,Color.RED); buttons[26]=new MyButton(listener,"C",62,25,Color.RED); }
//----组装菜单栏---public void initMenu(){
this.setJMenuBar(menuBar); menuBar.add(editMenu);
editMenu.add(menuItemCopy); editMenu.add(menuItemPause); menuBar.add(viewMenu); viewMenu.add(menuItemStand); viewMenu.add(menuItemScience); viewMenu.addSeparator();//添加分割线 viewMenu.add(menuItemGroup); menuBar.add(helpMenu); helpMenu.add(menuItemHelp); helpMenu.addSeparator();//添加分割线 helpMenu.add(menuItemAbout); menuItemAbout.addActionListener(this);
//创建一个放 27 个 MyButton 类型的数组 public MyButton[] buttons=new MyButton[27]; //fam 的作用是缓存 public StringBuffer fam=new StringBuffer(); public double value=0; public int operator=0; public boolean hasSave=false; public double numSave=0;
//labelInfo---标签的组装 labelInfo.setSize(30,26); labelInfo.setLocation(7,30); //能将标签凹陷 labelInfo.setBorder(new BevelBorder(BevelBorder.LOWERED));
//panSave---面板的组装 panSave.setSize(35,150); panSave.setLocation(7,60); //设置 button 和 button 之间的水平间距和垂直间距 panSave.setLayout(new FlowLayout(FlowLayout.CENTER,3,3)); //将 MC MR MS M+放入 panSave 面板上 for(int i=20;i<24;i++){
Βιβλιοθήκη Baidu
buttons[10]=new MyButton(listener,"1",Color.BLUE); buttons[11]=new MyButton(listener,"2",Color.BLUE); buttons[12]=new MyButton(listener,"3",Color.BLUE); buttons[13]=new MyButton(listener,"-",Color.RED); buttons[14]=new MyButton(listener,"1/x",Color.BLUE); buttons[15]=new MyButton(listener,"0",Color.BLUE); buttons[16]=new MyButton(listener,"+/-",Color.BLUE); buttons[17]=new MyButton(listener,".",Color.BLUE); buttons[18]=new MyButton(listener,"+",Color.RED); buttons[19]=new MyButton(listener,"=",Color.RED);
this.setLayout(null);//将 JFrame 窗体的布局清空 this.add(msg);//将文本输入框放入窗体里 this.add(labelInfo);//将标签放入窗体里 this.add(panSave); this.add(panBack); this.add(panNum); }
panSave.add(buttons[i]); }
//panClear---面板的组装 panBack.setLocation(45,30); panBack.setSize(200,26); panBack.setLayout(new FlowLayout(FlowLayout.CENTER,3,0)); for(int i=24;i<27;i++){
package com.tarena.main;
import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.AbstractAction; import javax.swing.AbstractButton; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.BevelBorder;
//-----此方法组装剩余控件(文本输入框、Label 和 button)----public void initAdd(){
//msg------文本输入框的组装 msg.setSize(240,20); msg.setLocation(7, 4); msg.setBackground(Color.WHITE);//背景颜色白色 msg.setEditable(false);//文本输入框不能编辑 //将文本输入框的内容居右显示 msg.setHorizontalAlignment(JTextField.RIGHT);
} //------此将计算器上的所有 button 创建出来----public void initButton(){
//将监听绑定到 button 上 Buttonlistener listener=new Buttonlistener(); buttons[0]=new MyButton(listener,"7",Color.BLUE); buttons[1]=new MyButton(listener,"8",Color.BLUE); buttons[2]=new MyButton(listener,"9",Color.BLUE); buttons[3]=new MyButton(listener,"/",Color.RED); buttons[4]=new MyButton(listener,"sqrt",Color.BLUE); buttons[5]=new MyButton(listener,"4",Color.BLUE); buttons[6]=new MyButton(listener,"5",Color.BLUE); buttons[7]=new MyButton(listener,"6",Color.BLUE); buttons[8]=new MyButton(listener,"*",Color.RED); buttons[9]=new MyButton(listener,"%",Color.BLUE);
public Calculator(){ initMenu(); initButton(); initAdd(); this.setResizable(false); this.setLocation(200,200); this.setSize(260,240); this.setVisible(true); this.setTitle("计算器"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public class Calculator extends JFrame implements ActionListener{ //----------------------菜单栏定义开始-------------------------------------// public JMenuBar menuBar=new JMenuBar(); public JMenu editMenu=new JMenu("编辑(E)"); public JMenuItem menuItemCopy=new JMenuItem("复制(C)"); public JMenuItem menuItemPause=new JMenuItem("粘贴(P)"); public JMenu viewMenu=new JMenu("查看(V)"); public JMenuItem menuItemStand=new JMenuItem("标准型(T)"); public JMenuItem menuItemScience=new JMenuItem("科学型(S)"); public JMenuItem menuItemGroup=new JMenuItem("数学分组(I)"); public JMenu helpMenu=new JMenu("帮助(H)"); public JMenuItem menuItemHelp=new JMenuItem("帮组主题(H)"); public JMenuItem menuItemAbout=new JMenuItem("关于计算器(A)"); //--------------------------菜单栏定义结束-------------------------------------------------// //文本输入框 public JTextField msg=new JTextField("0"); //标签 public JLabel labelInfo=new JLabel(); //定义一个 panel 放 MC----M+按钮 public JPanel panSave=new JPanel(); //定义一个 panel 放 Baskspace CE C 按钮 public JPanel panBack=new JPanel(); //定义一个 panel 放 0-9 + - */等剩余按钮 public JPanel panNum=new JPanel();
panBack.add(buttons[i]); }
//panBtn---面板的组装 panNum.setSize(200,150); panNum.setLocation(45,60); panNum.setLayout(new FlowLayout(FlowLayout.CENTER,4,3)); for(int i=0;i<20;i++){
buttons[20]=new MyButton(listener,"MC",Color.RED); buttons[21]=new MyButton(listener,"MR",Color.RED); buttons[22]=new MyButton(listener,"MS",Color.RED); buttons[23]=new MyButton(listener,"M+",Color.RED); buttons[24]=new MyButton(listener,"Bascspace",62,25,Color.RED); buttons[25]=new MyButton(listener,"CE",62,25,Color.RED); buttons[26]=new MyButton(listener,"C",62,25,Color.RED); }
//----组装菜单栏---public void initMenu(){
this.setJMenuBar(menuBar); menuBar.add(editMenu);
editMenu.add(menuItemCopy); editMenu.add(menuItemPause); menuBar.add(viewMenu); viewMenu.add(menuItemStand); viewMenu.add(menuItemScience); viewMenu.addSeparator();//添加分割线 viewMenu.add(menuItemGroup); menuBar.add(helpMenu); helpMenu.add(menuItemHelp); helpMenu.addSeparator();//添加分割线 helpMenu.add(menuItemAbout); menuItemAbout.addActionListener(this);
//创建一个放 27 个 MyButton 类型的数组 public MyButton[] buttons=new MyButton[27]; //fam 的作用是缓存 public StringBuffer fam=new StringBuffer(); public double value=0; public int operator=0; public boolean hasSave=false; public double numSave=0;
//labelInfo---标签的组装 labelInfo.setSize(30,26); labelInfo.setLocation(7,30); //能将标签凹陷 labelInfo.setBorder(new BevelBorder(BevelBorder.LOWERED));
//panSave---面板的组装 panSave.setSize(35,150); panSave.setLocation(7,60); //设置 button 和 button 之间的水平间距和垂直间距 panSave.setLayout(new FlowLayout(FlowLayout.CENTER,3,3)); //将 MC MR MS M+放入 panSave 面板上 for(int i=20;i<24;i++){
Βιβλιοθήκη Baidu
buttons[10]=new MyButton(listener,"1",Color.BLUE); buttons[11]=new MyButton(listener,"2",Color.BLUE); buttons[12]=new MyButton(listener,"3",Color.BLUE); buttons[13]=new MyButton(listener,"-",Color.RED); buttons[14]=new MyButton(listener,"1/x",Color.BLUE); buttons[15]=new MyButton(listener,"0",Color.BLUE); buttons[16]=new MyButton(listener,"+/-",Color.BLUE); buttons[17]=new MyButton(listener,".",Color.BLUE); buttons[18]=new MyButton(listener,"+",Color.RED); buttons[19]=new MyButton(listener,"=",Color.RED);