计算器.docx
- 文档编号:12208963
- 上传时间:2023-04-17
- 格式:DOCX
- 页数:19
- 大小:16.20KB
计算器.docx
《计算器.docx》由会员分享,可在线阅读,更多相关《计算器.docx(19页珍藏版)》请在冰豆网上搜索。
计算器
importjava.awt.BorderLayout;
importjavax.swing.JPanel;
importjavax.swing.JFrame;
importjavax.swing.JTextField;
importjava.awt.GridBagLayout;
importjavax.swing.JButton;
importjava.awt.GridBagConstraints;
importjava.awt.GridLayout;
importjava.awt.ComponentOrientation;
publicclasscalculatorextendsJFrame{
privatestaticfinallongserialVersionUID=1L;
privateJPaneljContentPane=null;
privateJTextFieldjTextField=null;
privateJPaneljPanel=null;
privateJButtonjButton=null;
privateJButtonjButton1=null;
privateJButtonjButton2=null;
privateJButtonjButton3=null;
privateJButtonjButton4=null;
privateJButtonjButton5=null;
privateJButtonjButton6=null;
privateJButtonjButton7=null;
privateJButtonjButton8=null;
privateJButtonjButton9=null;
privateJButtonjButton10=null;
privateJButtonjButton11=null;
privateJButtonjButton12=null;
privateJButtonjButton13=null;
privateJButtonjButton14=null;
privateJButtonjButton15=null;
privatedoubleresult=0;
privateStringlastCommand="=";//@jve:
decl-index=0:
privatebooleanstart=true;
privateJPaneljPanel1=null;
privateJButtonjButton16=null;
/**
*Thisisthedefaultconstructor
*/
publicqwert(){
super();
initialize();
}
/**
*Thismethodinitializesthis
*
*@returnvoid
*/
privatevoidinitialize(){
this.setSize(300,200);
this.setContentPane(getJContentPane());
this.setTitle("计算器");
}
/**
*ThismethodinitializesjContentPane
*
*@returnjavax.swing.JPanel
*/
privateJPanelgetJContentPane(){
if(jContentPane==null){
jContentPane=newJPanel();
jContentPane.setLayout(newBorderLayout());
jContentPane.add(getJPanel1(),BorderLayout.EAST);
jContentPane.add(getJTextField(),BorderLayout.NORTH);
jContentPane.add(getJPanel(),BorderLayout.CENTER);
}
returnjContentPane;
}
/**
*ThismethodinitializesjTextField
*
*@returnjavax.swing.JTextField
*/
privateJTextFieldgetJTextField(){
if(jTextField==null){
jTextField=newJTextField();
}
returnjTextField;
}
/**
*ThismethodinitializesjPanel
*
*@returnjavax.swing.JPanel
*/
privateJPanelgetJPanel(){
if(jPanel==null){
GridLayoutgridLayout=newGridLayout(4,4);
jPanel=newJPanel();
jPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
jPanel.setLayout(gridLayout);
jPanel.add(getJButton(),null);
jPanel.add(getJButton8(),null);
jPanel.add(getJButton12(),null);
jPanel.add(getJButton15(),null);
jPanel.add(getJButton14(),null);
jPanel.add(getJButton13(),null);
jPanel.add(getJButton11(),null);
jPanel.add(getJButton10(),null);
jPanel.add(getJButton7(),null);
jPanel.add(getJButton6(),null);
jPanel.add(getJButton1(),null);
jPanel.add(getJButton2(),null);
jPanel.add(getJButton3(),null);
jPanel.add(getJButton4(),null);
jPanel.add(getJButton5(),null);
jPanel.add(getJButton9(),null);
}
returnjPanel;
}
/**
*ThismethodinitializesjButton
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton(){
if(jButton==null){
jButton=newJButton();
jButton.setText("1");
jButton.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
if(start){
jTextField.setText("");
start=false;
}
jTextField.setText(jTextField.getText()+input);
}
});
}
returnjButton;
}
/**
*ThismethodinitializesjButton1
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton1(){
if(jButton1==null){
jButton1=newJButton();
jButton1.setText("9");
jButton1.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
if(start){
jTextField.setText("");
start=false;}
jTextField.setText(jTextField.getText()+input);
}
});
}
returnjButton1;
}
/**
*ThismethodinitializesjButton2
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton2(){
if(jButton2==null){
jButton2=newJButton();
jButton2.setText("*");
jButton2.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
calculate(Double.parseDouble(jTextField.getText()));
lastCommand=input;
start=true;
}
});
}
returnjButton2;
}
/**
*ThismethodinitializesjButton3
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton3(){
if(jButton3==null){
jButton3=newJButton();
jButton3.setText("0");
jButton3.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
if(start){
jTextField.setText("");
start=false;}
jTextField.setText(jTextField.getText()+input);
}
});
}
returnjButton3;
}
/**
*ThismethodinitializesjButton4
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton4(){
if(jButton4==null){
jButton4=newJButton();
jButton4.setText(".");
jButton4.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
if(start){
jTextField.setText("");
start=false;
}
jTextField.setText(jTextField.getText()+input);
}
});
}
returnjButton4;
}
/**
*ThismethodinitializesjButton5
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton5(){
if(jButton5==null){
jButton5=newJButton();
jButton5.setText("=");
jButton5.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
calculate(Double.parseDouble(jTextField.getText()));
lastCommand=input;
start=true;
}
});
}
returnjButton5;
}
/**
*ThismethodinitializesjButton6
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton6(){
if(jButton6==null){
jButton6=newJButton();
jButton6.setText("8");
jButton6.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
if(start){
jTextField.setText("");
start=false;
}
jTextField.setText(jTextField.getText()+input);
}
});
}
returnjButton6;
}
/**
*ThismethodinitializesjButton7
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton7(){
if(jButton7==null){
jButton7=newJButton();
jButton7.setText("7");
jButton7.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
if(start){
jTextField.setText("");
start=false;
}
jTextField.setText(jTextField.getText()+input);
}
});
}
returnjButton7;
}
/**
*ThismethodinitializesjButton8
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton8(){
if(jButton8==null){
jButton8=newJButton();
jButton8.setText("2");
jButton8.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
if(start){
jTextField.setText("");
start=false;}
jTextField.setText(jTextField.getText()+input);
}
});
}
returnjButton8;
}
/**
*ThismethodinitializesjButton9
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton9(){
if(jButton9==null){
jButton9=newJButton();
jButton9.setText("/");
jButton9.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
calculate(Double.parseDouble(jTextField.getText()));
lastCommand=input;
start=true;
}
});
}
returnjButton9;
}
/**
*ThismethodinitializesjButton10
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton10(){
if(jButton10==null){
jButton10=newJButton();
jButton10.setText("-");
jButton10.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
calculate(Double.parseDouble(jTextField.getText()));
lastCommand=input;
start=true;
}
});
}
returnjButton10;
}
/**
*ThismethodinitializesjButton11
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton11(){
if(jButton11==null){
jButton11=newJButton();
jButton11.setText("6");
jButton11.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
Stringinput=e.getActionCommand();
if(start){
jTextField.setText("");
start=false;}
jTextField.setText(jTextField.getText()+input);
}
});
}
returnjButton11;
}
/**
*ThismethodinitializesjButton12
*
*@returnjavax.swing.JButton
*/
privateJButtongetJButton12(){
if(jButton12==null){
jButton12=newJButton();
jButton12.setText("3");
jButton12.addActionListener(newjava.awt.event.ActionListener(){
p
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算器
![提示](https://static.bdocx.com/images/bang_tan.gif)