⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java4.txt

📁 一个计算器的源代码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
加0;否则保持原来的数值不变 */ 
else if (e.getSource() == number_key.zero) { 
if (com || i == 0) { 
text.setText("0"); 
com = false; 
i = 1; 
}else{ 
sum = text.getText(); 
if (Float.parseFloat(sum) > 0 || Float.parseFloat(sum) < 0) { 
text.setText(sum + "0"); 
}else{ 
if (sum.trim().indexOf(".") == -1) { 
text.setText(sum); 
}else{ 
text.setText(sum + "0"); 
} 
} 
} 
}//计算+/- 
else if (e.getSource() == number_key.ps) { 
if (com) { 
total = Double.parseDouble(text.getText()); 
double ad = total - 2*total; 
text.setText(ad + ""); 
com = false; 
i = 1; 
}else if(com=true) { 
double ad=Double.parseDouble(text.getText()) 
-2*Double.parseDouble(text.getText()); 
text.setText(ad + ""); 
} 
}else if (e.getSource() == action_key.c) { 
text.setText("0"); 
String p=text.getText(); 
total = Double.parseDouble(p); 
p="0"; 
i=0; 
com=false; 
}else if (e.getSource() == number_key.ce) { 
text.setText("0"); 
i=0; 
}else if (e.getSource() == action_key.backspace) { 
if(text.getText()!=""){ 
String p=text.getText(); 
int n=p.length(); 
String q=p.substring(0,n-1); 
if(n!=1){ 
text.setText(q +""); 
} 
if(n==1){ 
text.setText(""); 
sum=""; 
} 
} 
}else if (e.getSource() == number_key.point) { 
if (com || i == 0) { 
text.setText("0."); 
com = false; 
i = 1; 
}else{ 
sum = text.getText(); 
if (sum.trim().indexOf(".") == -1) { 
text.setText(sum + "."); 
}else{ 
text.setText(sum); 
} 

} 
}else if (e.getSource() == number_key.sqrt) { 
com = false; 
i = 0; 
total = Double.parseDouble(text.getText()); 
double ad=Math.sqrt(total); 
text.setText(ad + ""); 
}else if (e.getSource() == number_key.ds) { 
com = false; 
i = 0; 
total = Double.parseDouble(text.getText()); 
double ad=1/total; 
text.setText(ad + ""); 
}//获得点击+之前的数值 
else if (e.getSource() == number_key.plus) { 
com = true; 
i = 0; 
total = Double.parseDouble(text.getText()); 
symbol = 1; 
}//获得点击-之前的数值 
else if (e.getSource() == number_key.sub) { 
com = true; 
i = 0; 
total = Double.parseDouble(text.getText()); 
symbol = 2; 
}//获得点击*之前的数值 
else if (e.getSource() == number_key.mul) { 
com = true; 
i = 0; 
total = Double.parseDouble(text.getText()); 
System.out.println(total); 
symbol = 3; 
}//获得点击/之前的数值 
else if (e.getSource() == number_key.div) { 
com = true; 
i = 0; 
total = Double.parseDouble(text.getText()); 
symbol = 4; 
}else if (e.getSource() == number_key.equal) { 
switch (symbol) { 
case 1 ://计算加法 
{ 
double ad = 
total + Double.parseDouble(text.getText()); 
text.setText(ad + ""); 
i = 0; 
sum = ""; 

break; 
} 
case 2 ://计算减法 
{ 

double ad = 
total - Double.parseDouble(text.getText()); 
text.setText(ad + ""); 
i = 0; 
sum = ""; 
break; 
} 
case 3 ://计算乘法 
{ 
double ad = 
total * Double.parseDouble(text.getText()); 
text.setText(ad + ""); 
i = 0; 
sum = ""; 
break; 
} 
case 4 ://计算除法 
{ 
double ad = 
total / Double.parseDouble(text.getText()); 
if(Double.parseDouble(text.getText())!=0){text.setText(ad + "");} 
else text.setText("ERROR"); 
i = 0; 
sum = ""; 
break; 
} 

} 
System.out.println(com); 
} 
} 


