Java版計算器

java版的計算器

功能特點:

1.不接收鍵盤輸入。

2.支持+、-、*、/、乘方和開方運算。

3.支持清除和退格。

4.可以顯示運算表達式。

5.可以輸入.5、-.5格式的小數,並在表達式中格式化爲0.5、-0.5格式。

6.若輸入框中無內容,輸入0後自動補加小數點。

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

/**
*
* @author 中強
*/
public class Calculator extends javax.swing.JFrame {

/**
* 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() {

jPanel1 = new javax.swing.JPanel();
jLabelResult = new javax.swing.JLabel();
jTextFieldInputOutput = new javax.swing.JTextField();
jPanel2 = new javax.swing.JPanel();
jButtonSqrt = new javax.swing.JButton();
jButtonPower = new javax.swing.JButton();
jButtonClear = new javax.swing.JButton();
jButtonBackspace = new javax.swing.JButton();
jButtonSeven = new javax.swing.JButton();
jButtonEight = new javax.swing.JButton();
jButtonNine = new javax.swing.JButton();
jButtonDivide = new javax.swing.JButton();
jButtonFour = new javax.swing.JButton();
jButtonFive = new javax.swing.JButton();
jButtonSix = new javax.swing.JButton();
jButtonMultiply = new javax.swing.JButton();
jButtonOne = new javax.swing.JButton();
jButtonTwo = new javax.swing.JButton();
jButtonThree = new javax.swing.JButton();
jButtonMinus = new javax.swing.JButton();
jButtonPlus = new javax.swing.JButton();
jButtonEquals = new javax.swing.JButton();
jButtonDot = new javax.swing.JButton();
jButtonZero = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("計算器");
setResizable(false);

jLabelResult.setFont(new java.awt.Font("微軟雅黑", 0, 14)); // NOI18N
jLabelResult.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);

jTextFieldInputOutput.setEditable(false);
jTextFieldInputOutput.setFont(new java.awt.Font("微軟雅黑", 0, 24)); // NOI18N
jTextFieldInputOutput.setForeground(new java.awt.Color(0, 0, 255));
jTextFieldInputOutput.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabelResult, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextFieldInputOutput))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabelResult, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextFieldInputOutput, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
.addContainerGap())
);

jButtonSqrt.setFont(new java.awt.Font("微軟雅黑", 0, 12)); // NOI18N
jButtonSqrt.setText("開方");
jButtonSqrt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSqrtActionPerformed(evt);
}
});

jButtonPower.setFont(new java.awt.Font("微軟雅黑", 0, 12)); // NOI18N
jButtonPower.setText("乘方");
jButtonPower.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonPowerActionPerformed(evt);
}
});

jButtonClear.setFont(new java.awt.Font("微軟雅黑", 0, 12)); // NOI18N
jButtonClear.setText("清除");
jButtonClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonClearActionPerformed(evt);
}
});

jButtonBackspace.setFont(new java.awt.Font("微軟雅黑", 0, 12)); // NOI18N
jButtonBackspace.setText("退格");
jButtonBackspace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonBackspaceActionPerformed(evt);
}
});

jButtonSeven.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonSeven.setText("7");
jButtonSeven.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSevenActionPerformed(evt);
}
});

jButtonEight.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonEight.setText("8");
jButtonEight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonEightActionPerformed(evt);
}
});

jButtonNine.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonNine.setText("9");
jButtonNine.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonNineActionPerformed(evt);
}
});

jButtonDivide.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonDivide.setText("/");
jButtonDivide.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDivideActionPerformed(evt);
}
});

jButtonFour.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonFour.setText("4");
jButtonFour.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonFourActionPerformed(evt);
}
});

jButtonFive.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonFive.setText("5");
jButtonFive.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonFiveActionPerformed(evt);
}
});

jButtonSix.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonSix.setText("6");
jButtonSix.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSixActionPerformed(evt);
}
});

jButtonMultiply.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonMultiply.setText("*");
jButtonMultiply.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonMultiplyActionPerformed(evt);
}
});

jButtonOne.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonOne.setText("1");
jButtonOne.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonOneActionPerformed(evt);
}
});

jButtonTwo.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonTwo.setText("2");
jButtonTwo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonTwoActionPerformed(evt);
}
});

jButtonThree.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonThree.setText("3");
jButtonThree.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonThreeActionPerformed(evt);
}
});

jButtonMinus.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonMinus.setText("-");
jButtonMinus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonMinusActionPerformed(evt);
}
});

jButtonPlus.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonPlus.setText("+");
jButtonPlus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonPlusActionPerformed(evt);
}
});

jButtonEquals.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonEquals.setText("=");
jButtonEquals.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonEqualsActionPerformed(evt);
}
});

jButtonDot.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonDot.setText(".");
jButtonDot.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDotActionPerformed(evt);
}
});

jButtonZero.setFont(new java.awt.Font("Comic Sans MS", 1, 36)); // NOI18N
jButtonZero.setText("0");
jButtonZero.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonZeroActionPerformed(evt);
}
});

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButtonSqrt)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonPower)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonClear)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonBackspace))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButtonSeven)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonEight)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonNine)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonDivide))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButtonFour)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonFive)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonSix)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonMultiply))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButtonOne)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonTwo)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonThree)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonMinus))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jButtonZero)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonDot)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonEquals)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonPlus)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonSqrt)
.addComponent(jButtonPower)
.addComponent(jButtonClear)
.addComponent(jButtonBackspace))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonSeven)
.addComponent(jButtonEight)
.addComponent(jButtonNine)
.addComponent(jButtonDivide))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonFour)
.addComponent(jButtonFive)
.addComponent(jButtonSix)
.addComponent(jButtonMultiply))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonOne)
.addComponent(jButtonTwo)
.addComponent(jButtonThree)
.addComponent(jButtonMinus))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonZero)
.addComponent(jButtonDot)
.addComponent(jButtonEquals)
.addComponent(jButtonPlus))
.addContainerGap(18, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);

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

private void jButtonSevenActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
jTextFieldInputOutput.setText(input + "7");
}

private void jButtonEightActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
jTextFieldInputOutput.setText(input + "8");
}

private void jButtonNineActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
jTextFieldInputOutput.setText(input + "9");
}

private void jButtonFourActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
jTextFieldInputOutput.setText(input + "4");
}

private void jButtonFiveActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
jTextFieldInputOutput.setText(input + "5");
}

private void jButtonSixActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
jTextFieldInputOutput.setText(input + "6");
}

private void jButtonOneActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
jTextFieldInputOutput.setText(input + "1");
}

private void jButtonTwoActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
jTextFieldInputOutput.setText(input + "2");
}

private void jButtonThreeActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
jTextFieldInputOutput.setText(input + "3");
}

private void jButtonZeroActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
if ("0".equals(input) || "-0".equals(input)) {
return;
}
jTextFieldInputOutput.setText(input + "0");
input = jTextFieldInputOutput.getText();
if ("0".equals(input) || "-0".equals(input)) {
jTextFieldInputOutput.setText(input + ".");
}
}

private void jButtonDotActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
if (input.indexOf('.') >= 0) {
return;
}
jTextFieldInputOutput.setText(input + ".");
}

private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {
jTextFieldInputOutput.setText("");
jLabelResult.setText("");
d1 = 0.0;
d2 = 0.0;
d3 = 0.0;
opt = "";
}

private void jButtonBackspaceActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
if (!input.equals("") && input != null) {
jTextFieldInputOutput.setText(input.substring(0, input.length() - 1));
}
}

private void jButtonPlusActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
if (!input.equals("") && input != null) {
d1 = Double.parseDouble(input);
opt = "+";
input = numberStringFormat(input);
jLabelResult.setText(input + opt);
jTextFieldInputOutput.setText("");
}
}

private void jButtonEqualsActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
if ((("").equals(input) || input == null)
|| ("".equals(opt) || opt == null)){
return;
}
if ("-".equals(input)) {
JOptionPane.showMessageDialog(getParent(), "請輸入完整的數據!", "警告", JOptionPane.WARNING_MESSAGE);
return;
}
d2 = Double.parseDouble(input);

String resultString = jLabelResult.getText();
input = numberStringFormat(input);
jLabelResult.setText(resultString + input + " = ");


if (opt.equals("+")) {
d3 = d1 + d2;
}

if (opt.equals("-")) {
d3 = d1 - d2;
}

if (opt.equals("*")) {
d3 = d1 * d2;
}

if (opt.equals("/")) {
if (d2 == 0.0) {
jLabelResult.setText("Error!");
JOptionPane.showMessageDialog(getParent(), "除數不能爲 0 ", "警告", JOptionPane.WARNING_MESSAGE);
jButtonClearActionPerformed(evt);
return;
}
d3 = d1 / d2;
}

if (opt.equals("power")) {
if (d1 == 0.0 && d2 < 0.0) {
jLabelResult.setText("Error!");
JOptionPane.showMessageDialog(getParent(), "底數爲0時,指數不能爲負", "警告", JOptionPane.WARNING_MESSAGE);
jButtonClearActionPerformed(evt);
return;
}
d3 = Math.pow(d1, d2);
}

showResult();
reset();
}

private void jButtonMinusActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
if (!input.equals("") && !input.equals("-")) {
d1 = Double.parseDouble(input);
opt = "-";
input = numberStringFormat(input);
jLabelResult.setText(input + opt);
jTextFieldInputOutput.setText("");
}
if (input.equals("")) {
jTextFieldInputOutput.setText("-");
}
}

private void jButtonMultiplyActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
if (!input.equals("") && input != null) {
d1 = Double.parseDouble(input);
opt = "*";
input = numberStringFormat(input);
jLabelResult.setText(input + opt);
jTextFieldInputOutput.setText("");
}
}

private void jButtonDivideActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
if (!input.equals("") && input != null) {
d1 = Double.parseDouble(input);
opt = "/";
input = numberStringFormat(input);
jLabelResult.setText(input + opt);
jTextFieldInputOutput.setText("");
}
}

private void jButtonSqrtActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
if (input.equals("") || input == null) {
return;
}
d1 = Double.parseDouble(input);
if (d1 < 0.0) {
JOptionPane.showMessageDialog(getParent(), "不能對負數開平方! ", "警告", JOptionPane.WARNING_MESSAGE);
jButtonClearActionPerformed(evt);
return;
}
d3 =Math.sqrt(d1);
jLabelResult.setText("sqrt(" + input + ")=");
jTextFieldInputOutput.setText("" + d3);
d1 = 0.0;
d2 = 0.0;
d3 = 0.0;
}

private void jButtonPowerActionPerformed(java.awt.event.ActionEvent evt) {
String input = jTextFieldInputOutput.getText();
if (input.equals("") || input == null) {
return;
}
d1 = Double.parseDouble(input);
opt = "power";
input = numberStringFormat(input);
jLabelResult.setText(opt + "(" + input + ",");
jTextFieldInputOutput.setText("");
}

private void reset() {
opt = "";
d1 = 0.0;
d2 = 0.0;
d3 = 0.0;
}

private String numberStringFormat(String s) {
// .5 -> 0.5
if (s.charAt(0) == '.') {
s = "0" + s;
}

if (s.charAt(s.length() - 1) == '.') {
s = s + "0";
}

// -.5 -> (-0.5) -0.5 -> (-0.5)
if (s.charAt(0) == '-') {
if (s.charAt(1) == '.') {
s = "-0" + s.substring(1);
}
s = "(" + s + ")";
}
return s;
}

private void showResult() {
jTextFieldInputOutput.setText("" + d3);
}
/**
* @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 jButtonBackspace;
private javax.swing.JButton jButtonClear;
private javax.swing.JButton jButtonDivide;
private javax.swing.JButton jButtonDot;
private javax.swing.JButton jButtonEight;
private javax.swing.JButton jButtonEquals;
private javax.swing.JButton jButtonFive;
private javax.swing.JButton jButtonFour;
private javax.swing.JButton jButtonMinus;
private javax.swing.JButton jButtonMultiply;
private javax.swing.JButton jButtonNine;
private javax.swing.JButton jButtonOne;
private javax.swing.JButton jButtonPlus;
private javax.swing.JButton jButtonPower;
private javax.swing.JButton jButtonSeven;
private javax.swing.JButton jButtonSix;
private javax.swing.JButton jButtonSqrt;
private javax.swing.JButton jButtonThree;
private javax.swing.JButton jButtonTwo;
private javax.swing.JButton jButtonZero;
private javax.swing.JLabel jLabelResult;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField jTextFieldInputOutput;
// End of variables declaration
private double d1;
private double d2;
private double d3;
private String opt;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章