1、计算器import java.awt.BorderLayout;import javax.swing.JPanel;import javax.swing.JFrame;import javax.swing.JTextField;import java.awt.GridBagLayout;import javax.swing.JButton;import java.awt.GridBagConstraints;import java.awt.GridLayout;import java.awt.ComponentOrientation;public class calculator extend
2、s JFrame private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JTextField jTextField = null; private JPanel jPanel = null; private JButton jButton = null; private JButton jButton1 = null; private JButton jButton2 = null; private JButton jButton3 = null; private
3、 JButton jButton4 = null; private JButton jButton5 = null; private JButton jButton6 = null; private JButton jButton7 = null; private JButton jButton8 = null; private JButton jButton9 = null; private JButton jButton10 = null; private JButton jButton11 = null; private JButton jButton12 = null; private
4、 JButton jButton13 = null; private JButton jButton14 = null; private JButton jButton15 = null; private double result=0; private String lastCommand=; / jve:decl-index=0: private boolean start=true; private JPanel jPanel1 = null; private JButton jButton16 = null; /* * This is the default constructor *
5、/ public qwert() super(); initialize(); /* * This method initializes this * * return void */ private void initialize() this.setSize(300, 200); this.setContentPane(getJContentPane(); this.setTitle(计算器); /* * This method initializes jContentPane * * return javax.swing.JPanel */ private JPanel getJCont
6、entPane() if (jContentPane = null) jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout(); jContentPane.add(getJPanel1(), BorderLayout.EAST); jContentPane.add(getJTextField(), BorderLayout.NORTH); jContentPane.add(getJPanel(), BorderLayout.CENTER); return jContentPane; /* * This meth
7、od initializes jTextField * * return javax.swing.JTextField */ private JTextField getJTextField() if (jTextField = null) jTextField = new JTextField(); return jTextField; /* * This method initializes jPanel * * return javax.swing.JPanel */ private JPanel getJPanel() if (jPanel = null) GridLayout gri
8、dLayout = new GridLayout(4,4); jPanel = new JPanel(); 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(getJ
9、Button14(), 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(get
10、JButton4(), null); jPanel.add(getJButton5(), null); jPanel.add(getJButton9(), null); return jPanel; /* * This method initializes jButton * * return javax.swing.JButton */ private JButton getJButton() if (jButton = null) jButton = new JButton(); jButton.setText(1); jButton.addActionListener(new java.
11、awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) jTextField.setText(); start=false; jTextField.setText(jTextField.getText()+input); ); return jButton; /* * This method initializes jButton1 * * return javax.swing.JButton
12、 */ private JButton getJButton1() if (jButton1 = null) jButton1 = new JButton(); jButton1.setText(9); jButton1.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) jTextField.setText(); start=fals
13、e; jTextField.setText(jTextField.getText()+input); ); return jButton1; /* * This method initializes jButton2 * * return javax.swing.JButton */ private JButton getJButton2() if (jButton2 = null) jButton2 = new JButton(); jButton2.setText(*); jButton2.addActionListener(new java.awt.event.ActionListene
14、r() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); calculate(Double.parseDouble(jTextField.getText(); lastCommand=input; start=true; ); return jButton2; /* * This method initializes jButton3 * * return javax.swing.JButton */ private JButton getJButton3()
15、 if (jButton3 = null) jButton3 = new JButton(); jButton3.setText(0); jButton3.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) jTextField.setText(); start=false; jTextField.setText(jTextField.
16、getText()+input); ); return jButton3; /* * This method initializes jButton4 * * return javax.swing.JButton */ private JButton getJButton4() if (jButton4 = null) jButton4 = new JButton(); jButton4.setText(.); jButton4.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(j
17、ava.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) jTextField.setText(); start=false; jTextField.setText(jTextField.getText()+input); ); return jButton4; /* * This method initializes jButton5 * * return javax.swing.JButton */ private JButton getJButton5() if (jButton5 = null)
18、jButton5 = new JButton(); jButton5.setText(=); jButton5.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); calculate(Double.parseDouble(jTextField.getText(); lastCommand=input; start=true; ); return jButt
19、on5; /* * This method initializes jButton6 * * return javax.swing.JButton */ private JButton getJButton6() if (jButton6 = null) jButton6 = new JButton(); jButton6.setText(8); jButton6.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) Stri
20、ng input=e.getActionCommand(); if(start) jTextField.setText(); start=false; jTextField.setText(jTextField.getText()+input); ); return jButton6; /* * This method initializes jButton7 * * return javax.swing.JButton */ private JButton getJButton7() if (jButton7 = null) jButton7 = new JButton(); jButton
21、7.setText(7); jButton7.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) jTextField.setText(); start=false; jTextField.setText(jTextField.getText()+input); ); return jButton7; /* * This method
22、initializes jButton8 * * return javax.swing.JButton */ private JButton getJButton8() if (jButton8 = null) jButton8 = new JButton(); jButton8.setText(2); jButton8.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCo
23、mmand(); if(start) jTextField.setText(); start=false; jTextField.setText(jTextField.getText()+input); ); return jButton8; /* * This method initializes jButton9 * * return javax.swing.JButton */ private JButton getJButton9() if (jButton9 = null) jButton9 = new JButton(); jButton9.setText(/); jButton9
24、.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); calculate(Double.parseDouble(jTextField.getText(); lastCommand=input; start=true; ); return jButton9; /* * This method initializes jButton10 * * return
25、javax.swing.JButton */ private JButton getJButton10() if (jButton10 = null) jButton10 = new JButton(); jButton10.setText(-); jButton10.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); calculate(Double.p
26、arseDouble(jTextField.getText(); lastCommand=input; start=true; ); return jButton10; /* * This method initializes jButton11 * * return javax.swing.JButton */ private JButton getJButton11() if (jButton11 = null) jButton11 = new JButton(); jButton11.setText(6); jButton11.addActionListener(new java.awt
27、.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) jTextField.setText(); start=false; jTextField.setText(jTextField.getText()+input); ); return jButton11; /* * This method initializes jButton12 * * return javax.swing.JButton */ private JButton getJButton12() if (jButton12 = null) jButton12 = new JButton(); jButton12.setText(3); jButton12.addActionListener(new java.awt.event.ActionListener() p
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1