class seven2Dpaint extends JTextField{ 

public void paintComponent(Graphics comp){ 

super.paintComponent(comp); 
drawStick gt =new drawStick(); 

StringBuffer m=new StringBuffer(text.getText()); 
for (int p=0;p<=text.getText().length()-1;p++){ 

char lc = m.charAt(p); 
if(lc==′1′){ 
gt.drawRightUp(comp,p); 
gt.drawRightDown(comp,p); 
} 
else if(lc==′2′){ 
gt.drawUp(comp,p); 
gt.drawRightUp(comp,p); 
gt.drawMid(comp,p); 
gt.drawLeftDown(comp,p); 
gt.drawDown(comp,p); 
} 
else if(lc==′3′){ 
gt.drawUp(comp,p); 
gt.drawRightUp(comp,p); 
gt.drawMid(comp,p); 
gt.drawRightDown(comp,p); 
gt.drawDown(comp,p); 
} 
else if(lc==′4′){ 
gt.drawLeftUp(comp,p); 
gt.drawMid(comp,p); 
gt.drawRightUp(comp,p); 
gt.drawRightDown(comp,p); 
} 
else if(lc==′5′){ 
gt.drawUp(comp,p); 
gt.drawLeftUp(comp,p); 
gt.drawMid(comp,p); 
gt.drawRightDown(comp,p); 
gt.drawDown(comp,p); 
} 
else if(lc==′6′){ 
gt.drawLeftDown(comp,p); 
gt.drawUp(comp,p); 
gt.drawDown(comp,p); 
gt.drawLeftUp(comp,p); 
gt.drawRightDown(comp,p); 
gt.drawMid(comp,p); 
} 
else if(lc==′7′){ 
gt.drawUp(comp,p); 
gt.drawLeftUp(comp,p); 
gt.drawRightUp(comp,p); 
gt.drawRightDown(comp,p); 
} 
else if(lc==′8′){ 
gt.drawRightUp(comp,p); 
gt.drawLeftDown(comp,p); 
gt.drawUp(comp,p); 
gt.drawDown(comp,p); 
gt.drawLeftUp(comp,p); 
gt.drawRightDown(comp,p); 
gt.drawMid(comp,p); 
} 
else if(lc==′9′){ 
gt.drawRightUp(comp,p); 
gt.drawUp(comp,p); 
gt.drawDown(comp,p); 
gt.drawLeftUp(comp,p); 
gt.drawRightDown(comp,p); 
gt.drawMid(comp,p); 
} 
else if(lc==′0′){ 
gt.drawRightUp(comp,p); 
gt.drawLeftDown(comp,p); 
gt.drawUp(comp,p); 
gt.drawDown(comp,p); 
gt.drawLeftUp(comp,p); 
gt.drawRightDown(comp,p); 
} 
else if(lc==′-′){ 
gt.drawMid(comp,p); 
} 
else if(lc==′.′){ 
gt.drawPoint(comp,p); 
} 
else if(lc==′E′){ 
gt.drawUp(comp,p); 
gt.drawDown(comp,p); 
gt.drawMid(comp,p); 
gt.drawLeftUp(comp,p); 
gt.drawLeftDown(comp,p); 
} 
else if(lc==′O′){ 
gt.drawUp(comp,p); 
gt.drawDown(comp,p); 
gt.drawLeftUp(comp,p); 
gt.drawLeftDown(comp,p); 
gt.drawRightUp(comp,p); 
gt.drawRightDown(comp,p); 
} 
else if(lc==′R′){ 
gt.drawUp(comp,p); 
gt.drawMid(comp,p); 
gt.drawLeftUp(comp,p); 
gt.drawLeftDown(comp,p); 
gt.drawRightUp(comp,p); 
gt.drawR(comp,p); 
} 

} 

} 
} 


//********************************画图****************************************** 


