//Calculateur import java.awt.*; import java.applet.*; public class Calculateur extends Applet { int arg1,arg2; Button number[] = new Button[10]; Button func[] = new Button[4]; Panel num_panel; Panel func_panel; Panel rez_panel; TextField rez; int op_state; //0 - premier, 1 - deuxieme argument char oper; public void
init(){
num_panel = new Panel(); |
|
num_panel.add(new Button());
func[1] = new Button("=");
num_panel.add(func[1]);
add("Center", num_panel);
func_panel = new Panel();
func_panel.setLayout(new
GridLayout(4,1));
func[2] = new Button("+");
func_panel.add(func[2]);
func[3] = new Button("-");
func_panel.add(func[3]);
func_panel.add(new Button());
func_panel.add(new Button());
add("East",func_panel);
rez_panel = new Panel();
rez = new TextField("0",5);
rez_panel.add(rez);
add("North",rez_panel);
}
public boolean action(Event evt, Object arg) {
for (int i = 0; i<=9; i++)
if(evt.target.equals(number[i])){
if (op_state != 0){
arg2 = i;rez.setText(arg2
+ ""); }
else {arg1 = i;
rez.setText(arg1 + "");}
return true;
}
for (int i = 1; i< 4; i++)
if(evt.target.equals(func[i]))
{
switch (i) {
case 1: if (oper == '+')
rez.setText((arg1
+ arg2)+ "");
else if ( oper
== '-')
rez.setText((arg1-arg2)
+ "");
op_state = 0; break;
case 2: oper = '+'; op_state
= 1; break;
case 3: oper = '-'; op_state
= 1;
}
return true;
}
return super.action(evt,arg);
}
}
//CalculateurN import java.awt.*; import java.awt.event.*; import java.applet.*; public class CalculateurN extends Applet { String arg1=""; String arg2=""; int rezVal; Button number[] = new Button[10]; Button func[] = new Button[4]; Panel num_panel; Panel func_panel; Panel rez_panel; TextField rez; int op_state; //0 - premier, 1 - deuxieme argument char oper; public void
init(){ num_panel =
new Panel(); |
|
num_panel.add(new
Button());
func[1] = new Button("=");
func[1].addActionListener(new BE());
num_panel.add(func[1]);
add("Center", num_panel);
func_panel = new
Panel();
func_panel.setLayout(new GridLayout(4,1));
func[2] = new Button("+");
func[2].addActionListener(new BA());
func_panel.add(func[2]);
func[3] = new
Button("-");
func[3].addActionListener(new BS());
func_panel.add(func[3]);
func_panel.add(new Button());
func_panel.add(new Button());
add("East",func_panel);
rez_panel = new
Panel();
rez = new TextField("0",5);
rez_panel.add(rez);
add("North",rez_panel);
}
class BN implements
ActionListener{
public void actionPerformed(ActionEvent e) {
if (op_state != 0){
arg2 += e.getActionCommand();
rez.setText(arg2);
}
else {
arg1 += e.getActionCommand();
rez.setText(arg1);
}
}
}
class BE implements
ActionListener{
public void actionPerformed(ActionEvent e) {
if(oper == '+')
rezVal = (new Integer(arg1)).intValue()
+ (new Integer(arg2)).intValue();
else
rezVal = (new Integer(arg1)).intValue()
- (new Integer(arg2)).intValue();
rez.setText(rezVal+"");
arg1= ""; arg2 = ""; op_state = 0;
}
}
class BA implements
ActionListener{
public void actionPerformed(ActionEvent e) {
oper = '+';
op_state = 1;
}
}
class BS implements
ActionListener{
public void actionPerformed(ActionEvent e) {
oper = '-';
op_state = 1;
}
}
// To close the application:
static class WL extends WindowAdapter {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
// A main() for the
application:
public static void main(String[] args) {
CalculateurN applet = new
CalculateurN();
Frame aFrame = new
Frame("CalculateurN");
aFrame.addWindowListener(new WL());
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300,200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
//CalcNC import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; public class CalcNC extends Applet { String arg=""; int rezVal=0; Button number[] = new Button[10]; Button func[] = new Button[6]; Panel num_panel; Panel func_panel; Panel rez_panel; TextField rez,toper; char oper; public void init(){ oper = ' '; setLayout(null); num_panel = new Panel(); num_panel.setLayout(new GridLayout(4,3)); for(int i=9; i>=0; i--){ number[i] = new Button((new String()).valueOf(i)); number[i].addActionListener(new BN()); num_panel.add(number[i]); } func[0] = new Button("C"); func[0].addActionListener(new C()); num_panel.add(func[0]); func[1] = new Button("="); func[1].addActionListener(new OPER()); num_panel.add(func[1]); num_panel.setBounds(20,100,180,180); add( num_panel); func_panel = new Panel(); func_panel.setLayout(new GridLayout(4,1)); func[2] = new Button("+"); func[2].addActionListener(new OPER()); func_panel.add(func[2]); func[3] = new Button("-"); func[3].addActionListener(new OPER()); func_panel.add(func[3]); func[4] = new Button("*"); func[4].addActionListener(new OPER()); func_panel.add(func[4]); func[5] = new Button("^"); func[5].addActionListener(new OPER()); func_panel.add(func[5]); func_panel.setBounds(210,100,65,180); add(func_panel); rez_panel = new Panel(); rez = new TextField("0",10); toper = new TextField(" ",1); rez_panel.add(rez); rez_panel.add(toper); rez_panel.setBounds(20,20,240,40); add(rez_panel); } class BN implements ActionListener{ public void actionPerformed(ActionEvent e) { arg += e.getActionCommand(); rez.setText(arg); toper.setText(""); } } class OPER implements ActionListener{ public void actionPerformed(ActionEvent e) { int i,help,argi; if(arg.length()==0)argi=0; else argi = (new Integer(arg)).intValue(); switch(oper){ case '+': rezVal += argi;break; case '-': rezVal -= argi;break; case '*': rezVal *= argi;break; case '=': oper = ' ';break; case '^': for(i=1,help=rezVal; i < argi;i++) rezVal *=help; break; case ' ': rezVal= argi; } rez.setText(rezVal+""); arg = ""; oper = e.getActionCommand().charAt(0); if(oper!='=')toper.setText(oper+""); else toper.setText(""); } } class C implements ActionListener{ public void actionPerformed(ActionEvent e) { arg = ""; rezVal =0; oper = ' '; rez.setText(rezVal+""); toper.setText(""); } } // A main() for the application: public static void main(String[] args) { CalcNC applet = new CalcNC(); JFrame aFrame = new JFrame("CalcNC"); aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aFrame.add(applet, BorderLayout.CENTER); aFrame.setSize(400,400); applet.init(); applet.start(); aFrame.setVisible(true); } } |
CalcNC
: |