简单的JAVA小程序(计算器)

只是自我的学习记录,里面可能有许多的缺陷。
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Calculator;

/**
*
* @author Administrator
*/
public class Calculator extends javax.swing.JFrame {
private int operation;
private int firstvalue;
private int secondevalue;

/**
* Creates new form Calculator
*/
public Calculator() {
operation=0;
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// //GEN-BEGIN:initComponents
private void initComponents() {

DividejButton = new javax.swing.JButton();
MuljButton = new javax.swing.JButton();
AmountjButton = new javax.swing.JButton();
OnejButton = new javax.swing.JButton();
TwojButton = new javax.swing.JButton();
ThreejButton = new javax.swing.JButton();
AddjButton = new javax.swing.JButton();
FourjButton = new javax.swing.JButton();
FivejButton = new javax.swing.JButton();
SixjButton = new javax.swing.JButton();
SubjButton = new javax.swing.JButton();
SevenjButton = new javax.swing.JButton();
EightjButton = new javax.swing.JButton();
NinejButton = new javax.swing.JButton();
ZerojButton = new javax.swing.JButton();
OutTextField = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
ClearjButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

DividejButton.setText("/");
DividejButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
DividejButtonActionPerformed(evt);
}
});

MuljButton.setText("*");
MuljButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MuljButtonActionPerformed(evt);
}
});

AmountjButton.setText("=");
AmountjButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AmountjButtonActionPerformed(evt);
}
});

OnejButton.setText("1");
OnejButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OnejButtonActionPerformed(evt);
}
});

TwojButton.setText("2");
TwojButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TwojButtonActionPerformed(evt);
}
});

ThreejButton.setText("3");
ThreejButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ThreejButtonActionPerformed(evt);
}
});

AddjButton.setText("+");
AddjButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
AddjButtonActionPerformed(evt);
}
});

FourjButton.setText("4");
FourjButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FourjButtonActionPerformed(evt);
}
});

FivejButton.setText("5");
FivejButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FivejButtonActionPerformed(evt);
}
});

SixjButton.setText("6");
SixjButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SixjButtonActionPerformed(evt);
}
});

SubjButton.setText("-");
SubjButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SubjButtonActionPerformed(evt);
}
});

SevenjButton.setText("7");
SevenjButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SevenjButtonActionPerformed(evt);
}
});

EightjButton.setText("8");
EightjButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
EightjButtonActionPerformed(evt);
}
});

NinejButton.setText("9");
NinejButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NinejButtonActionPerformed(evt);
}
});

ZerojButton.setText("0");
ZerojButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ZerojButtonActionPerformed(evt);
}
});

OutTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OutTextFieldActionPerformed(evt);
}
});

jLabel1.setText("结果");

ClearjButton.setText("C");
ClearjButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearjButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(SevenjButton)
.addComponent(FourjButton)
.addComponent(OnejButton)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(ClearjButton)))
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(EightjButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(NinejButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ZerojButton))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(DividejButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(MuljButton))
.addGroup(layout.createSequentialGroup()
.addComponent(TwojButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ThreejButton)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(AddjButton)
.addComponent(AmountjButton)))
.addGroup(layout.createSequentialGroup()
.addComponent(FivejButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SixjButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SubjButton))
.addComponent(OutTextField))
.addContainerGap(198, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(OutTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(DividejButton)
.addComponent(MuljButton)
.addComponent(AmountjButton)
.addComponent(ClearjButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(OnejButton)
.addComponent(TwojButton)
.addComponent(ThreejButton)
.addComponent(AddjButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(FourjButton)
.addComponent(FivejButton)
.addComponent(SixjButton)
.addComponent(SubjButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(SevenjButton)
.addComponent(EightjButton)
.addComponent(NinejButton)
.addComponent(ZerojButton))
.addContainerGap(97, Short.MAX_VALUE))
);

pack();
}// //GEN-END:initComponents

private void MuljButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MuljButtonActionPerformed
// TODO add your handling code here:
String firstStr = OutTextField.getText();
firstvalue = Integer.parseInt(firstStr);
OutTextField.setText("");
operation = 2;
}//GEN-LAST:event_MuljButtonActionPerformed