class drawStick{ 
public void drawUp(Graphics g,int n){ 
Graphics2D g2 = (Graphics2D)g; 
int x1[] = { 6+n*25,10+n*25,18+n*25,22+n*25}; 
int y1[] = { 4,8,8,4 }; 
int pts1 = x1.length; 
Polygon poly1 = new Polygon(x1, y1, pts1); 
g2.setColor(Color.green); 
g2.drawPolygon(poly1); 
g2.fillPolygon(poly1); 
} 
public void drawLeftUp(Graphics g,int n){ 
Graphics2D g2 = (Graphics2D)g; 
int x2[] = {4+n*25,4+n*25,8+n*25,8+n*25}; 
int y2[] = {6,26,22,10}; 
int pts2 = x2.length; 
Polygon poly2 = new Polygon(x2,y2,pts2); 
g2.setColor(Color.green); 
g2.drawPolygon(poly2); 
g2.fillPolygon(poly2); 

} 
public void drawRightUp(Graphics g,int n){ 
Graphics2D g2 = (Graphics2D)g; 
int x3[] = {20+n*25,20+n*25,24+n*25,24+n*25}; 
int y3[] = {10,22,26,6}; 
int pts3 = x3.length; 
Polygon poly3 = new Polygon(x3,y3,pts3); 
g2.setColor(Color.green); 
g2.drawPolygon(poly3); 
g2.fillPolygon(poly3); 
} 
public void drawMid(Graphics g,int n){ 
Graphics2D g2 = (Graphics2D)g; 
int x4[] = {6+n*25,10+n*25,18+n*25,22+n*25,18+n*25,10+n*25}; 
int y4[] = {28,30,30,28,26,26}; 
int pts4 = x4.length; 
Polygon poly4 = new Polygon(x4,y4,pts4); 
g2.setColor(Color.green); 
g2.drawPolygon(poly4); 
g2.fillPolygon(poly4); 
} 
public void drawLeftDown(Graphics g,int n){ 
Graphics2D g2 = (Graphics2D)g; 
int x5[] = {4+n*25,4+n*25,8+n*25,8+n*25}; 
int y5[] = {30,50,46,34}; 
int pts5 = x5.length; 
Polygon poly5 = new Polygon(x5,y5,pts5); 
g2.setColor(Color.green); 
g2.drawPolygon(poly5); 
g2.fillPolygon(poly5); 
} 
public void drawRightDown(Graphics g,int n){ 
Graphics2D g2 = (Graphics2D)g; 
int x6[] = {20+n*25,20+n*25,24+n*25,24+n*25}; 
int y6[] = {34,46,50,30}; 
int pts6 = x6.length; 
Polygon poly6 = new Polygon(x6,y6,pts6); 
g2.setColor(Color.green); 
g2.drawPolygon(poly6); 
g2.fillPolygon(poly6); 
} 
public void drawDown(Graphics g,int n){ 
Graphics2D g2 = (Graphics2D)g; 
int x7[] = {6+n*25,10+n*25,18+n*25,22+n*25}; 
int y7[] = {52,48,48,52}; 
int pts7 = x7.length; 
Polygon poly7 = new Polygon(x7, y7, pts7); 
g2.setColor(Color.green); 
g2.drawPolygon(poly7); 
g2.fillPolygon(poly7); 
} 
public void drawPoint(Graphics g,int n){ 
Graphics2D g2 = (Graphics2D)g; 
int x8[] = {24+n*20,25+n*20,25+n*20,24+n*20}; 
int y8[] = {48,48,49,49}; 
int pts8 = x8.length; 
Polygon poly8 = new Polygon(x8, y8, pts8); 
g2.setColor(Color.green); 
g2.drawPolygon(poly8); 
g2.fillPolygon(poly8); 
} 
public void drawR(Graphics g,int n){ 
Graphics2D g2 = (Graphics2D)g; 
int x9[] = {11+n*25,23+n*25,23+n*25,11+n*25}; 
int y9[] = {34,45,50,39}; 
int pts9 = x9.length; 
Polygon poly9 = new Polygon(x9, y9, pts9); 
g2.setColor(Color.green); 
g2.drawPolygon(poly9); 
g2.fillPolygon(poly9); 
} 
} 

public static void main(String args[]){ 
JFrame frame=new cal(); 
frame.show(); 
frame.setVisible(true); 
} 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -