您的位置:首页 > 编程语言 > Java开发

简易计算器Netbeans 图形界面

2015-09-16 21:12 281 查看
刚开始学习使用Netbeans,鼓捣了半天,总算是有点头绪了,作出一个简易的计算器,功能还有待改进

/*
* 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 javaapplication3;

/**
*
* @author HP-Developer
*/
public class Calculator extends javax.swing.JFrame {
private double number1;
private double number2;
private String operator;
private double result;
/**
* Creates new form Calculator
*/
public Calculator() {
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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jButton11 = new javax.swing.JButton();
jButton12 = new javax.swing.JButton();
jButton13 = new javax.swing.JButton();
jButton14 = new javax.swing.JButton();
jButton15 = new javax.swing.JButton();
jButton16 = new javax.swing.JButton();
jButton17 = new javax.swing.JButton();
jButton18 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

jButton17.setText(".");
jButton17.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton17ActionPerformed(evt);
}
});

jButton18.setText("D");
jButton18.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton18ActionPerformed(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()
.addGap(60, 60, 60)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton6)
.addGap(18, 18, 18)
.addComponent(jButton18)
.addGap(18, 18, 18)
.addComponent(jButton4))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton8)
.addGap(18, 18, 18)
.addComponent(jButton9)
.addGap(18, 18, 18)
.addComponent(jButton10))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton11)
.addGap(18, 18, 18)
.addComponent(jButton12)
.addGap(18, 18, 18)
.addComponent(jButton13))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton14)
.addGap(18, 18, 18)
.addComponent(jButton15))
.addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton16))))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton5))))
.addContainerGap(60, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton4)
.addComponent(jButton6)
.addComponent(jButton18)
.addComponent(jButton3))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton8)
.addComponent(jButton9)
.addComponent(jButton10)
.addComponent(jButton2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton11)
.addComponent(jButton12)
.addComponent(jButton13)
.addComponent(jButton1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton14)
.addComponent(jButton15)
.addComponent(jButton16))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton7)
.addComponent(jButton17)))
.addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(22, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//        String test=evt.getActionCommand();
//        System.out.println(test);
numberPressed(evt);
}

private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
numberPressed(evt);        // TODO add your handling code here:
}

private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
numberPressed(evt);        // TODO add your handling code here:
}

private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
numberPressed(evt);        // TODO add your handling code here:
}

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
numberPressed(evt);        // TODO add your handling code here:
}

private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
numberPressed(evt);        // TODO add your handling code here:
}

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
numberPressed(evt);        // TODO add your handling code here:
}

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
numberPressed(evt);        // TODO add your handling code here:
}

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
numberPressed(evt);        // TODO add your handling code here:
}

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
numberPressed(evt);        // TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
operatorPressed(evt);
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
operatorPressed(evt);        // TODO add your handling code here:
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
operatorPressed(evt);        // TODO add your handling code here:
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
operatorPressed(evt);        // TODO add your handling code here:
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String tempString=jTextField1.getText();
System.out.println(tempString);
System.out.println(operator);
int operatorIndex=tempString.indexOf(operator);
String stringNumber2=tempString.substring(operatorIndex+1);
System.out.println(stringNumber2);
number2=Double.valueOf(stringNumber2);
switch(operator){
case "+":
result=number1+number2;
jTextField1.setText(""+result);
break;
case "-":
result=number1-number2;
jTextField1.setText(""+result);
break;
case "*":
result=number1*number2;
jTextField1.setText(""+result);
break;
case "/":
if(number2==0){
jTextField1.setText("除数不能为0!");
}else{
result=number1/number2;
jTextField1.setText(""+result);
break;
}

}
}

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");       // TODO add your handling code here:
}

private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
// 小数点
String numberPointName=evt.getActionCommand();
jTextField1.setText(jTextField1.getText()+numberPointName);
//        number1=jTextField1.setText(jTextField1.getText()+numberPointName);

}

private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
// 删除键
String delete=evt.getActionCommand();
String x=jTextField1.getText();
x=x.substring(0,x.length()-1);
jTextField1.setText(x);

//        double y=Double.valueOf(x);

//          bt_clear=new JButton("clear");
//          bt_jisuan=new JButton("=");
}
public void operatorPressed(java.awt.event.ActionEvent evt){
String stringNumber1=jTextField1.getText();
number1=Double.valueOf(stringNumber1);
String operatorButtonName=evt.getActionCommand();
operator=operatorButtonName;
//jTextField1.setText("+");
jTextField1.setText(jTextField1.getText()+operatorButtonName);
}
public void numberPressed(java.awt.event.ActionEvent evt){
String numberButtonName=evt.getActionCommand();

jTextField1.setText(jTextField1.getText()+numberButtonName);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* 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);
}
//</editor-fold>

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

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton13;
private javax.swing.JButton jButton14;
private javax.swing.JButton jButton15;
private javax.swing.JButton jButton16;
private javax.swing.JButton jButton17;
private javax.swing.JButton jButton18;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: