Ad ogni modo, questo è il codice:
codice:
package provaFinestre;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author s_e_b_a
*/
public class ProvaFinestre extends Frame {
static float a, b;
public int tmp = 0;
Label welcome = new Label("Started: Prova Finestre");
Label result = new Label("Risultato:");
Button zero = new Button("0");
Button one = new Button("1");
Button two = new Button("2");
Button three = new Button("3");
Button four = new Button("4");
Button five = new Button("5");
Button six = new Button("6");
Button seven = new Button("7");
Button eight = new Button("8");
Button nine = new Button("9");
Button plus = new Button("+");
Panel container = new Panel(new GridLayout(2,2));
Panel buttons = new Panel(new GridLayout(4,3));
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
provaFinestre();
float sum = a + b;
}
public void provaFinestre() {
zero.addActionListener(new Listener());
one.addActionListener(new Listener());
two.addActionListener(new Listener());
three.addActionListener(new Listener());
four.addActionListener(new Listener());
five.addActionListener(new Listener());
six.addActionListener(new Listener());
seven.addActionListener(new Listener());
eight.addActionListener(new Listener());
nine.addActionListener(new Listener());
plus.addActionListener(new Listener());
zero.setActionCommand("CLICK");
one.setActionCommand("CLICK");
two.setActionCommand("CLICK");
three.setActionCommand("CLICK");
four.setActionCommand("CLICK");
five.setActionCommand("CLICK");
six.setActionCommand("CLICK");
seven.setActionCommand("CLICK");
eight.setActionCommand("CLICK");
nine.setActionCommand("CLICK");
plus.setActionCommand("CLICK");
container.add(buttons, BorderLayout.WEST);
buttons.add(zero);
buttons.add(one);
buttons.add(two);
buttons.add(three);
buttons.add(four);
buttons.add(five);
buttons.add(six);
buttons.add(seven);
buttons.add(eight);
buttons.add(nine);
pack();
show();
}
public class Listener implements ActionListener {
public void actionPerformed (ActionEvent e, String buttonName) {
String command = e.getActionCommand();
String name = buttonName;
if (command.compareTo("CLICK")==0){
if (tmp == 0){
if ("0".equals(name)) a = 0;
if ("1".equals(name)) a = 1;
if ("2".equals(name)) a = 2;
if ("3".equals(name)) a = 3;
if ("4".equals(name)) a = 4;
if ("5".equals(name)) a = 5;
if ("6".equals(name)) a = 6;
if ("7".equals(name)) a = 7;
if ("8".equals(name)) a = 8;
if ("9".equals(name)) a = 9;
tmp++;
}
if (tmp == 1){
if ("0".equals(name)) b = 0;
if ("1".equals(name)) b = 1;
if ("2".equals(name)) b = 2;
if ("3".equals(name)) b = 3;
if ("4".equals(name)) b = 4;
if ("5".equals(name)) b = 5;
if ("6".equals(name)) b = 6;
if ("7".equals(name)) b = 7;
if ("8".equals(name)) b = 8;
if ("9".equals(name)) b = 9;
tmp++;
}
if (tmp > 1) System.err.println("tmp variable error!");
}
}
@Override
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}