private void AmountjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AmountjButtonActionPerformed
// TODO add your handling code here:
secondevalue=Integer.parseInt(OutTextField.getText());
switch(operation){
case 1:{
Integer result=firstvalue+secondevalue;
OutTextField.setText(result.toString());
break;
}
case 2:{
Integer result=firstvalue-secondevalue;
OutTextField.setText(result.toString());
break;
}
case 3:
{
Integer result = firstvalue *secondevalue;
OutTextField.setText(result.toString());
break;
}
case 4:
{
Integer result = firstvalue *secondevalue;
OutTextField.setText(result.toString());
break;
}
default:
break;
}
}//GEN-LAST:event_AmountjButtonActionPerformed

private void ThreejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ThreejButtonActionPerformed
// TODO add your handling code here:
OutTextField.setText(OutTextField.getText()+3);
}//GEN-LAST:event_ThreejButtonActionPerformed

private void FivejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FivejButtonActionPerformed
// TODO add your handling code here:
OutTextField.setText(OutTextField.getText()+5);
}//GEN-LAST:event_FivejButtonActionPerformed

private void OutTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OutTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_OutTextFieldActionPerformed

private void DividejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DividejButtonActionPerformed
// TODO add your handling code here:
String firstStr = OutTextField.getText();
firstvalue = Integer.parseInt(firstStr);
OutTextField.setText("");
operation = 2;
}//GEN-LAST:event_DividejButtonActionPerformed

private void TwojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TwojButtonActionPerformed
// TODO add your handling code here:
OutTextField.setText(OutTextField.getText()+2);
}//GEN-LAST:event_TwojButtonActionPerformed

private void OnejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OnejButtonActionPerformed
// TODO add your handling code here:
OutTextField.setText(OutTextField.getText()+1);
}//GEN-LAST:event_OnejButtonActionPerformed

private void ClearjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearjButtonActionPerformed
// TODO add your handling code here:
OutTextField.setText("");
firstvalue=0;
secondevalue=0;
}//GEN-LAST:event_ClearjButtonActionPerformed

private void FourjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FourjButtonActionPerformed
// TODO add your handling code here:
OutTextField.setText(OutTextField.getText()+4);
}//GEN-LAST:event_FourjButtonActionPerformed

private void SixjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SixjButtonActionPerformed
// TODO add your handling code here:
OutTextField.setText(OutTextField.getText()+6);
}//GEN-LAST:event_SixjButtonActionPerformed

private void SevenjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SevenjButtonActionPerformed
// TODO add your handling code here:
OutTextField.setText(OutTextField.getText()+7);
}//GEN-LAST:event_SevenjButtonActionPerformed

private void EightjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EightjButtonActionPerformed
// TODO add your handling code here:
OutTextField.setText(OutTextField.getText()+8);
}//GEN-LAST:event_EightjButtonActionPerformed

private void NinejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NinejButtonActionPerformed
// TODO add your handling code here:
OutTextField.setText(OutTextField.getText()+9);
}//GEN-LAST:event_NinejButtonActionPerformed

private void ZerojButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ZerojButtonActionPerformed
// TODO add your handling code here:
OutTextField.setText(OutTextField.getText());
}//GEN-LAST:event_ZerojButtonActionPerformed

private void SubjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubjButtonActionPerformed
// TODO add your handling code here:
String firstStr = OutTextField.getText();
firstvalue = Integer.parseInt(firstStr);
OutTextField.setText("");
operation = 2;
}//GEN-LAST:event_SubjButtonActionPerformed

private void AddjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddjButtonActionPerformed
// TODO add your handling code here:
String firstStr=OutTextField.getText();
firstvalue=Integer.parseInt(firstStr);
OutTextField.setText("");
operation=1;
}//GEN-LAST:event_AddjButtonActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculator().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton AddjButton;
private javax.swing.JButton AmountjButton;
private javax.swing.JButton ClearjButton;
private javax.swing.JButton DividejButton;
private javax.swing.JButton EightjButton;
private javax.swing.JButton FivejButton;
private javax.swing.JButton FourjButton;
private javax.swing.JButton MuljButton;
private javax.swing.JButton NinejButton;
private javax.swing.JButton OnejButton;
private javax.swing.JTextField OutTextField;
private javax.swing.JButton SevenjButton;
private javax.swing.JButton SixjButton;
private javax.swing.JButton SubjButton;
private javax.swing.JButton ThreejButton;
private javax.swing.JButton TwojButton;
private javax.swing.JButton ZerojButton;
private javax.swing.JLabel jLabel1;
// End of variables declaration//GEN-END:variables
}

你可能感兴趣的:(Java的自我学习记录)