Home • ECDL • Algoritmi • Java • Basi di dati • Seconda prova • Eccetera • Cerca nel sito

AWT: Calcolatrice

Precedente
SUPERIORE
Successiva

richiesto il layout seguente

"LCD" "Modello"
7 8 9 / - C
4 5 6 x + CE
1 2 3 % --> M
0 . = */- <-- MR
import java.awt.*;
import java.awt.event.*;

public class Calcolatrice extends Frame implements ActionListener
{
   Label  lcd;
   Label  modello;
   Button bRIGHT[]=new Button[8];
   String lRIGHT[]={ "-", "C", "+", "CE", "->", "M", "<-", "MR"
                   };
   Button bLEFT []=new Button[16];
   String lLEFT []={ "7", "8", "9", "/", "4", "5", "6", "X",
                     "1", "2", "3", "%", "0", ".", "=", "+/-"
                   };

   public Calcolatrice()
   {
      this.setLayout(new GridLayout(2, 2, 10, 10));

      // nord-ovest
      lcd=new Label("0", Label.RIGHT);
      lcd.setBackground(Color.lightGray);
      this.add(lcd);

      // nord-est
      modello=new Label("RDC 2006", Label.CENTER);
      modello.setBackground(Color.lightGray);
      this.add(modello);

      // sud-ovest
      Panel g4x4=new Panel();
      g4x4.setLayout(new GridLayout(4, 4, 2, 2));
      for(int i=0; i < 16; i++)
      {
         bLEFT[i]=new Button(lLEFT[i]);
         bLEFT[i].addActionListener(this);
         g4x4.add(bLEFT[i]);
      }
      this.add(g4x4); 

      // sud-est
      Panel g4x2=new Panel();
      g4x2.setLayout(new GridLayout(4, 2, 2, 2));
      for(int i=0; i < 8; i++)
      {
         bRIGHT[i]=new Button(lRIGHT[i]);
         bRIGHT[i].addActionListener(this);
         g4x2.add(bRIGHT[i]);
      }
      this.add(g4x2); 

      //globale
      this.setTitle("Calcolatrice");
      this.pack();
      this.setResizable(false);
      this.setVisible(true);
   }

   public void actionPerformed(ActionEvent ae)
   {
      String evento=ae.getActionCommand();
      lcd.setText(evento);
   }  
   public static void main(String args[])
   {
      Calcolatrice c=new Calcolatrice(); 
   }
}

Osserva

  1. Frame, setLayout, GridLayout, add, setTitle, pack, setVisible
  2. ActionListener, addActionListener, ActionEvent, getActionCommand

AWT: Calcolatrice - ApPuNtIdIuNiNfOrMaTiCo

Home • ECDL • Algoritmi • Java • Basi di dati • Seconda prova • Eccetera • Cerca nel sito

Precedente
SUPERIORE
Successiva