学习了Swing基础中的基础知识,做了个简易中的简易版计算器,囧……
在退格按钮的事件处理代码块中还存在异常,不过不影响计算,呃……
上代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CounterTest {
private JFrame frame;
private JPanel panel;
private JTextField textfile;
private StringBuffer sb = new StringBuffer("");
private StringBuffer sb1 = new StringBuffer(""); //用来暂时存储第一个数字的数据
private StringBuffer sb2 = new StringBuffer(""); 用来暂时存储第二个数字的数据
private String countMethod = ""; //用来接收计算方法的字符串
private Double num1; //用来接收第一个数字
private Double num2; //用来接收第二个数字
private double result = 0; //用来接收计算结果
public CounterTest(){
frame = new JFrame("LHY版简易计算器");
panel = new JPanel();
textfile = new JTextField();
frame.setBounds(20, 20, 400, 630);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createPanel();
createNumBar();
}
/**
* 创建计算器面板
*/
public void createPanel(){
frame.add(panel);
panel.setLayout(null);
panel.setBounds(20,20,400, 630);
JButton btnNull = new JButton("欢迎使用");
btnNull.setBounds(0, 100, 100, 100);
panel.add(btnNull);
//退格按钮
JButton btnBack = new JButton("退格");
btnBack.setBounds(100, 100, 100, 100);
panel.add(btnBack);
btnBack.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Character judge = sb.toString().charAt(sb.length()-1);
try{
sb = sb.deleteCharAt(sb.length()-1);
String str = sb.toString();
textfile.setText(str);
if((judge.equals("+") || judge.equals("-") || judge.equals("*") || judge.equals("/")) == true){
countMethod = "";
return;
}else if(countMethod.length() != 0){
sb2 = sb2.deleteCharAt(sb2.length()-1);
String str2 = sb2.toString();
num2 = Double.parseDouble(str2);
}else if(countMethod.length() == 0){
sb1 = sb1.deleteCharAt(sb1.length()-1);
String str1 = sb1.toString();
num1 = Double.parseDouble(str1);
}
///
// if(countMethod.length() == 0){
// sb1 = sb1.deleteCharAt(sb1.length()-1);
// String str1 = sb1.toString();
// num1 = Double.parseDouble(str1);
// }else if(countMethod.length() != 0){
// if(judge.equals("+") || judge.equals("-") || judge.equals("*") || judge.equals("/")){
// countMethod = "";
// }else{
// sb2 = sb2.deleteCharAt(sb2.length()-1);
// String str2 = sb2.toString();
// num2 = Double.parseDouble(str2);
// }
// }
}catch(Exception ex){
ex.printStackTrace();
}
}
});
//CE按钮
JButton btnCE = new JButton("CE(清零)");
btnCE.setBounds(200, 100, 100, 100);
panel.add(btnCE);
btnCE.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createNumBar();
sb = new StringBuffer("");
sb1 = new StringBuffer("");
sb2 = new StringBuffer("");
countMethod = "";
}
});
//C按钮
JButton btnC = new JButton("C");
btnC.setBounds(300, 100, 100, 100);
panel.add(btnC);
//数字7按钮
JButton btn7 = new JButton("7");
btn7.setBounds(0, 200, 100, 100);
panel.add(btn7);
btn7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sb.append("7");
String str = sb.toString();
textfile.setText(str);
if(countMethod.length() == 0){
sb1.append("7");
num1 = Double.parseDouble(sb1.toString());
}else if(countMethod.length() != 0){
sb2.append("7");
num2 = Double.parseDouble(sb2.toString());
}
}
});
//数字8按钮
JButton btn8 = new JButton("8");
btn8.setBounds(100, 200, 100, 100);
panel.add(btn8);
btn8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sb.append("8");
String str = sb.toString();
textfile.setText(str);
if(countMethod.length() == 0){
sb1.append("8");
num1 = Double.parseDouble(sb1.toString());
}else if(countMethod.length() != 0){
sb2.append("8");
num2 = Double.parseDouble(sb2.toString());
}
}
});
//数字9按钮
JButton btn9 = new JButton("9");
btn9.setBounds(200, 200, 100, 100);
panel.add(btn9);
btn9.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sb.append("9");
String str = sb.toString();
textfile.setText(str);
if(countMethod.length() == 0){
sb1.append("9");
num1 = Double.parseDouble(sb1.toString());
}else if(countMethod.length() != 0){
sb2.append("9");
num2 = Double.parseDouble(sb2.toString());
}
}
});
//除号按钮
JButton btnDivision = new JButton("/");
btnDivision.setBounds(300, 200, 100, 100);
panel.add(btnDivision);
btnDivision.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
countMethod = "/";
sb.append("/");
String str = sb.toString();
textfile.setText(str);
}
});
//数字4按钮
JButton btn4 = new JButton("4");
btn4.setBounds(0, 300, 100, 100);
panel.add(btn4);
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sb.append("4");
String str = sb.toString();
textfile.setText(str);
if(countMethod.length() == 0){
sb1.append("4");
num1 = Double.parseDouble(sb1.toString());
}else if(countMethod.length() != 0){
sb2.append("4");
num2 = Double.parseDouble(sb2.toString());
}
}
});
//数字5按钮
JButton btn5 = new JButton("5");
btn5.setBounds(100, 300, 100, 100);
panel.add(btn5);
btn5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sb.append("5");
String str = sb.toString();
textfile.setText(str);
if(countMethod.length() == 0){
sb1.append("5");
num1 = Double.parseDouble(sb1.toString());
}else if(countMethod.length() != 0){
sb2.append("5");
num2 = Double.parseDouble(sb2.toString());
}
}
});
//数字6按钮
JButton btn6 = new JButton("6");
btn6.setBounds(200, 300, 100, 100);
panel.add(btn6);
btn6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sb.append("6");
String str = sb.toString();
textfile.setText(str);
if(countMethod.length() == 0){
sb1.append("6");
num1 = Double.parseDouble(sb1.toString());
}else if(countMethod.length() != 0){
sb2.append("6");
num2 = Double.parseDouble(sb2.toString());
}
}
});
//乘号按钮
JButton btnMult = new JButton("*");
btnMult.setBounds(300, 300, 100, 100);
panel.add(btnMult);
btnMult.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
countMethod = "*";
sb.append("*");
String str = sb.toString();
textfile.setText(str);
}
});
//数字1按钮
JButton btn1 = new JButton("1");
btn1.setBounds(0, 400, 100, 100);
panel.add(btn1);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sb.append("1");
String str = sb.toString();
textfile.setText(str);
if(countMethod.length() == 0){
sb1.append("1");
num1 = Double.parseDouble(sb1.toString());
}else if(countMethod.length() != 0){
sb2.append("1");
num2 = Double.parseDouble(sb2.toString());
}
}
});
//数字2按钮
JButton btn2 = new JButton("2");
btn2.setBounds(100, 400, 100, 100);
panel.add(btn2);
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sb.append("2");
String str = sb.toString();
textfile.setText(str);
if(countMethod.length() == 0){
sb1.append("2");
num1 = Double.parseDouble(sb1.toString());
}else if(countMethod.length() != 0){
sb2.append("2");
num2 = Double.parseDouble(sb2.toString());
}
}
});
//数字3按钮
JButton btn3 = new JButton("3");
btn3.setBounds(200, 400, 100, 100);
panel.add(btn3);
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sb.append("3");
String str = sb.toString();
textfile.setText(str);
if(countMethod.length() == 0){
sb1.append("3");
num1 = Double.parseDouble(sb1.toString());
}else if(countMethod.length() != 0){
sb2.append("3");
num2 = Double.parseDouble(sb2.toString());
}
}
});
//减号按钮
JButton btnSub = new JButton("-");
btnSub.setBounds(300, 400, 100, 100);
panel.add(btnSub);
btnSub.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
countMethod = "-";
sb.append("-");
String str = sb.toString();
textfile.setText(str);
}
});
//数字0按钮
JButton btn0 = new JButton("0");
btn0.setBounds(0, 500, 100, 100);
panel.add(btn0);
btn0.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sb.append("0");
String str = sb.toString();
textfile.setText(str);
if(countMethod.length() == 0){
sb1.append("0");
num1 = Double.parseDouble(sb1.toString());
}else if(countMethod.length() != 0){
sb2.append("0");
num2 = Double.parseDouble(sb2.toString());
}
}
});
//小数点按钮
JButton btnDot = new JButton(".");
btnDot.setBounds(100, 500, 100, 100);
panel.add(btnDot);
btnDot.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sb.append(".");
String str = sb.toString();
textfile.setText(str);
if(countMethod.length() == 0){
sb1.append(".");
num1 = Double.parseDouble(sb1.toString());
}else if(countMethod.length() != 0){
sb2.append(".");
num2 = Double.parseDouble(sb2.toString());
}
}
});
//等号按钮
JButton btnEqual = new JButton("=");
btnEqual.setBounds(200, 500, 100, 100);
panel.add(btnEqual);
btnEqual.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
countNum();
}
});
//加号按钮
JButton btnAdd = new JButton("+");
btnAdd.setBounds(300, 500, 100, 100);
panel.add(btnAdd);
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
countMethod = "+";
sb.append("+");
String str = sb.toString();
textfile.setText(str);
}
});
}
/**
* 创建计算器显示条
*/
public void createNumBar(){
textfile.setText("");
textfile.setBounds(0, 0, 400, 100);
panel.add(textfile);
}
/**
* 进行计算的方法
*/
public void countNum(){
if(countMethod.equals("+")){
addMethod();
}else if(countMethod.equals("-")){
subMethod();
}else if(countMethod.equals("*")){
multMethod();
}else if(countMethod.equals("/")){
divisionMethod();
}else{
///
}
}
/**
* 加法
*/
public void addMethod(){
result = num1 + num2;
sb.append("=").append(result);
String str = sb.toString();
textfile.setText(str);
}
/**
* 减法
*/
public void subMethod(){
result = num1 - num2;
sb.append("=").append(result);
String str = sb.toString();
textfile.setText(str);
}
/**
* 乘法
*/
public void multMethod(){
result = num1 * num2;
sb.append("=").append(result);
String str = sb.toString();
textfile.setText(str);
}
/**
* 除法
*/
public void divisionMethod(){
result = num1 / num2;
sb.append("=").append(result);
String str = sb.toString();
textfile.setText(str);
}
public static void main(String[] args) {
new CounterTest();
}
}