以下是我正在尝试处理的代码,我已经使每个按钮在计算器中工作,但不知道=按钮代码。请帮忙。
案例“+”在文本字段中显示“结果”,而其他案例则给我例外。
package calculator;
public class Calculator_JFrame extends javax.swing.JFrame {
double firstnum;
double secondnum;
double result;
String operation;
public Calculator_JFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
txtDisplay = 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);
txtDisplay.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
txtDisplay.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jButton1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton3.setText("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton4.setText("+");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton5.setText("4");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton6.setText("5");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton7.setText("6");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton8.setText("-");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton9.setText("7");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton10.setText("8");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
jButton11.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton11.setText("9");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
jButton12.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton12.setText("*");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});
jButton13.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton13.setText("0");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});
jButton14.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jButton14.setText("00");
jButton14.setMargin(new java.awt.Insets(0, 0, 0, 0));
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});
jButton15.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton15.setText(".");
jButton15.setToolTipText("");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton15ActionPerformed(evt);
}
});
jButton16.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton16.setText("/");
jButton16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton16ActionPerformed(evt);
}
});
jButton17.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton17.setText("C");
jButton17.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton17ActionPerformed(evt);
}
});
jButton18.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton18.setText("=");
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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtDisplay)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton15)
.addGap(3, 3, 3)
.addComponent(jButton16, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton18, 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(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton18, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton1.getText();
txtDisplay.setText(input);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
firstnum=Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("");
operation="+";
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton2.getText();
txtDisplay.setText(input);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton5.getText();
txtDisplay.setText(input);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton3.getText();
txtDisplay.setText(input);
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton6.getText();
txtDisplay.setText(input);
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton9.getText();
txtDisplay.setText(input);
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton10.getText();
txtDisplay.setText(input);
}
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton11.getText();
txtDisplay.setText(input);
}
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton13.getText();
txtDisplay.setText(input);
}
private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton14.getText();
txtDisplay.setText(input);
}
private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText("");
}
private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
String ans;
switch(operation){
case "+":
result=firstnum+secondnum;
ans=String.valueOf(result);
txtDisplay.setText(ans);
break;
case "-":
result=firstnum-secondnum;
ans=String.format("%.0f", result);
txtDisplay.setText(ans);
break;
case "*":
result=firstnum*secondnum;
ans=String.format("%.0f", result);
txtDisplay.setText(ans);
break;
case "/":
result=firstnum/secondnum;
ans=String.format("%.0f", result);
txtDisplay.setText(ans);
break;
}
}
private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton15.getText();
txtDisplay.setText(input);
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton7.getText();
txtDisplay.setText(input);
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
firstnum=Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("");
operation="-";
}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
firstnum=Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("");
operation="*";
}
private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
firstnum=Double.parseDouble(txtDisplay.getText());
txtDisplay.setText("");
operation="/";
}
/**
* @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_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculator_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculator_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculator_JFrame.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_JFrame().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 txtDisplay;
// End of variables declaration
}
答案 0 :(得分:0)
在键入操作符号时,您已经在输入中输入了两个操作数; 并且因为你正在进行基本操作(二进制操作(只接受两个参数)),那么你的工作没问题,你不需要“=”操作,因为操作的形式如下:
op(arg1,arg2)
但是如果你想要评估可变参数操作(那些接受任意数量的变量),那么你需要在实践中使用“=”符号。
最后,那些异常呢?你需要为我们提供完整的堆栈跟踪,也许它是关于格式化数字的字符串,检查“%.of”或许它需要是“%。0f”。
答案 1 :(得分:0)
您需要使用String.valueOf
方法,否则会出现运行时错误。
result= firstnum-secondnum;
ans=String.valueOf(result);
System.out.println(ans);
break;
这是如何运作的:See Live Demo
答案 2 :(得分:0)
= 操作类型应该只是Identity操作,即它返回与参数相同的值:
private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
String ans;
switch(operation){
//...
case "=":
//Do nothing when the = sign is pressed as the result would remain the same
txtDisplay.setText((result != 0) ?
Double.toString(result) :
Integer.toString(0));
break;
}
}
您无法在switch
文字上绝对使用String
语句。它只能用于字节,字符或枚举类型。
在设计基于操作的计算器时,我强烈建议使用标有Enum
的{{1}}类型来定义计算方法的抽象方法,并且具有不断的特定实现:
Operation
请注意,上述Enum类型可让您获得灵活性,您可以根据需要添加尽可能多的操作,而不会损害其他软件组件,您可以将其用作public enum Operation
{
ADD
{
@Override
double apply(double argOne, double argTwo)
{
return argOne + argTwo;
}
},
SUBSTRUCT
{
@Override
double apply(double argOne, double argTwo)
{
return argOne - argTwo;
}
},
DEVIDE
{
@Override
double apply(double argOne, double argTwo)
{
return argOne / argTwo;
}
},
MULTIPLY
{
@Override
double apply(double argOne, double argTwo)
{
return argOne * argTwo;
}
};
abstract double apply(double argOne, double argTwo);
}
标签:
switch
注意: Here您可以找到计算器的全功能工作示例。
答案 3 :(得分:0)
我看到你创造了很多按钮。实际上你可以简单地创建一个按钮数组,并根据数组索引设置按钮的文本。
当用户按下按钮时,只需使用 getText()方法获取按钮&#39;值。
同样适用于数学运算符(+, - ,*,/)。
使用 3行代码创建 10个按钮。
JButton[] button = new JButton[10];
for(int x=0; x<10; x++)
button = new JButton(Integer.toString(x)); //create buttons 0-9
在actionListener:
int valuePressed = Integer.parseInt(button.getText());
通过这些,您的整个计划将优雅地运作。
答案 4 :(得分:0)
感谢大家的帮助。我终于成功了。 Yahoooo。谢谢你的建议。
package calculator;
public class Calculator_JFrame extends javax.swing.JFrame {
float firstnum;
float secondnum;
float result;
String operation;
public Calculator_JFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
txtDisplay = 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);
setResizable(false);
txtDisplay.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
txtDisplay.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jButton1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton1.setText("1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton2.setText("2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton3.setText("3");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton4.setText("+");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton5.setText("4");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
jButton6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton6.setText("5");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});
jButton7.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton7.setText("6");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});
jButton8.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton8.setText("-");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton9.setText("7");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton10.setText("8");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
jButton11.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton11.setText("9");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton11ActionPerformed(evt);
}
});
jButton12.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton12.setText("*");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton12ActionPerformed(evt);
}
});
jButton13.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton13.setText("0");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton13ActionPerformed(evt);
}
});
jButton14.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jButton14.setText("00");
jButton14.setMargin(new java.awt.Insets(0, 0, 0, 0));
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton14ActionPerformed(evt);
}
});
jButton15.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton15.setText(".");
jButton15.setToolTipText("");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton15ActionPerformed(evt);
}
});
jButton16.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton16.setText("/");
jButton16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton16ActionPerformed(evt);
}
});
jButton17.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton17.setText("C");
jButton17.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton17ActionPerformed(evt);
}
});
jButton18.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jButton18.setText("=");
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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtDisplay)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton15)
.addGap(3, 3, 3)
.addComponent(jButton16, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton18, 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(txtDisplay, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton10, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton11, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton12, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton13, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton14, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton15, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton16, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton17, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton18, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton1.getText();
txtDisplay.setText(input);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
try{
firstnum=Float.parseFloat(txtDisplay.getText());
txtDisplay.setText("");
operation="+";
}
catch (Exception e){
txtDisplay.setText("Invalid Number");
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton2.getText();
txtDisplay.setText(input);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton5.getText();
txtDisplay.setText(input);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton3.getText();
txtDisplay.setText(input);
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton6.getText();
txtDisplay.setText(input);
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton9.getText();
txtDisplay.setText(input);
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton10.getText();
txtDisplay.setText(input);
}
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton11.getText();
txtDisplay.setText(input);
}
private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton13.getText();
txtDisplay.setText(input);
}
private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton14.getText();
txtDisplay.setText(input);
}
private void jButton17ActionPerformed(java.awt.event.ActionEvent evt) {
txtDisplay.setText("");
}
private void jButton18ActionPerformed(java.awt.event.ActionEvent evt) {
secondnum=Float.parseFloat(txtDisplay.getText());
String ans;
try{
if(operation=="+"){
result=firstnum+secondnum;
ans=String.valueOf(result);
txtDisplay.setText(ans);
}else if(operation=="-"){
result=firstnum-secondnum;
ans=String.valueOf(result);
txtDisplay.setText(ans);
}else if (operation.equals("*")){
result=firstnum*secondnum;
ans=String.valueOf(result);
txtDisplay.setText(ans);
}else if(operation.equals("/")){
result=firstnum/secondnum;
ans=String.valueOf(result);
txtDisplay.setText(ans);
}
}
catch(Exception e){
txtDisplay.setText("Enter Valid Number");
}
}
private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton15.getText();
txtDisplay.setText(input);
}
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
String input;
input=txtDisplay.getText()+jButton7.getText();
txtDisplay.setText(input);
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
try{
firstnum=Float.parseFloat(txtDisplay.getText());
txtDisplay.setText("");
operation="-";
}
catch (Exception e){
txtDisplay.setText("Invalid Number");
}
}
private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {
try{
firstnum=Float.parseFloat(txtDisplay.getText());
txtDisplay.setText("");
operation="*";
}
catch (Exception e){
txtDisplay.setText("Invalid Number");
}
}
private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {
try{
firstnum=Float.parseFloat(txtDisplay.getText());
txtDisplay.setText("");
operation="/";
}
catch (Exception e){
txtDisplay.setText("Invalid Number");
}
}
/**
* @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_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculator_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calculator_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculator_JFrame.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_JFrame().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 txtDisplay;
// End of variables declaration
}