该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
使用GUI
Actionlistener 监听
多重IF嵌套~~我这有个简单板的代码~~~你可以参考下
package 计算器简单版;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class JiSuanqi extends JFrame {
private JButton jb[];
private JTextField jtf;
private int Num1;
private int Num2;
private JPanel jp;
private String str[] = { "7", "8", "9", "+", "4", "5", "6", "-", "1", "2",
"3", "*", "CE", "0", "=", "/" };
private String tempstr;
private String opt;
private int results;
private double resultsc;
private boolean num = false;
private boolean show = false;
public JiSuanqi() {
this.setTitle("计算器-简单版");
this.jtf = new JTextField("0");
this.jtf.setHorizontalAlignment(JTextField.RIGHT);
this.jtf.setBackground(Color.BLACK);
this.jtf.setEnabled(false);
this.add(jtf, BorderLayout.NORTH);
this.jp = new JPanel(new GridLayout(4, 4, 1, 1));
this.jb = new JButton[str.length];
for (int i = 0; i
this.jb[i] = new JButton(str[i]);
this.jp.add(jb[i]);
this.jb[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fangfa(e.getActionCommand());
}
});
}
this.add(this.jp);
this.setBounds(300, 200, 260, 200);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
this.setVisible(true);
}
public void fangfa(String curr){
if (show == true)
{
reset0();
}
this.tempstr = this.jtf.getText();
if ('0' <= curr.charAt(0) && curr.charAt(0) <= '9'
&& curr.length() == 1)
{
if (show == true)
{
reset0();
}
this.tempstr += curr;
if (this.tempstr.charAt(0) == '0') {
this.tempstr = this.tempstr.substring(1, this.tempstr.length());
}
this.jtf.setText(this.tempstr);
}else if("CE".equals(this.opt)){
this.jtf.setText("0");
this.Num1 = 0;
this.Num2 = 0;
this.num = false;
this.results = 0;
this.tempstr= "";
this.opt = "";
this.show = false;
return;
}
else
{
if (curr.equals("="))
{
if (this.num == true)
{
this.Num2 = Integer.parseInt(this.jtf.getText());
this.num = false;
result(Num1, Num2);
this.show = true;
}
}
else
{
if (num == false)
{
this.opt = curr;
this.Num1 = Integer.parseInt(this.jtf.getText());
this.num = true;
this.jtf.setText("0");
}
}
}
}
public void result(int a, int b) {
if ("+".equals(this.opt)) {
this.results = a + b;
this.tempstr = Integer.toString(this.results);
this.jtf.setText(this.tempstr);
}else if("-".equals(this.opt)){
this.results = a - b;
this.tempstr = Integer.toString(this.results);
this.jtf.setText(this.tempstr);
}else if("*".equals(this.opt)){
this.results = a * b;
this.tempstr = Integer.toString(this.results);
this.jtf.setText(this.tempstr);
}else if("/".equals(this.opt)){
double a1 = a, b1 = b;
this.resultsc = a1 / b1;
this.tempstr = Double.toString(this.resultsc);
this.jtf.setText(this.tempstr);
}
//else if("CE".equals(this.opt)){
//this.jtf.setText("0");
//this.Num1 = 0;
//this.Num2 = 0;
//this.num = false;
//this.tempstr = null;
//this.opt = null;
//this.results = 0;
//this.resultsc = 0.0;
//}
}
public void reset0() {
this.Num1 = 0;
this.Num2 = 0;
this.num = false;
this.results = 0;
this.tempstr= "";
this.jtf.setText(new String());
this.opt = "";
this.show = false;
}
public static void main(String[] args) {
JiSuanqi ji = new JiSuanqi();
}
}