第一个Java程序——计算器

第一次写的,一边写一边修改,很简单的整数计算器,拿来抛砖引玉吧

  1 /*

  2 *简单的计算器

  3 *没有写注释,不过很简单的小程序估计也没人看吧┑( ̄Д  ̄)┍

  4 */

  5 

  6 package com.Calculator;

  7 

  8 import javax.swing.SwingUtilities;

  9 import java.awt.BorderLayout;

 10 import javax.swing.JPanel;

 11 import javax.swing.JFrame;

 12 import java.awt.FlowLayout;

 13 import java.awt.GridBagLayout;

 14 import java.awt.GridLayout;

 15 import java.awt.Toolkit;

 16 

 17 import javax.swing.JButton;

 18 import java.awt.Dimension;

 19 import java.awt.Font;

 20 import javax.swing.JTextField;

 21 import java.awt.GridBagConstraints;

 22 

 23 public class CalculatorDeom extends JFrame {

 24 

 25 

 26     public String st1="";  //  @jve:decl-index=0:

 27     public String st2="";  //  @jve:decl-index=0:

 28     public int mao=0;

 29     //  @jve:decl-index=0:

 30     

 31     private static final long serialVersionUID = 1L;

 32     private JPanel jContentPane = null;

 33     private JPanel jPanel = null;

 34     private JPanel jPanel1 = null;

 35     private JButton jButton = null;

 36     private JTextField jTextField = null;

 37     private JButton jButton1 = null;

 38     private JButton jButton2 = null;

 39     private JButton jButton3 = null;

 40     private JButton jButton4 = null;

 41     private JButton jButton5 = null;

 42     private JButton jButton6 = null;

 43     private JButton jButton7 = null;

 44     private JButton jButton8 = null;

 45     private JButton jButton9 = null;

 46     private JButton jButton10 = null;

 47     private JButton jButton11 = null;

 48     private JButton jButton12 = null;

 49     private JButton jButton13 = null;

 50     private JButton jButton14 = null;

 51     private JButton jButton15 = null;

 52 

 53     /**

 54      * This method initializes jPanel    

 55      *     

 56      * @return javax.swing.JPanel    

 57      */

 58     private JPanel getJPanel() {

 59         if (jPanel == null) {

 60             jPanel = new JPanel();

 61             jPanel.setLayout(new FlowLayout());

 62             jPanel.add(getJTextField(), null);

 63         }

 64         return jPanel;

 65     }

 66 

 67     /**

 68      * This method initializes jPanel1    

 69      *     

 70      * @return javax.swing.JPanel    

 71      */

 72     private JPanel getJPanel1() {

 73         if (jPanel1 == null) {

 74             GridLayout gridLayout = new GridLayout();

 75             gridLayout.setRows(4);

 76             gridLayout.setHgap(10);

 77             gridLayout.setVgap(10);

 78             gridLayout.setColumns(4);

 79             jPanel1 = new JPanel();

 80             jPanel1.setLayout(gridLayout);

 81             jPanel1.add(getJButton(), null);

 82             jPanel1.add(getJButton1(), null);

 83             jPanel1.add(getJButton2(), null);

 84             jPanel1.add(getJButton3(), null);

 85             jPanel1.add(getJButton4(), null);

 86             jPanel1.add(getJButton5(), null);

 87             jPanel1.add(getJButton6(), null);

 88             jPanel1.add(getJButton7(), null);

 89             jPanel1.add(getJButton8(), null);

 90             jPanel1.add(getJButton9(), null);

 91             jPanel1.add(getJButton10(), null);

 92             jPanel1.add(getJButton11(), null);

 93             jPanel1.add(getJButton12(), null);

 94             jPanel1.add(getJButton13(), null);

 95             jPanel1.add(getJButton14(), null);

 96             jPanel1.add(getJButton15(), null);

 97         }

 98         return jPanel1;

 99     }

100 

101     /**

102      * This method initializes jButton    

103      *     

104      * @return javax.swing.JButton    

105      */

106     private JButton getJButton() {

107         if (jButton == null) {

108             jButton = new JButton();

109             jButton.setPreferredSize(new Dimension(70, 50));

110             jButton.setText("1");

111             jButton.setFont(new Font("Times New Roman", Font.BOLD, 18));

112             jButton.addActionListener(new java.awt.event.ActionListener() {

113                 public void actionPerformed(java.awt.event.ActionEvent e) {

114                     st1=st1+jButton.getText();

115                     jTextField.setText(st1);

116                 }

117             });

118         }

119         return jButton;

120     }

121 

122     /**

123      * This method initializes jTextField    

124      *     

125      * @return javax.swing.JTextField    

126      */

127     private JTextField getJTextField() {

128         if (jTextField == null) {

129             jTextField = new JTextField();

130             jTextField.setColumns(21);

131             jTextField.setEditable(false);

132             

133             jTextField.setFont(new Font("Times New Roman", Font.PLAIN, 18));

134             jTextField.setPreferredSize(new Dimension(279, 60));

135         }

136         return jTextField;

137     }

138 

139     /**

140      * This method initializes jButton1    

141      *     

142      * @return javax.swing.JButton    

143      */

144     private JButton getJButton1() {

145         if (jButton1 == null) {

146             jButton1 = new JButton();

147             jButton1.setFont(new Font("Times New Roman", Font.BOLD, 18));

148             jButton1.setText("2");

149             jButton1.setPreferredSize(new Dimension(70, 50));

150             jButton1.addActionListener(new java.awt.event.ActionListener() {

151                 public void actionPerformed(java.awt.event.ActionEvent e) {

152                     st1=st1+jButton1.getText();

153                     jTextField.setText(st1);

154                 }

155             });

156         }

157         return jButton1;

158     }

159 

160     /**

161      * This method initializes jButton2    

162      *     

163      * @return javax.swing.JButton    

164      */

165     private JButton getJButton2() {

166         if (jButton2 == null) {

167             jButton2 = new JButton();

168             jButton2.setFont(new Font("Times New Roman", Font.BOLD, 18));

169             jButton2.setText("3");

170             jButton2.setPreferredSize(new Dimension(70, 50));

171             jButton2.addActionListener(new java.awt.event.ActionListener() {

172                 public void actionPerformed(java.awt.event.ActionEvent e) {

173                     st1=st1+jButton2.getText();

174                     jTextField.setText(st1);

175                 }

176             });

177         }

178         return jButton2;

179     }

180 

181     /**

182      * This method initializes jButton3    

183      *     

184      * @return javax.swing.JButton    

185      */

186     private JButton getJButton3() {

187         if (jButton3 == null) {

188             jButton3 = new JButton();

189             jButton3.setFont(new Font("Times New Roman", Font.BOLD, 18));

190             jButton3.setText("÷");

191             jButton3.setPreferredSize(new Dimension(70, 50));

192             jButton3.addActionListener(new java.awt.event.ActionListener() {

193                 public void actionPerformed(java.awt.event.ActionEvent e) {

194                     st2=st1;

195                     st1="";

196                     jTextField.setText(st1);

197                     mao=4;

198                 }

199             });

200         }

201         return jButton3;

202     }

203 

204     /**

205      * This method initializes jButton4    

206      *     

207      * @return javax.swing.JButton    

208      */

209     private JButton getJButton4() {

210         if (jButton4 == null) {

211             jButton4 = new JButton();

212             jButton4.setFont(new Font("Times New Roman", Font.BOLD, 18));

213             jButton4.setText("4");

214             jButton4.setPreferredSize(new Dimension(70, 50));

215             jButton4.addActionListener(new java.awt.event.ActionListener() {

216                 public void actionPerformed(java.awt.event.ActionEvent e) {

217                     st1=st1+jButton4.getText();

218                     jTextField.setText(st1);

219                 }

220             });

221         }

222         return jButton4;

223     }

224 

225     /**

226      * This method initializes jButton5    

227      *     

228      * @return javax.swing.JButton    

229      */

230     private JButton getJButton5() {

231         if (jButton5 == null) {

232             jButton5 = new JButton();

233             jButton5.setFont(new Font("Times New Roman", Font.BOLD, 18));

234             jButton5.setText("5");

235             jButton5.setPreferredSize(new Dimension(70, 50));

236             jButton5.addActionListener(new java.awt.event.ActionListener() {

237                 public void actionPerformed(java.awt.event.ActionEvent e) {

238                     st1=st1+jButton5.getText();

239                     jTextField.setText(st1);

240                 }

241             });

242         }

243         return jButton5;

244     }

245 

246     /**

247      * This method initializes jButton6    

248      *     

249      * @return javax.swing.JButton    

250      */

251     private JButton getJButton6() {

252         if (jButton6 == null) {

253             jButton6 = new JButton();

254             jButton6.setFont(new Font("Times New Roman", Font.BOLD, 18));

255             jButton6.setText("6");

256             jButton6.setPreferredSize(new Dimension(70, 50));

257             jButton6.addActionListener(new java.awt.event.ActionListener() {

258                 public void actionPerformed(java.awt.event.ActionEvent e) {

259                     st1=st1+jButton6.getText();

260                     jTextField.setText(st1);

261                 }

262             });

263         }

264         return jButton6;

265     }

266 

267     /**

268      * This method initializes jButton7    

269      *     

270      * @return javax.swing.JButton    

271      */

272     private JButton getJButton7() {

273         if (jButton7 == null) {

274             jButton7 = new JButton();

275             jButton7.setFont(new Font("Times New Roman", Font.BOLD, 18));

276             jButton7.setText("×");

277             jButton7.setPreferredSize(new Dimension(70, 50));

278             jButton7.addActionListener(new java.awt.event.ActionListener() {

279                 public void actionPerformed(java.awt.event.ActionEvent e) {

280                     st2=st1;

281                     st1="";

282                     jTextField.setText(st1);

283                     mao=3;

284                 }

285             });

286         }

287         return jButton7;

288     }

289 

290     /**

291      * This method initializes jButton8    

292      *     

293      * @return javax.swing.JButton    

294      */

295     private JButton getJButton8() {

296         if (jButton8 == null) {

297             jButton8 = new JButton();

298             jButton8.setFont(new Font("Times New Roman", Font.BOLD, 18));

299             jButton8.setText("7");

300             jButton8.setPreferredSize(new Dimension(70, 50));

301             jButton8.addActionListener(new java.awt.event.ActionListener() {

302                 public void actionPerformed(java.awt.event.ActionEvent e) {

303                     st1=st1+jButton8.getText();

304                     jTextField.setText(st1);

305                 }

306             });

307         }

308         return jButton8;

309     }

310 

311     /**

312      * This method initializes jButton9    

313      *     

314      * @return javax.swing.JButton    

315      */

316     private JButton getJButton9() {

317         if (jButton9 == null) {

318             jButton9 = new JButton();

319             jButton9.setFont(new Font("Times New Roman", Font.BOLD, 18));

320             jButton9.setText("8");

321             jButton9.setPreferredSize(new Dimension(70, 50));

322             jButton9.addActionListener(new java.awt.event.ActionListener() {

323                 public void actionPerformed(java.awt.event.ActionEvent e) {

324                     st1=st1+jButton9.getText();

325                     jTextField.setText(st1);

326                 }

327             });

328         }

329         return jButton9;

330     }

331 

332     /**

333      * This method initializes jButton10    

334      *     

335      * @return javax.swing.JButton    

336      */

337     private JButton getJButton10() {

338         if (jButton10 == null) {

339             jButton10 = new JButton();

340             jButton10.setFont(new Font("Times New Roman", Font.BOLD, 18));

341             jButton10.setText("9");

342             jButton10.setPreferredSize(new Dimension(70, 50));

343             jButton10.addActionListener(new java.awt.event.ActionListener() {

344                 public void actionPerformed(java.awt.event.ActionEvent e) {

345                     st1=st1+jButton10.getText();

346                     jTextField.setText(st1);

347                 }

348             });

349         }

350         return jButton10;

351     }

352 

353     /**

354      * This method initializes jButton11    

355      *     

356      * @return javax.swing.JButton    

357      */

358     private JButton getJButton11() {

359         if (jButton11 == null) {

360             jButton11 = new JButton();

361             jButton11.setFont(new Font("Times New Roman", Font.BOLD, 18));

362             jButton11.setText("+");

363             jButton11.setPreferredSize(new Dimension(70, 50));

364             jButton11.addActionListener(new java.awt.event.ActionListener() {

365                 public void actionPerformed(java.awt.event.ActionEvent e) {

366                     st2=st1;

367                     st1="";

368                     jTextField.setText(st1);

369                     mao=1;

370                 }

371             });

372         }

373         return jButton11;

374     }

375 

376     /**

377      * This method initializes jButton12    

378      *     

379      * @return javax.swing.JButton    

380      */

381     private JButton getJButton12() {

382         if (jButton12 == null) {

383             jButton12 = new JButton();

384             jButton12.setFont(new Font("Times New Roman", Font.BOLD, 18));

385             jButton12.setText("0");

386             jButton12.setPreferredSize(new Dimension(70, 50));

387             jButton12.addActionListener(new java.awt.event.ActionListener() {

388                 public void actionPerformed(java.awt.event.ActionEvent e) {

389                     st1=st1+jButton12.getText();

390                     jTextField.setText(st1);

391                 }

392             });

393         }

394         return jButton12;

395     }

396 

397     /**

398      * This method initializes jButton13    

399      *     

400      * @return javax.swing.JButton    

401      */

402     private JButton getJButton13() {

403         if (jButton13 == null) {

404             jButton13 = new JButton();

405             jButton13.setFont(new Font("Times New Roman", Font.BOLD, 18));

406             jButton13.setText("C");

407             jButton13.setPreferredSize(new Dimension(70, 50));

408             jButton13.addActionListener(new java.awt.event.ActionListener() {   

409                 public void actionPerformed(java.awt.event.ActionEvent e) {    

410                     st1="";

411                     st2="";

412                     mao=0;

413                     jTextField.setText(st1);

414                 }

415             

416             });

417         }

418         return jButton13;

419     }

420 

421     /**

422      * This method initializes jButton14    

423      *     

424      * @return javax.swing.JButton    

425      */

426     private JButton getJButton14() {

427         if (jButton14 == null) {

428             jButton14 = new JButton();

429             jButton14.setFont(new Font("Times New Roman", Font.BOLD, 18));

430             jButton14.setText("=");

431             jButton14.setPreferredSize(new Dimension(70, 50));

432             jButton14.addActionListener(new java.awt.event.ActionListener() {

433                 public void actionPerformed(java.awt.event.ActionEvent e) {

434                     if (mao==1) {

435                         int i=(Integer.valueOf(st2).intValue())+(Integer.valueOf(st1).intValue());

436                         st2=String.valueOf(i);

437                         jTextField.setText(st2);

438                         st1="";

439                         mao=0;

440                     }if (mao==2) {

441                         int i=(Integer.valueOf(st2).intValue())-(Integer.valueOf(st1).intValue());

442                         st2=String.valueOf(i);

443                         jTextField.setText(st2);

444                         st1="";

445                         mao=0;

446                     }if (mao==3) {

447                         int i=(Integer.valueOf(st2).intValue())*(Integer.valueOf(st1).intValue());

448                         st2=String.valueOf(i);

449                         jTextField.setText(st2);

450                         st1="";

451                         mao=0;

452                     }if (mao==4) {

453                         try {

454                             int i=(Integer.valueOf(st2).intValue())/(Integer.valueOf(st1).intValue());

455                             st2=String.valueOf(i);

456                             jTextField.setText(st2);

457                             st1="";

458                             mao=0;

459                         } catch (Exception e2) {

460                             // TODO: handle exception

461                             jTextField.setText("By/Zore");

462                         }

463                     }

464                 }

465             });

466         }

467         return jButton14;

468     }

469 

470     /**

471      * This method initializes jButton15    

472      *     

473      * @return javax.swing.JButton    

474      */

475     private JButton getJButton15() {

476         if (jButton15 == null) {

477             jButton15 = new JButton();

478             jButton15.setFont(new Font("Times New Roman", Font.BOLD, 18));

479             jButton15.setText("-");

480             jButton15.setPreferredSize(new Dimension(70, 50));

481             jButton15.addActionListener(new java.awt.event.ActionListener() {

482                 public void actionPerformed(java.awt.event.ActionEvent e) {

483                     st2=st1;

484                     st1="";

485                     jTextField.setText(st1);

486                     mao=2;

487                 }

488             });

489         }

490         return jButton15;

491     }

492 

493     /**

494      * @param args

495      */

496     public static void main(String[] args) {

497         // TODO Auto-generated method stub

498         SwingUtilities.invokeLater(new Runnable() {

499             public void run() {

500                 CalculatorDeom thisClass = new CalculatorDeom();

501                 thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

502                 thisClass.setVisible(true);

503             }

504         });

505     }

506 

507     /**

508      * This is the default constructor

509      */

510     public CalculatorDeom() {

511         super();

512         initialize();

513     }

514 

515     /**

516      * This method initializes this

517      * 

518      * @return void

519      */

520     private void initialize() {

521         this.setSize(336, 351);

522         Toolkit tk = Toolkit.getDefaultToolkit();

523         Dimension ds = tk.getScreenSize();

524         int x = (int)(ds.getWidth()-336)/2;

525         int y = (int)(ds.getHeight()-351)/2;

526         this.setLocation(x, y);

527         this.setResizable(false);

528         this.setContentPane(getJContentPane());

529         this.setTitle("JFrame");

530         this.setVisible(true);

531     }

532 

533     /**

534      * This method initializes jContentPane

535      * 

536      * @return javax.swing.JPanel

537      */

538     private JPanel getJContentPane() {

539         if (jContentPane == null) {

540             jContentPane = new JPanel();

541             jContentPane.setLayout(new FlowLayout());

542             jContentPane.add(getJPanel(), null);

543             jContentPane.add(getJPanel1(), null);

544         }

545         return jContentPane;

546     }

547 

548 }  //  @jve:decl-index=0:visual-constraint="10,10"

一个多小时的时间,难免有很多错误,等完成作业了在尝试着改进下吧~~

你可能感兴趣的:(java)