計算器
- 這個小demo是我上學時的遠古代碼(嘻嘻嘻),今天整理代碼時看到的,看着以前的代碼,突然感覺這些是啥?看不懂了都,而且寫得也不規範。
- 運行一下,還是可以的,先截張圖
- 試了一下,bug還是有的,但是可以基本的運算,有興趣的可以試一下
- 代碼就貼在這裏,感慨!!!
package com.waking.call;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class CalculatorExample {
public static void main(String[] args) {
CalculatorFrame calculatorFrame=new CalculatorFrame();
}
}
class CalculatorFrame extends JFrame{
public CalculatorFrame() {
setTitle("計算器");
setSize(340,500);
CalcultorPanel calcultorPanel=new CalcultorPanel();
getContentPane().add(calcultorPanel);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
class CalcultorPanel extends JPanel{
public static boolean start=true;
public static String result="";
public static String lastcommand="=";
public static boolean firstDigit = true;
public CalcultorPanel() {
setLayout(new BorderLayout());
ShowPanel showPanel=new ShowPanel();
BTNPanel btnPanel=new BTNPanel();
add(showPanel, BorderLayout.NORTH);
add(btnPanel,BorderLayout.CENTER);
}
}
class ShowPanel extends JPanel{
public static JLabel display1;
public static JLabel display2;
public ShowPanel() {
display1=new JLabel("",SwingConstants.RIGHT);
display2=new JLabel("0",SwingConstants.RIGHT);
display1.setFont(new Font("黑體",Font.BOLD,30));
display2.setFont(new Font("黑體",Font.BOLD,40));
display1.setPreferredSize(new Dimension(300, 80));
display2.setPreferredSize(new Dimension(300, 50));
setLayout(new BorderLayout());
add(display1,BorderLayout.NORTH);
add(display2,BorderLayout.CENTER);
}
}
class BTNPanel extends JPanel{
public BTNPanel() {
setLayout(new GridLayout(6, 4));
InsertAciton insertAciton=new InsertAciton();
CommandAction commandAction=new CommandAction();
addButton("%", commandAction);
addButton("√", commandAction);
addButton("pow", commandAction);
addButton("1/x", commandAction);
addButton("CE", commandAction);
addButton("C", commandAction);
addButton("<<", commandAction);
addButton("/",commandAction);
addButton("7",insertAciton);
addButton("8",insertAciton);
addButton("9",insertAciton);
addButton("*",commandAction);
addButton("4",insertAciton);
addButton("5",insertAciton);
addButton("6",insertAciton);
addButton("-",commandAction);
addButton("1",insertAciton);
addButton("2",insertAciton);
addButton("3",insertAciton);
addButton("+",commandAction);
addButton("+/-",commandAction);
addButton("0",insertAciton);
addButton(".",insertAciton);
addButton("=",commandAction);
}
void addButton(String label,ActionListener actionListener) {
JButton button=new JButton(label);
button.setFont(new Font("黑體",Font.BOLD,15));
button.addActionListener(actionListener);
add(button);
}
}
class InsertAciton implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(CalcultorPanel.start==true) {
ShowPanel.display1.setText("");
ShowPanel.display2.setText("");
CalcultorPanel.start=false;
}
if ("0123456789.".indexOf(e.getActionCommand()) >= 0) {
if (CalcultorPanel.firstDigit) {
if(e.getActionCommand().equals(".")) {
ShowPanel.display2.setText("0.");
}else if(e.getActionCommand().equals("0")) {
ShowPanel.display2.setText("0");
CalcultorPanel.firstDigit=true;
return;
}
else ShowPanel.display2.setText(e.getActionCommand());
}
else if ((e.getActionCommand().equals(".")) && (ShowPanel.display2.getText().indexOf(".") < 0)) {
ShowPanel.display2.setText(ShowPanel.display2.getText() + ".");
}
else
if (!e.getActionCommand().equals(".")) {
ShowPanel.display2.setText(ShowPanel.display2.getText() + e.getActionCommand());
}
CalcultorPanel.firstDigit = false;
}
}
}
class CommandAction implements ActionListener{
public void actionPerformed(ActionEvent e) {
String string = e.getActionCommand();
if(string.equals("CE")) {
ShowPanel.display1.setText("");
ShowPanel.display2.setText("0");
}else if(string.equals("C")) {
CalcultorPanel.start=true;
CalcultorPanel.result="";
CalcultorPanel.lastcommand="=";
CalcultorPanel.firstDigit = true;
ShowPanel.display1.setText("");
ShowPanel.display2.setText("0");
}else if(string.equals("<<")) {
ShowPanel.display1.setText("");
String text=ShowPanel.display2.getText();
int i = text.length();
if (i > 0) {
text = text.substring(0, i - 1);
if (text.length() == 0) {
ShowPanel.display2.setText("0");
CalcultorPanel.firstDigit = true;
CalcultorPanel.lastcommand = "=";
} else {
ShowPanel.display2.setText(text);
}
}
} else if(string.equals("+/-")) {
String a1=ShowPanel.display2.getText();
ShowPanel.display1.setText("");
if(a1.indexOf("-")==-1) {
if(Double.parseDouble(a1)!=0)
ShowPanel.display2.setText("-"+ShowPanel.display2.getText());
}
else {
ZeroText(Double.parseDouble(a1)*-1);
}
CalcultorPanel.result=a1;
}else{
CalcultorPanel.start=true;
if(CalcultorPanel.lastcommand.equals("="))
{
CalcultorPanel.result=ShowPanel.display2.getText();
}
else {
if (CalcultorPanel.lastcommand.equals("%")) {
double a1=Double.parseDouble(CalcultorPanel.result);
a1= a1 / 100;
if (CalcultorPanel.result=="0.")
ShowPanel.display1.setText(0+"/100"+"=");
else ShowPanel.display1.setText(CalcultorPanel.result+"/100=");
ZeroText(a1);
CalcultorPanel.result=a1+"";
}else if (CalcultorPanel.lastcommand.equals("√")) {
double a1=Double.parseDouble(CalcultorPanel.result);
a1 = Math.sqrt(a1);
if(CalcultorPanel.result=="0.")
ShowPanel.display1.setText("√("+0+")"+"=");
else ShowPanel.display1.setText("√"+"("+CalcultorPanel.result+")=");
ZeroText(a1);
CalcultorPanel.result=a1+"";
}else if(CalcultorPanel.lastcommand.equals("pow")) {
double a1=Double.parseDouble(CalcultorPanel.result);
double a2=Double.parseDouble(ShowPanel.display2.getText());
double s=Math.pow(a1,a2);
if (CalcultorPanel.result=="0.") {
if(ShowPanel.display2.getText()=="0.")
ShowPanel.display1.setText("pow("+0+","+0+")"+"=");
else ShowPanel.display1.setText("pow("+0+","+ShowPanel.display2.getText()+")"+"=");
}else if(ShowPanel.display2.getText()=="0.") {
ShowPanel.display1.setText("pow("+CalcultorPanel.result+","+0+")"+"=");
}else
ShowPanel.display1.setText("pow"+"("+CalcultorPanel.result+","+ShowPanel.display2.getText()+")=");
ZeroText(s);
CalcultorPanel.result=a1+"";
}else if (CalcultorPanel.lastcommand.equals("1/x")) {
double a1=Double.parseDouble(CalcultorPanel.result);
if (a1 == 0.0) {
ShowPanel.display2.setText("零沒有倒數");
} else {
a1 = 1 / a1;
ShowPanel.display1.setText("1"+"/"+CalcultorPanel.result+"=");
ZeroText(a1);
CalcultorPanel.result=a1+"";
}
}
else if(CalcultorPanel.lastcommand.equals("+")) {
double a1=Double.parseDouble(CalcultorPanel.result);
double a2=Double.parseDouble(ShowPanel.display2.getText());
double s=a1+a2;
if (CalcultorPanel.result=="0.") {
if(ShowPanel.display2.getText()=="0.")
ShowPanel.display1.setText(0+"+"+0+"=");
else ShowPanel.display1.setText(0+"+"+ShowPanel.display2.getText()+"=");
}else if(ShowPanel.display2.getText()=="0.") {
ShowPanel.display1.setText(CalcultorPanel.result+"+"+0+"=");
}else
ShowPanel.display1.setText(CalcultorPanel.result+"+"+ShowPanel.display2.getText()+"=");
ZeroText(s);
CalcultorPanel.result=s+"";
}
else if(CalcultorPanel.lastcommand.equals("-")) {
double a1=Double.parseDouble(CalcultorPanel.result);
double a2=Double.parseDouble(ShowPanel.display2.getText());
double s=a1-a2;
if (CalcultorPanel.result=="0.") {
if(ShowPanel.display2.getText()=="0.")
ShowPanel.display1.setText(0+"-"+0+"=");
else ShowPanel.display1.setText(0+"-"+ShowPanel.display2.getText()+"=");
}else if(ShowPanel.display2.getText()=="0.") {
ShowPanel.display1.setText(CalcultorPanel.result+"-"+0+"=");
}else
ShowPanel.display1.setText(CalcultorPanel.result+"-"+ShowPanel.display2.getText()+"=");
ZeroText(s);
CalcultorPanel.result=s+"";
}else if(CalcultorPanel.lastcommand.equals("*")) {
double a1=Double.parseDouble(CalcultorPanel.result);
double a2=Double.parseDouble(ShowPanel.display2.getText());
double s=a1*a2;
if (CalcultorPanel.result=="0.") {
if(ShowPanel.display2.getText()=="0.")
ShowPanel.display1.setText(0+"*"+0+"=");
else ShowPanel.display1.setText(0+"*"+ShowPanel.display2.getText()+"=");
}else if(ShowPanel.display2.getText()=="0.") {
ShowPanel.display1.setText(CalcultorPanel.result+"*"+0+"=");
}else
ShowPanel.display1.setText(CalcultorPanel.result+"*"+ShowPanel.display2.getText()+"=");
ZeroText(s);
CalcultorPanel.result=s+"";
}else if (CalcultorPanel.lastcommand.equals("/")) {
double a1=Double.parseDouble(CalcultorPanel.result);
double a2=Double.parseDouble(ShowPanel.display2.getText());
if (a2 == 0.0) {
ShowPanel.display2.setText("除數不能爲零");
} else {
double s=a1/a2;
if(CalcultorPanel.result=="0.")
ShowPanel.display1.setText(0+"/"+ShowPanel.display2.getText()+"=");
else ShowPanel.display1.setText(CalcultorPanel.result+"/"+ShowPanel.display2.getText()+"=");
ZeroText(s);
CalcultorPanel.result=s+"";
}
}
} CalcultorPanel.lastcommand=e.getActionCommand();
CalcultorPanel.firstDigit = true;
}
}
public void ZeroText(double s) {
long t1;
double t2;
t1 = (long) s;
t2 = s-t1;
if (t2 == 0) {
ShowPanel.display2.setText(String.valueOf(t1));
} else {
ShowPanel.display2.setText(String.valueOf(s));
}
}
}
- 不要介意我把類寫在一起,因爲那時我爲什麼寫在一起,我現在也不知道了呀!
- 有興趣的你可以研究研究,感謝您的觀看。