KalkApp.java [Last ned]

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
 
/*
 *  Brukergrensesnittet for en enkel kalkulator.
 *
 *  Kalkulatoren har knapper for +, *, -, /,
 *  snu fortegn og blank ut, samt et tekstfelt
 *  som viser nåværende verdi.
 *
 */
public class KalkApp extends JFrame
                     implements ActionListener {
 
  private Kalkulator kalk = new Kalkulator();
 
  private JPanel knapper = new JPanel();
 
  private JTextField display = new JTextField( 15 );
 
  private JButton k0 = new JButton( "0" );
  private JButton k1 = new JButton( "1" );
  private JButton k2 = new JButton( "2" );
  private JButton k3 = new JButton( "3" );
  private JButton k4 = new JButton( "4" );
  private JButton k5 = new JButton( "5" );
  private JButton k6 = new JButton( "6" );
  private JButton k7 = new JButton( "7" );
  private JButton k8 = new JButton( "8" );
  private JButton k9 = new JButton( "9" );
 
  private JButton pluss    = new JButton( "+" );
  private JButton minus    = new JButton( "-" );
  private JButton mult     = new JButton( "*" );
  private JButton divisjon = new JButton( "/" );
  private JButton fortegn  = new JButton( "+/-" );
  private JButton c        = new JButton( "C" );
  private JButton erLik    = new JButton( "=" );
 
 
  public static void main(String[] args) {
    KalkApp gui = new KalkApp();
  }
 
 
  public KalkApp() {
    this.setTitle("Kalkulator");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    init();
    this.pack();
    this.setVisible(true);
  }
 
 
  public void init() {
 
    // Sett font og farge på GUI-komponentene.
    Font font = new Font("Arial", Font.PLAIN, 24);
 
    k0.setForeground( Color.blue );        k0.setFont(font);
    k1.setForeground( Color.blue );        k1.setFont(font);
    k2.setForeground( Color.blue );        k2.setFont(font);
    k3.setForeground( Color.blue );        k3.setFont(font);
    k4.setForeground( Color.blue );        k4.setFont(font);
    k5.setForeground( Color.blue );        k5.setFont(font);
    k6.setForeground( Color.blue );        k6.setFont(font);
    k7.setForeground( Color.blue );        k7.setFont(font);
    k8.setForeground( Color.blue );        k8.setFont(font);
    k9.setForeground( Color.blue );        k9.setFont(font);
    pluss.setForeground( Color.red );      pluss.setFont(font);
    minus.setForeground( Color.red );      minus.setFont(font);
    mult.setForeground( Color.red );       mult.setFont(font);
    divisjon.setForeground( Color.red );   divisjon.setFont(font);
    fortegn.setForeground( Color.blue );   fortegn.setFont(font);
    erLik.setForeground( Color.red );      erLik.setFont(font);
    c.setForeground( Color.red.darker() ); c.setFont(font);
 
    display.setEditable( false );          display.setFont(font);
    display.setHorizontalAlignment(JTextField.RIGHT);
 
 
    // Layout-håndterer for å legge ut knappene
    // i "rader" og "kolonner".
    GridLayout gridlayout = new GridLayout(4, 4);
    knapper.setLayout( gridlayout );
 
    // Legg ut knapper i knappepanelet.
    knapper.add( k7 );
    knapper.add( k8 );
    knapper.add( k9 );
    knapper.add( pluss );
 
    knapper.add( k4 );
    knapper.add( k5 );
    knapper.add( k6 );
    knapper.add( minus );
 
    knapper.add( k1 );
    knapper.add( k2 );
    knapper.add( k3 );
    knapper.add( mult );
 
    knapper.add( k0 );
    knapper.add( fortegn );
    knapper.add( c );
    knapper.add( divisjon );
 
    // Legg sifferknapper, display, er-lik knapp ut i
    // hhv. "nord", "senter" og "sør i hovedvinduet.
    setLayout( new BorderLayout() );
    add( display, BorderLayout.NORTH );
    add( knapper, BorderLayout.CENTER );
    add( erLik, BorderLayout.SOUTH );
 
    // Registrering av lyttere.
    k0.addActionListener( this );
    k1.addActionListener( this );
    k2.addActionListener( this );
    k3.addActionListener( this );
    k4.addActionListener( this );
    k5.addActionListener( this );
    k6.addActionListener( this );
    k7.addActionListener( this );
    k8.addActionListener( this );
    k9.addActionListener( this );
    pluss.addActionListener( this );
    minus.addActionListener( this );
    mult.addActionListener( this );
    divisjon.addActionListener( this );
    fortegn.addActionListener( this );
    c.addActionListener( this );
    erLik.addActionListener( this );
  }
 
 
  public void actionPerformed(ActionEvent e) {
    if      (e.getSource()==c)        kalk.nullstill();
    else if (e.getSource()==pluss)    kalk.oper('+');
    else if (e.getSource()==minus)    kalk.oper('-');
    else if (e.getSource()==mult)     kalk.oper('*');
    else if (e.getSource()==divisjon) kalk.oper('/');
    else if (e.getSource()==fortegn)  kalk.snuFortegn();
    else if (e.getSource()==erLik)    kalk.erLik();
    else if (e.getSource()==k0)       kalk.siffer(0);
    else if (e.getSource()==k1)       kalk.siffer(1);
    else if (e.getSource()==k2)       kalk.siffer(2);
    else if (e.getSource()==k3)       kalk.siffer(3);
    else if (e.getSource()==k4)       kalk.siffer(4);
    else if (e.getSource()==k5)       kalk.siffer(5);
    else if (e.getSource()==k6)       kalk.siffer(6);
    else if (e.getSource()==k7)       kalk.siffer(7);
    else if (e.getSource()==k8)       kalk.siffer(8);
    else if (e.getSource()==k9)       kalk.siffer(9);
 
    display.setText(kalk.hentVerdi()); // Vis ny verdi
  }
 
}
 
Kildekode blir vist ved hjelp av GeSHi.