- 论坛徽章:
- 0
|
我学java一周尝试做了个计算器 运行没报错 但单击按钮 文本框中不显示相应的数字,郁闷 求大师帮忙找找错
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class edec extends JFrame implements ActionListener {
private JFrame frame;
private JButton button1, button2, button3, button4, button5, button6,
button7, button8, button9, button0, buttonadd, buttonreduce,
buttonride, buttondivide, buttonpoint, buttonequal;
private JTextField textField;
String string;
public void init() {
getContentPane().setLayout(null);
setBounds(100, 100, 291, 280);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button1 = new JButton();
button1.setBackground(Color.WHITE);
button1.setText("1" ;
button1.setBounds(10, 88, 59, 29);
getContentPane().add(button1);
button1.addActionListener(this);
final JButton button2 = new JButton();
button2.setBackground(Color.WHITE);
button2.setText("2" ;
button2.setBounds(75, 88, 59, 29);
getContentPane().add(button2);
button2.addActionListener(this);
final JButton button3 = new JButton();
button3.setBackground(Color.WHITE);
button3.setText("3" ;
button3.addActionListener(this);
button3.setBounds(140, 88, 59, 29);
getContentPane().add(button3);
button3.addActionListener(this);
final JButton button4 = new JButton();
button4.setBackground(Color.WHITE);
button4.setText("4" ;
button4.setBounds(10, 123, 59, 29);
getContentPane().add(button4);
button4.addActionListener(this);
final JButton button5 = new JButton();
button5.setBackground(Color.WHITE);
button5.setText("5" ;
button5.setBounds(75, 123, 59, 29);
getContentPane().add(button5);
button5.addActionListener(this);
final JButton button6 = new JButton();
button6.setBackground(Color.WHITE);
button6.setText("6" ;
button6.setBounds(140, 123, 59, 29);
getContentPane().add(button6);
button6.addActionListener(this);
final JButton button7 = new JButton();
button7.setBackground(Color.WHITE);
button7.setText("7" ;
button7.setBounds(10, 158, 59, 29);
getContentPane().add(button7);
button7.addActionListener(this);
final JButton button8 = new JButton();
button8.setBackground(Color.WHITE);
button8.setText("8" ;
button8.setBounds(75, 158, 59, 29);
getContentPane().add(button ;
button8.addActionListener(this);
final JButton button9 = new JButton();
button9.setBackground(Color.WHITE);
button9.setText("9" ;
button9.setBounds(140, 158, 59, 29);
getContentPane().add(button9);
button9.addActionListener(this);
final JButton button0 = new JButton();
button0.setBackground(Color.WHITE);
button0.setText("0" ;
button0.setBounds(10, 193, 59, 29);
getContentPane().add(button0);
button0.addActionListener(this);
final JButton buttonpoint = new JButton();
buttonpoint.setBackground(Color.WHITE);
buttonpoint.setText(".");
buttonpoint.setBounds(75, 193, 59, 29);
getContentPane().add(buttonpoint);
button0.addActionListener(this);
final JButton buttonequal = new JButton();
buttonequal.setBackground(Color.WHITE);
buttonequal.setText("=");
buttonequal.setBounds(140, 193, 59, 29);
getContentPane().add(buttonequal);
buttonequal.addActionListener(this);
final JButton buttondivide = new JButton();
buttondivide.setBackground(Color.WHITE);
buttondivide.setText("/");
buttondivide.setBounds(205, 88, 59, 29);
getContentPane().add(buttondivide);
buttondivide.addActionListener(this);
final JButton buttonride = new JButton();
buttonride.setBackground(Color.WHITE);
buttonride.setText("*");
buttonride.setBounds(205, 123, 59, 29);
getContentPane().add(buttonride);
buttonride.addActionListener(this);
final JButton buttonreduce = new JButton();
buttonreduce.setBackground(Color.WHITE);
buttonreduce.setText("-");
buttonreduce.setBounds(205, 158, 59, 29);
getContentPane().add(buttonreduce);
buttonreduce.addActionListener(this);
final JButton buttonadd = new JButton();
buttonadd.setBackground(Color.WHITE);
buttonadd.setText("+");
buttonadd.setBounds(205, 193, 59, 29);
getContentPane().add(buttonadd);
buttonadd.addActionListener(this);
final JTextField textField = new JTextField();
textField.setBounds(10, 53, 254, 29);
getContentPane().add(textField);
}
public static void main(String args[]) {
edec frame = new edec();
frame.setVisible(true);
frame.init();
}
String foreNum = null;
String currNum = null;
String currOpr = null;
public void actionPerformed(ActionEvent e) {
Object object = e.getSource();
if (object == button0 || object == button1 || object == button3
|| object == button2 || object == button4 || object == button5
|| object == button6 || object == button7 || object == button8
|| object == button9) {
textField.setText(e.getActionCommand());
} else if (object == buttonpoint) {
textField.setText(textField.getText() + '.');
} else if (object == buttonadd || object == buttonreduce
|| object == buttonride || object == buttonride) {
currNum = textField.getText();
if (currOpr == "+")
textField.setText(String.valueOf(Double.parseDouble(foreNum)
+ Double.parseDouble(currNum)));
else if (currOpr == "-")
textField.setText(String.valueOf(Double.parseDouble(foreNum)
- Double.parseDouble(currNum)));
else if (currOpr == "*")
textField.setText(String.valueOf(Double.parseDouble(foreNum)
* Double.parseDouble(currNum)));
else if (currOpr == "/") {
if (Double.parseDouble(textField.getText()) == 0)
textField.setText("除数不能为零");
else
textField
.setText(String.valueOf(Double.parseDouble(foreNum)
/ Double.parseDouble(currNum)));
}
} else if (object == buttonequal) {
currNum = textField.getText();
if (currOpr == "+")
textField.setText(String.valueOf(Double.parseDouble(foreNum)
+ Double.parseDouble(currNum)));
else if (currOpr == "-")
textField.setText(String.valueOf(Double.parseDouble(foreNum)
- Double.parseDouble(currNum)));
else if (currOpr == "*")
textField.setText(String.valueOf(Double.parseDouble(foreNum)
* Double.parseDouble(currNum)));
else if (currOpr == "/") {
if (Double.parseDouble(textField.getText()) == 0)
textField.setText("除数不能为零");
else
textField
.setText(String.valueOf(Double.parseDouble(foreNum)
/ Double.parseDouble(currNum)));
}
}
}
}
|
|