Si puņ usare CheckboxGroup piuttosto che Choice...
import java.awt.*;
import java.awt.event.*;
public class CmPollici extends Frame implements ActionListener
{
private TextField tf;
private Button bu;
private Label la;
private Checkbox cb1, cb2;
private CheckboxGroup cg;
private final float FC=2.54f;
public CmPollici()
{
// Nord
tf=new TextField("Input");
this.add(tf, BorderLayout.NORTH);
// Centro
cg=new CheckboxGroup(); cb1=new Checkbox("DA cm A pollici", cg, true);
cb2=new Checkbox("DA pollici A cm", cg, false);
bu=new Button("Calcola"); bu.addActionListener(this);
Panel p=new Panel(); p.add(cb1);
p.add(cb2);
p.add(bu);
this.add(p, BorderLayout.CENTER);
// Sud
la=new Label("Output", Label.CENTER);
this.add(la, BorderLayout.SOUTH );
// Globale
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String s="";
float f=Float.parseFloat(tf.getText());
float r=0.0f;
Object o=cg.getSelectedCheckbox();
if(o == cb1) { r=f/FC; s=f + " cm = " + r + " pollici"; }
else if(o == cb2) { r=f*FC; s=f + " pollici = " + r + " cm" ; }
la.setText(s);
}
public static void main(String[] args)
{
CmPollici f=new CmPollici();
}
} |