用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.
*/
/**
*
* @author gtt
*/
public class jisuanqi extends javax.swing.JFrame {
/**
* Creates new form jisuanqi
*/
public jisuanqi() {
initComponents();
}
int firstnum;
int secondnum;
int fangfa;
int result;
/**
* 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() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jPanel2 = new javax.swing.JPanel();
jButton_1 = new javax.swing.JButton();
jButton_2 = new javax.swing.JButton();
jButton_3 = new javax.swing.JButton();
jButton_add = new javax.swing.JButton();
jButton_4 = new javax.swing.JButton();
jButton_5 = new javax.swing.JButton();
jButton_6 = new javax.swing.JButton();
jButton_sub = new javax.swing.JButton();
jButton_7 = new javax.swing.JButton();
jButton_8 = new javax.swing.JButton();
jButton_9 = new javax.swing.JButton();
jButton_mul = new javax.swing.JButton();
jButton_0 = new javax.swing.JButton();
jButton_MC = new javax.swing.JButton();
jButton_r = new javax.swing.JButton();
jButton_div = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setLayout(new java.awt.GridLayout(2, 1));
jLabel1.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
jLabel1.setText(" 计算器");
jPanel1.add(jLabel1);
jPanel1.add(jTextField1);
jPanel2.setLayout(new java.awt.GridLayout(4, 4));
jButton_1.setText("1");
jButton_1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_1ActionPerformed(evt);
}
});
jPanel2.add(jButton_1);
jButton_2.setText("2");
jButton_2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_2ActionPerformed(evt);
}
});
jPanel2.add(jButton_2);
jButton_3.setText("3");
jButton_3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_3ActionPerformed(evt);
}
});
jPanel2.add(jButton_3);
jButton_add.setText("+");
jButton_add.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_addActionPerformed(evt);
}
});
jPanel2.add(jButton_add);
jButton_4.setText("4");
jButton_4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_4ActionPerformed(evt);
}
});
jPanel2.add(jButton_4);
jButton_5.setText("5");
jButton_5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_5ActionPerformed(evt);
}
});
jPanel2.add(jButton_5);
jButton_6.setText("6");
jButton_6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_6ActionPerformed(evt);
}
});
jPanel2.add(jButton_6);
jButton_sub.setText("-");
jButton_sub.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_subActionPerformed(evt);
}
});
jPanel2.add(jButton_sub);
jButton_7.setText("7");
jButton_7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_7ActionPerformed(evt);
}
});
jPanel2.add(jButton_7);
jButton_8.setText("8");
jButton_8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_8ActionPerformed(evt);
}
});
jPanel2.add(jButton_8);
jButton_9.setText("9");
jButton_9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_9ActionPerformed(evt);
}
});
jPanel2.add(jButton_9);
jButton_mul.setText("*");
jButton_mul.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_mulActionPerformed(evt);
}
});
jPanel2.add(jButton_mul);
jButton_0.setText("0");
jButton_0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_0ActionPerformed(evt);
}
});
jPanel2.add(jButton_0);
jButton_MC.setText("MC");
jButton_MC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_MCActionPerformed(evt);
}
});
jPanel2.add(jButton_MC);
jButton_r.setText("=");
jButton_r.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_rActionPerformed(evt);
}
});
jPanel2.add(jButton_r);
jButton_div.setText("/");
jButton_div.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_divActionPerformed(evt);
}
});
jPanel2.add(jButton_div);
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(114, 114, 114)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(166, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(38, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton_1ActionPerformed(java.awt.event.ActionEvent evt) {
// 输入数据1
jTextField1.setText(jTextField1.getText()+"1");
}
private void jButton_2ActionPerformed(java.awt.event.ActionEvent evt) {
// 输入数据2
jTextField1.setText(jTextField1.getText()+"2");
}
private void jButton_3ActionPerformed(java.awt.event.ActionEvent evt) {
// 输入数据3
jTextField1.setText(jTextField1.getText()+"3");
}
private void jButton_4ActionPerformed(java.awt.event.ActionEvent evt) {
// 输入数据4
jTextField1.setText(jTextField1.getText()+"4");
}
private void jButton_5ActionPerformed(java.awt.event.ActionEvent evt) {
// 输入数据5
jTextField1.setText(jTextField1.getText()+"5");
}
private void jButton_6ActionPerformed(java.awt.event.ActionEvent evt) {
// 输入数据6
jTextField1.setText(jTextField1.getText()+"6");
}
private void jButton_7ActionPerformed(java.awt.event.ActionEvent evt) {
// 输入数据7
jTextField1.setText(jTextField1.getText()+"7");
}
private void jButton_8ActionPerformed(java.awt.event.ActionEvent evt) {
// 输入数据8
jTextField1.setText(jTextField1.getText()+"8");
}
private void jButton_9ActionPerformed(java.awt.event.ActionEvent evt) {
// 输入数据9
jTextField1.setText(jTextField1.getText()+"9");
}
private void jButton_0ActionPerformed(java.awt.event.ActionEvent evt) {
//输入数据0
jTextField1.setText(jTextField1.getText()+"0");
}
private void jButton_MCActionPerformed(java.awt.event.ActionEvent evt) {
//清零
jTextField1.setText(null);
}
private void jButton_addActionPerformed(java.awt.event.ActionEvent evt) {
// 获取第一个数据
String firstv=jTextField1.getText();
firstnum=Integer.parseInt(firstv);
//设置方法
fangfa=1;
//清空
jTextField1.setText(null);
}
private void jButton_subActionPerformed(java.awt.event.ActionEvent evt) {
// 获取第一个数据
String firstv=jTextField1.getText();
firstnum=Integer.parseInt(firstv);
//设置方法
fangfa=2;
//清空
jTextField1.setText(null);
}
private void jButton_mulActionPerformed(java.awt.event.ActionEvent evt) {
// 获取第一个数据
String firstv=jTextField1.getText();
firstnum=Integer.parseInt(firstv);
//设置方法
fangfa=3;
//清空
jTextField1.setText(null);
}
private void jButton_divActionPerformed(java.awt.event.ActionEvent evt) {
// 获取第一个数据
String firstv=jTextField1.getText();
firstnum=Integer.parseInt(firstv);
//设置方法
fangfa=4;
//清空
jTextField1.setText(null);
}
private void jButton_rActionPerformed(java.awt.event.ActionEvent evt) {
// 获取第二个数据
String secondv = jTextField1.getText();
secondnum=Integer.parseInt(secondv);
//根据方法运算
switch(fangfa){
case 1:
result=firstnum+secondnum;
break;
case 2:
result=firstnum-secondnum;
break;
case 3:
result=firstnum*secondnum;
break;
case 4:
result=firstnum/secondnum;
break;
}
//显示结果
String h = String.valueOf(result);
jTextField1.setText(h);
}
/**
* @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(jisuanqi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(jisuanqi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(jisuanqi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(jisuanqi.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 jisuanqi().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton_0;
private javax.swing.JButton jButton_1;
private javax.swing.JButton jButton_2;
private javax.swing.JButton jButton_3;
private javax.swing.JButton jButton_4;
private javax.swing.JButton jButton_5;
private javax.swing.JButton jButton_6;
private javax.swing.JButton jButton_7;
private javax.swing.JButton jButton_8;
private javax.swing.JButton jButton_9;
private javax.swing.JButton jButton_MC;
private javax.swing.JButton jButton_add;
private javax.swing.JButton jButton_div;
private javax.swing.JButton jButton_mul;
private javax.swing.JButton jButton_r;
private javax.swing.JButton jButton_sub;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
界面如下: