主题:计算器代码,进来给点建议
/************未完成*************/
package javalessons;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private final int WIDTH = 480;
private final int HEIGHT = 300;
private Panel panel1, panel2;
private JTextField textField;
private JButton[] button;
private String[] buttonText = { "7", "8", "9", "+", "C", "4", "5", "6",
"-", "退格", "1", "2", "3", "*", "1/X", "0", "+/-", ".", "/", "=" };
/** **************** 当按下的按钮是操作符时,operator 为 true ******************* */
public static boolean operator;
private int count = 0;// count = 0 为第一次输入
private String text = "";
private double total = 0;// 存放结果
private int symbol = 0;// +、-、*、/分别对应 1、2、3、4
public static void main(String args[]) {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public Calculator() {
super();
setTitle("计算器");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
setBounds((screenSize.width - WIDTH) / 2,
(screenSize.height - HEIGHT) / 2, WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1 = new Panel();
panel2 = new Panel();
setLayout(new BorderLayout());
textField = new JTextField("0", 30);
textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
textField.setFont(new Font("", Font.BOLD, 22));
textField.setForeground(Color.BLUE);
textField.setEditable(false);
textField.setBackground(Color.WHITE);
panel1.setLayout(new GridLayout(1, 1));
panel1.add(textField);
button = new JButton[20];
for (int i = 0; i < button.length; i++) {
button[i] = new JButton();
button[i].setText(buttonText[i]);
button[i].setFont(new Font("", Font.BOLD, 22));
button[i].setForeground(Color.BLUE);
panel2.add(button[i]);
button[i].addActionListener(this);
}
button[3].setForeground(Color.RED);
button[4].setForeground(Color.RED);
button[8].setForeground(Color.RED);
button[9].setForeground(Color.RED);
button[13].setForeground(Color.RED);
button[18].setForeground(Color.RED);
button[19].setForeground(Color.RED);
panel2.setLayout(new GridLayout(4, 5));
add(panel1, "North");
add(panel2, "Center");
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == this.button[0]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("7");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "7");
}
}
if (e.getSource() == this.button[1]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("8");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "8");
}
}
if (e.getSource() == this.button[2]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("9");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "9");
}
}
if (e.getSource() == this.button[5]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("4");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "4");
}
}
if (e.getSource() == this.button[6]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("5");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "5");
}
}
if (e.getSource() == this.button[7]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("6");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "6");
}
}
if (e.getSource() == this.button[10]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("1");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "1");
}
}
if (e.getSource() == this.button[11]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("2");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "2");
}
}
if (e.getSource() == this.button[12]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("3");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "3");
}
}
/** *********** 对 textField 中本为“0”时进行特殊处理 ************* */
if (e.getSource() == this.button[15]) {
if (operator || count == 0) {
textField.setText("0");
operator = false;
count = 1;
} else {
text = textField.getText();
if (Float.parseFloat(text) != 0) {
textField.setText(text + "0");
} else {
if (text.trim().indexOf(".") == -1) {
textField.setText(text);
} else {
textField.setText(text + "0");
}
}
}
}
if (e.getSource() == this.button[4]) {
textField.setText("0");
count = 0;
total=0;
operator = true;
}
/** *********** 处理小数点,在一个数不能有一个以上的小数点 ************* */
if (e.getSource() == this.button[17]) {
if (operator || count == 0) {
textField.setText("0.");
operator = false;
count = 1;
} else {
text = textField.getText();
if (text.trim().indexOf(".") == -1) {
textField.setText(text + ".");
} else {
textField.setText(text);
}
}
}
/** *************处理1/X************* */
if (e.getSource() == this.button[14]) {
operator = true;
if (count != 0) {
total = 1 / Double.parseDouble(textField.getText());
textField.setText(total + "");
}
}
/** *********处理“=”********** */
if (e.getSource() == this.button[19]) {
operator = true;
if (count != 0) {
// if (count != 1) {
total += Double.parseDouble(textField.getText());
textField.setText(total + "");
// }
}
}
/** ********获取“-”前的数值********* */
if (e.getSource() == this.button[3]) {
operator = true;
count = 1;
total = Double.parseDouble(textField.getText());
textField.setText(textField.getText());
symbol = 1;
}
/** ********获取“-”前的数值********* */
if (e.getSource() == this.button[8]) {
operator = true;
count = 1;
total = Double.parseDouble(textField.getText());
textField.setText(textField.getText());
symbol = 2;
}
/** ********获取“*”前的数值********* */
if (e.getSource() == this.button[13]) {
operator = true;
count = 1;
total = Double.parseDouble(textField.getText());
textField.setText(textField.getText());
symbol = 3;
}
/** ********获取“/”前的数值********* */
if (e.getSource() == this.button[18]) {
operator = true;
count = 1;
total = Double.parseDouble(textField.getText());
textField.setText(textField.getText());
symbol = 4;
}
switch (symbol) {
case 1:// 计算加法
{
double ad = total + Double.parseDouble(textField.getText());
textField.setText(ad + "");
count = 0;
text = "";
break;
}
case 2:// 计算减法
{
double ad = total - Double.parseDouble(textField.getText());
textField.setText(String.valueOf(ad));
count = 0;
text = "";
break;
}
case 3:// 计算乘法
{
double ad = total * Double.parseDouble(textField.getText());
textField.setText(ad + "");
count = 0;
text = "";
break;
}
case 4:// 计算除法
{
double ad = total / Double.parseDouble(textField.getText());
textField.setText(ad + "");
count = 0;
text = "";
break;
}
}
System.out.println(operator);
}
}
觉得这样写太多重复的代码了
哪位坛友给个好的建议?
大四就好找工作了还要做这么多实验,还全是要做界面的,郁闷
package javalessons;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private final int WIDTH = 480;
private final int HEIGHT = 300;
private Panel panel1, panel2;
private JTextField textField;
private JButton[] button;
private String[] buttonText = { "7", "8", "9", "+", "C", "4", "5", "6",
"-", "退格", "1", "2", "3", "*", "1/X", "0", "+/-", ".", "/", "=" };
/** **************** 当按下的按钮是操作符时,operator 为 true ******************* */
public static boolean operator;
private int count = 0;// count = 0 为第一次输入
private String text = "";
private double total = 0;// 存放结果
private int symbol = 0;// +、-、*、/分别对应 1、2、3、4
public static void main(String args[]) {
try {
Calculator frame = new Calculator();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public Calculator() {
super();
setTitle("计算器");
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
setBounds((screenSize.width - WIDTH) / 2,
(screenSize.height - HEIGHT) / 2, WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1 = new Panel();
panel2 = new Panel();
setLayout(new BorderLayout());
textField = new JTextField("0", 30);
textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
textField.setFont(new Font("", Font.BOLD, 22));
textField.setForeground(Color.BLUE);
textField.setEditable(false);
textField.setBackground(Color.WHITE);
panel1.setLayout(new GridLayout(1, 1));
panel1.add(textField);
button = new JButton[20];
for (int i = 0; i < button.length; i++) {
button[i] = new JButton();
button[i].setText(buttonText[i]);
button[i].setFont(new Font("", Font.BOLD, 22));
button[i].setForeground(Color.BLUE);
panel2.add(button[i]);
button[i].addActionListener(this);
}
button[3].setForeground(Color.RED);
button[4].setForeground(Color.RED);
button[8].setForeground(Color.RED);
button[9].setForeground(Color.RED);
button[13].setForeground(Color.RED);
button[18].setForeground(Color.RED);
button[19].setForeground(Color.RED);
panel2.setLayout(new GridLayout(4, 5));
add(panel1, "North");
add(panel2, "Center");
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == this.button[0]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("7");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "7");
}
}
if (e.getSource() == this.button[1]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("8");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "8");
}
}
if (e.getSource() == this.button[2]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("9");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "9");
}
}
if (e.getSource() == this.button[5]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("4");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "4");
}
}
if (e.getSource() == this.button[6]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("5");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "5");
}
}
if (e.getSource() == this.button[7]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("6");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "6");
}
}
if (e.getSource() == this.button[10]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("1");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "1");
}
}
if (e.getSource() == this.button[11]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("2");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "2");
}
}
if (e.getSource() == this.button[12]) {
if (operator || count == 0 || textField.getText().equals("0")) {
textField.setText("3");
operator = false;
count = 1;
} else {
text = textField.getText();
textField.setText(text + "3");
}
}
/** *********** 对 textField 中本为“0”时进行特殊处理 ************* */
if (e.getSource() == this.button[15]) {
if (operator || count == 0) {
textField.setText("0");
operator = false;
count = 1;
} else {
text = textField.getText();
if (Float.parseFloat(text) != 0) {
textField.setText(text + "0");
} else {
if (text.trim().indexOf(".") == -1) {
textField.setText(text);
} else {
textField.setText(text + "0");
}
}
}
}
if (e.getSource() == this.button[4]) {
textField.setText("0");
count = 0;
total=0;
operator = true;
}
/** *********** 处理小数点,在一个数不能有一个以上的小数点 ************* */
if (e.getSource() == this.button[17]) {
if (operator || count == 0) {
textField.setText("0.");
operator = false;
count = 1;
} else {
text = textField.getText();
if (text.trim().indexOf(".") == -1) {
textField.setText(text + ".");
} else {
textField.setText(text);
}
}
}
/** *************处理1/X************* */
if (e.getSource() == this.button[14]) {
operator = true;
if (count != 0) {
total = 1 / Double.parseDouble(textField.getText());
textField.setText(total + "");
}
}
/** *********处理“=”********** */
if (e.getSource() == this.button[19]) {
operator = true;
if (count != 0) {
// if (count != 1) {
total += Double.parseDouble(textField.getText());
textField.setText(total + "");
// }
}
}
/** ********获取“-”前的数值********* */
if (e.getSource() == this.button[3]) {
operator = true;
count = 1;
total = Double.parseDouble(textField.getText());
textField.setText(textField.getText());
symbol = 1;
}
/** ********获取“-”前的数值********* */
if (e.getSource() == this.button[8]) {
operator = true;
count = 1;
total = Double.parseDouble(textField.getText());
textField.setText(textField.getText());
symbol = 2;
}
/** ********获取“*”前的数值********* */
if (e.getSource() == this.button[13]) {
operator = true;
count = 1;
total = Double.parseDouble(textField.getText());
textField.setText(textField.getText());
symbol = 3;
}
/** ********获取“/”前的数值********* */
if (e.getSource() == this.button[18]) {
operator = true;
count = 1;
total = Double.parseDouble(textField.getText());
textField.setText(textField.getText());
symbol = 4;
}
switch (symbol) {
case 1:// 计算加法
{
double ad = total + Double.parseDouble(textField.getText());
textField.setText(ad + "");
count = 0;
text = "";
break;
}
case 2:// 计算减法
{
double ad = total - Double.parseDouble(textField.getText());
textField.setText(String.valueOf(ad));
count = 0;
text = "";
break;
}
case 3:// 计算乘法
{
double ad = total * Double.parseDouble(textField.getText());
textField.setText(ad + "");
count = 0;
text = "";
break;
}
case 4:// 计算除法
{
double ad = total / Double.parseDouble(textField.getText());
textField.setText(ad + "");
count = 0;
text = "";
break;
}
}
System.out.println(operator);
}
}
觉得这样写太多重复的代码了
哪位坛友给个好的建议?
大四就好找工作了还要做这么多实验,还全是要做界面的,郁闷