ToTallTiTall.java [Last ned]

/*
 * GUI-program for inntasting av verdier i
 * 2-tallsystemet med omregning til 10-sys.
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class ToTallTiTall extends JFrame implements ActionListener {
 
  JTextField txtTo, txtTi;
     JButton knapp0, knapp1, blankKnapp;
         int tall10 = 0;
      String tall2 = "";
 
  public static void main(String[] args)  {
    ToTallTiTall vindu = new ToTallTiTall();
    vindu.setTitle("Tallsystem");
    vindu.setDefaultCloseOperation(EXIT_ON_CLOSE);
    vindu.opprettGUI();
    vindu.pack();
    vindu.setVisible(true);
  }
 
  private void opprettGUI() {
    setLayout( new FlowLayout() );
    knapp0 = new JButton("0");
    knapp0.addActionListener(this);
    add(knapp0);
    knapp1 = new JButton("1");
    knapp1.addActionListener(this);
    add(knapp1);
    txtTo = new JTextField(8);
    // Høyrejusterer siffersekvensen i tekstfeltet
    txtTo.setHorizontalAlignment(JTextField.RIGHT);
    add(txtTo);
    add( new JLabel("(2)  = ") );
    txtTi = new JTextField(8);
    txtTi.setHorizontalAlignment(JTextField.RIGHT);
    add(txtTi);
    add( new JLabel("(10)") );
    blankKnapp = new JButton(" Blank ut ");
    blankKnapp.addActionListener(this);
    add(blankKnapp);
  }
 
  public void actionPerformed (ActionEvent e) {
    if ( e.getSource() == blankKnapp ) {
      txtTo.setText("");
      txtTi.setText("");
      tall10 = 0;
      tall2 = "";
    }
    else if ( e.getSource() == knapp0 ) {
      tall10 = 2*tall10;
      tall2 += "0";
      txtTo.setText(tall2);
      txtTi.setText(""+tall10);
    }
    else if ( e.getSource() == knapp1 ) {
      tall10 = 2*tall10+1;
      tall2 += "1";
      txtTo.setText(tall2);
      txtTi.setText(""+tall10);
    }
  }
 
}
Kildekode blir vist ved hjelp av GeSHi.