萌新寫了給小計算器 想問一下為什么我按C按鈕后 會在文本框內生成一個C字符呢?

以下是全部的代碼
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @author 86176
*/
public class Main implements ActionListener {
static boolean hasPoint = false;
static String preKey;
private static double result;
JTextField tf;
JTextField tf1;
JButton bt;
String[] buttonText = {"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+",};
String strA = "";
String strB = "";
String strC = "";
char operator = '~';
public static void main(String[] args){
preKey = "";
hasPoint = false;
Main app = new Main();
app.go();
}
void go() {
//整體的框架
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//顯示計算結果的方框
tf = new JTextField();
tf1 = new JTextField(20);
tf.setPreferredSize(new Dimension(400, 60));
bt = new JButton();
JPanel p1 =new JPanel();
JButton b1 = new JButton("C");
b1.addActionListener(this);
p1.add(tf1);
p1.add(b1);
// 添加方框和按鈕到框架
frame.getContentPane().add(tf, BorderLayout.NORTH);
frame.getContentPane().add(bt, BorderLayout.CENTER);
frame.getContentPane().add(p1, BorderLayout.SOUTH);
//放按鈕的方框
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));
for (int i = 0; i < 16; i++){
JButton bt = new JButton(buttonText[i]);
p.add(bt);
bt.addActionListener(this);
}
frame.getContentPane().add(p, BorderLayout.CENTER);
//框架大小
frame.setSize(300, 400);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
double secondOperand = 0.0;
String whichButton = "";
try {
whichButton = e.getActionCommand().trim();
System.out.println("Action now " + whichButton.toString());
if ("C".equals(whichButton)){
tf.setText("");
tf1.setText("");
hasPoint = false;
}
if (".".equals(whichButton)){
if (hasPoint){
tf1.setText("操作非法,已經有小數點了");
}else {
tf.setText(tf.getText() + ".");
hasPoint = true;
}
}else {
if ("1234567890".contains(whichButton)){
for (int i = 0; i <= 9; i++){
if (Integer.parseInt(whichButton) == i){
tf.setText(tf.getText() + Integer.toString(i));
}
}
}
if (!"".equals(tf.getText())){
secondOperand = Double.valueOf(tf.getText()).doubleValue();
}
}
if ("=+-*/".contains(whichButton)){
tf.setText("");
hasPoint = false;
switch (preKey){
case "+":
result += secondOperand;
break;
case "-":
result -= secondOperand;
break;
case "*":
result *= secondOperand;
break;
case "/":
result /= secondOperand;
break;
default:
result = secondOperand;
tf.setText("");
}
if ("=".equals(whichButton)){
tf.setText(String.valueOf(result));
if (tf.getText().contains(".")){
hasPoint = true;
}
}
preKey = whichButton;
}
tf1.setText(tf1.getText() + whichButton.toString());
} catch (Exception e1) {
tf1.setText("操作非法" + e1.toString());
e1.printStackTrace();
}
}
}
uj5u.com熱心網友回復:
頂頂頂頂頂頂uj5u.com熱心網友回復:
if ("C".equals(whichButton)){tf.setText("");
tf1.setText("");
hasPoint = false;
// 添加
return;
}
這個里面,按 C表示已經結束了, 應該直接回傳,不再向下了.
uj5u.com熱心網友回復:
你要將按鈕系結到這個事件上: actionPerformed轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/283245.html
標籤:Java相關
上一篇:gitlab是不是沒有release。github有
下一篇:idea開發器運行配置問題
