java 간단하게 win 10 계산기 따라 하기
이 작은 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;
/**
*
* @author waking
*
*/
public class CalculatorExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
CalculatorFrame calculatorFrame=new CalculatorFrame();
}
}
class CalculatorFrame extends JFrame{
public CalculatorFrame() {//
setTitle(" ");
setSize(340,500);
CalcultorPanel calcultorPanel=new CalcultorPanel();
getContentPane().add(calcultorPanel);// Frame
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) {
// TODO Auto-generated method stub
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")) {// “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(Double.parseDouble(ShowPanel.display2.getText())!=0)
if (!e.getActionCommand().equals(".")) {
// ,
ShowPanel.display2.setText(ShowPanel.display2.getText() + e.getActionCommand());
}
//
CalcultorPanel.firstDigit = false;
}
}
}
class CommandAction implements ActionListener{//
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
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(); // 1
}
else {
if (CalcultorPanel.lastcommand.equals("%")) {
// , 100
double a1=Double.parseDouble(CalcultorPanel.result);
a1= a1 / 100;
if (CalcultorPanel.result=="0.") // 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.")// 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.") {// 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.") {// 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.") {// 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.") {// 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("/")) {
//
// 0
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.")// 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));
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.