扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
&.VI*F;Xp&+y[{:P
public class Calculator{ ="05Q<V '
private String result = "0"; #c~RA?QY
private int op = 0,add = 1,sub = 2,mul = 3,div = 4; >G4aQp>l
70Hi7 -m-
private double stringToDouble(String x){ u{uY?q_g|s
double y = Double.parseDouble(x); O)?4z**
return y; dt}R\-
} ~;|w-P R
private void operate(String x){ *>H~rY%
double x1 = stringToDouble(x); ;gN VO
double y = stringToDouble(result); {o^VA8 O
switch (op){ J`!nlq3
case 0: F oa8w?v
result = x; T1[5=%p+Zu
break; 2 Npg;Ho
case 1: 2yhj> VFy
result = String.valueOf(y+x1); lgeL,@-k
break; 2#RUc@k14v
case 2: [+9N <
result = String.valueOf(y-x1); UH6 }2]
break; 30z=/g
case 3: 2/OE;/c
result = String.valueOf(y*x1); ZTQ =n7e
break; \P
case 4: llVPB.|gm
if(x1!=0){ .w`7<P!
result = String.valueOf(y/x1); ol^;wC
}else{ OV78,pkN
result = "The divisor can't be zero!"; yY71%N5
} q5X (16
break; `)z*\#
} $(t{-0}5
} Q1K;G?j7r
W_B%9k7i
public String opAdd(String x){ c {&kT O{
operate(x); teU@
op = add; px+8,8Q!
return result; )-IK!FlBEx
} L,X]
public String opSubtract(String x){ *<#_U6["(
operate(x); (Q,lH<B
op = sub; sw>KBFC
return result; 98 K Rr>
} K["\<9a
public String opMultiply(String x){ #I;s!][o{
operate(x); uPd.TUww
op = mul; Q R O4J8
return result; ~f D,z[]5:
} ?_}PJ:e&$~
public String opDivide(String x){ SPs"~)T
operate(x); VR*8D;1#
op = div; 9bt[wwW%Uu
return result; |~H)#+in]
} y1Ila3JV
public String opEquals(String x){ +;xJ(2K-k
operate(x); (FYdSq#>
op = 0; p-MA`!b
return result; &.H PNNP*]
} OkBCi>H=
public void opClean(){ iSf+TBmTs
op = 0; ;`^yCXOy8
result = "0"; ]:TL6:{l![
} 8< G//O
} =E]wLl
a % U`/查看本文来源 ml>
import java.awt.*; *S hJnf.
import java.awt.event.*; E)9 }>lEh
import java.util.EventObject; mCK0*rKB
L6.NPRz/w
public class CalculatorGUI{ r(3R09L;^8
private Frame f; rxx}NFztE
private Panel p1,p2; I='S6
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9; Xk0n[
private Button bPoint,bAdd,bDec,bMul,bDiv,bCal; -7`8 %k
private TextField tf; h# y qG
private String s,op; Wq|c l /
private Calculator cal = new Calculator(); ]D(Ho$\S
private boolean ifOp; +gN,wo
W<Q1fQ22
public CalculatorGUI(){ *:x'i-
f = new Frame("Calculator"); 7VaL}o<j
p1 = new Panel(); 7QhWA>1
p2 = new Panel(); MTC<30 &
G72]JCYB
b0 = new Button("0"); ]FJo%?C`MG
b1 = new Button("1"); q[VXEb
b2 = new Button("2"); DK6Tsi=
b3 = new Button("3"); (MjjH%W
b4 = new Button("4"); #Y9-b%z
b5 = new Button("5"); '>kP7'
b6 = new Button("6"); 4E/4[\%A
b7 = new Button("7"); gJI&%n,z
b8 = new Button("8"); kyzAP|eh~
b9 = new Button("9"); `5$\hXHv`A
bPoint = new Button("."); pw5n[%
bAdd = new Button("+"); S9 3TV~ 5
bDec = new Button("-"); K/QSQIK
bMul = new Button("*"); >K1|1W<&4y
bDiv = new Button("/"); 7s/34A} #
bCal = new Button("="); "n2 ?$
hV~ KM?S
tf = new TextField(25); ^ft0hl!6
tf.setEditable(false); Kb?KEo2a\
*!7I1f
Djf ,[
} Lf7v$1
)KyBEJ;s8
public void launchFrame(){ /*2?rk
f.setSize(220,160); 9&N N)((
f.setResizable(false); b/HbwqK6
f.addWindowListener(new myWindowListener()); M)6jDf2
p1.setLayout(new FlowLayout(FlowLayout.CENTER)); +.Y@S6
p1.add(tf); dmoqBl@z
f.add(p1,BorderLayout.NORTH); 0Q2qxy8oe
p2.setLayout(new GridLayout(4,4)); BMX T|n
B!-&)
b0.addActionListener(new setLabelText_ActionListener()); ib #R^)j+
b1.addActionListener(new setLabelText_ActionListener()); NlOaV5&
b2.addActionListener(new setLabelText_ActionListener()); 1DEJ|ldv\b
b3.addActionListener(new setLabelText_ActionListener()); _0|&/,JCCy
b4.addActionListener(new setLabelText_ActionListener()); 5-C^jqh;
b5.addActionListener(new setLabelText_ActionListener()); kZmY)y
b6.addActionListener(new setLabelText_ActionListener()); J*,gSJ=
b7.addActionListener(new setLabelText_ActionListener()); ;Rzx9h2
b8.addActionListener(new setLabelText_ActionListener()); cnTYm
b9.addActionListener(new setLabelText_ActionListener()); *rbeiua(j
bPoint.addActionListener(new setLabelText_ActionListener()); PEw pn=Fb
bAdd.addActionListener(new setOperator_ActionListener()); >hgX1kq
bDec.addActionListener(new setOperator_ActionListener()); G:C.&8=<
bMul.addActionListener(new setOperator_ActionListener()); 'VeW98l*]
bDiv.addActionListener(new setOperator_ActionListener()); %~Do;k
bCal.addActionListener(new setOperator_ActionListener()); U1-k'
PB!-3_,
p2.add(b7); H4G>h !
p2.add(b8); 7<Pv
p2.add(b9); \tr !>f
p2.add(bAdd); ?WViOmD_
p2.add(b4); b9c/ kIh
p2.add(b5); Jm$ zrP7
p2.add(b6); 8) DA 6dp
p2.add(bDec); mScjy<v
p2.add(b1); \Op*_^KcE
p2.add(b2); 9V!(FPD'
p2.add(b3); A`%|6^3XT
p2.add(bMul); @.vSz;9)
p2.add(b0); Py{C2S*fZZ
p2.add(bPoint); $}":l?V'>
p2.add(bCal); A&\OGi!A3
p2.add(bDiv); gCp ,H)- m
f.add(p2,BorderLayout.SOUTH); t`ZN8dJ
f.setVisible(true); |k' Kv0k
} tYlPjZ\E
%BfplhE1a
public void setTextFieldText_Temp(){ iXhn?_
if (tf.getText().length()<15 && (tf.getText().indexOf(".")==-1 || !s.equals("."))){ -`8Y-2&
tf.setText(tf.getText()+s); Jd L7)_
}else{ E~n1S9;fX
tf.setText((tf.getText()+s).substring(0,15)); DH}VlQ5%
} $M=T6le
} vEsdk}EEb
public void setTextFieldText(){ j0-J9O?T
if(ifOp){ 0%x&q9-le
ifOp = false; [Nv7/_0H
tf.setText(""); -ho$:qw]~4
setTextFieldText_Temp(); ix9 Hh{
}else{ UY"L]:g.
setTextFieldText_Temp(); @O *DVee
} B947*hjs
} tH{``Y9M.R
V~h/pf]5A
public static void main(String[] args){ 3^={\CUh
CalculatorGUI calculator = new CalculatorGUI(); SfPfqL
calculator.launchFrame(); [Qqf]J
} u%kD|/u%z7
<_ HNAs n
class myWindowListener extends WindowAdapter{ q<`_R7C~`
public void windowClosing(WindowEvent e){ bf,m MhE;
System.exit(0); v(psxcq?
} h1-r^['h?
} 9)`g#V2[8
__zn,!AcX5
class setLabelText_ActionListener implements ActionListener{ r6F"k7/3j
public void actionPerformed(ActionEvent e){ tWtZ%brh
Button tempB = (Button)e.getSource(); Zl @Z}
s = tempB.getLabel(); }RJ}O~;1<P
setTextFieldText(); W6sC)7F
} $t|nNfiI
} _],1$dj
{>S!OY^qs
class setOperator_ActionListener implements ActionListener{ Z s0'<6) A
public void actionPerformed(ActionEvent e){ H q]g@?
Button tempB = (Button)e.getSource(); &,a.x"^T
op = tempB.getLabel(); "EWE'E 6
if(op.equals("+")){ k2p&FN
tf.setText(cal.opAdd(tf.getText())); "exF2
ifOp = true; iM!2jnA
}else if(op.equals("-")){ Po"xj,bX
tf.setText(cal.opSubtract(tf.getText())); i0jZ>tnH
ifOp = true; @s2#VTP*
}else if(op.equals("*")){ pR\Sn]
tf.setText(cal.opMultiply(tf.getText())); Sw^)l-u
ifOp = true; =,S!`C?}Q
}else if(op.equals("/")){ 6&jL6i<
tf.setText(cal.opDivide(tf.getText())); d: xn}s
ifOp = true; I#N"td`
}else if(op.equals("=")){ 8C/amEU
tf.setText(cal.opEquals(tf.getText())); C"io/MdK
ifOp = true; n |pgOBk,
} tcC=UT"
} M N 89I
} )IG]G_
} `|6 p1Kir;
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者