JavaGrundernaIntroduktion av awtHändelser
[ Hem ] Allmänt ASP XML SQL ADO HTML CSS VB Java Design Karta
Grunderna Exempel

Händelser

Nedan tar jag upp de vanligaste händelserna i awt. Jag tar upp en åt gången. För varje steg är de nya raderna markerade med fetstil.

Utgångsprogrammet

import java.awt.*;
import java.applet.*;


public class Applet1 extends Applet
{
  Label label1 = new Label("text för labeln");
  Button button1 = new Button("text för knappen");
  Checkbox check1 = new Checkbox("en checkbox");
  CheckboxGroup grp = new CheckboxGroup();
  Checkbox check2 = new Checkbox("ett",grp,false);
  Checkbox check3 = new Checkbox("två",grp,true);
  Choice choice1 = new Choice();
  List list1 = new List();
  TextField text1 = new TextField("skriv här");
  TextArea area1 = new TextArea("text", 10,30);
  public void init()
  {
     this.add(label1);
     this.add(button1);
     this.add(check1);
     this.add(check2);
     this.add(check3);
     this.add(choice1);
     choice1.add("en rad");
     choice1.add("en till rad");
     choice1.add("en tredje rad");
     this.add(list1);
     list1.add("en rad");
     list1.add("en till rad");
     list1.add("en tredje rad");
     this.add(text1);
     this.add(area1);
  }
}

Resultat:

Klick för knappar

import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class Applet1 extends Applet implements ActionListener
{
  Label label1 = new Label("text för labeln");
  Button button1 = new Button("text för knappen");
  Checkbox check1 = new Checkbox("en checkbox");
  CheckboxGroup grp = new CheckboxGroup();
  Checkbox check2 = new Checkbox("ett",grp,false);
  Checkbox check3 = new Checkbox("två",grp,true);
  Choice choice1 = new Choice();
  List list1 = new List();
  TextField text1 = new TextField("skriv här");
  TextArea area1 = new TextArea("text", 10,30);

  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == button1)
    {
      // gör något
      label1.setText("HEPP");
    }
  }

  public void init()
  {
     this.add(label1);
     this.add(button1);
     this.add(check1);
     this.add(check2);
     this.add(check3);
     this.add(choice1);
     choice1.add("en rad");
     choice1.add("en till rad");
     choice1.add("en tredje rad");
     this.add(list1);
     list1.add("en rad");
     list1.add("en till rad");
     list1.add("en tredje rad");
     this.add(text1);
     this.add(area1);
     this.add(area1);
     button1.addActionListener( this );
  }
}

Resultat:

Händelser för Choice, List, Checkbox mm

import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class Applet1 extends Applet implements ItemListener
{
  Label label1 = new Label("text för labeln");
  Button button1 = new Button("text för knappen");
  Checkbox check1 = new Checkbox("en checkbox");
  CheckboxGroup grp = new CheckboxGroup();
  Checkbox check2 = new Checkbox("ett",grp,false);
  Checkbox check3 = new Checkbox("två",grp,true);
  Choice choice1 = new Choice();
  List list1 = new List();
  TextField text1 = new TextField("skriv här");
  TextArea area1 = new TextArea("text", 10,30);

  public void itemStateChanged(ItemEvent e)
  {
    if (e.getSource() == choice1)
    {
      area1.setText("choice1: " + choice1.getSelectedItem());
    }
    if (e.getSource() == check1)
    {
      area1.setText("check1: " + check1.getState());
    }
  }


  public void init()
  {
     this.add(label1);
     this.add(button1);
     this.add(check1);
     this.add(check2);
     this.add(check3);
     this.add(choice1);
     choice1.add("en rad");
     choice1.add("en till rad");
     choice1.add("en tredje rad");
     this.add(list1);
     list1.add("en rad");
     list1.add("en till rad");
     list1.add("en tredje rad");
     this.add(text1);
     this.add(area1);
     this.add(area1);
     choice1.addItemListener( this );
     check1.addItemListener( this );
  }
}

Resultat:

Händelser för TextField och TextArea

import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class Applet1 extends Applet implements TextListener
{
  Label label1 = new Label("text för labeln");
  Button button1 = new Button("text för knappen");
  Checkbox check1 = new Checkbox("en checkbox");
  CheckboxGroup grp = new CheckboxGroup();
  Checkbox check2 = new Checkbox("ett",grp,false);
  Checkbox check3 = new Checkbox("två",grp,true);
  Choice choice1 = new Choice();
  List list1 = new List();
  TextField text1 = new TextField("skriv här");
  TextArea area1 = new TextArea("text", 10,30);

  public void textValueChanged(TextEvent e)
  {
    if (e.getSource() == text1)
    {
      area1.setText("text1: " + text1.getText());
    }
  }


  public void init()
  {
     this.add(label1);
     this.add(button1);
     this.add(check1);
     this.add(check2);
     this.add(check3);
     this.add(choice1);
     choice1.add("en rad");
     choice1.add("en till rad");
     choice1.add("en tredje rad");
     this.add(list1);
     list1.add("en rad");
     list1.add("en till rad");
     list1.add("en tredje rad");
     this.add(text1);
     this.add(area1);
     this.add(area1);
     text1.addTextListener( this );
  }
}

Resultat:

Flera olika händelser i samma program

import java.awt.*;
import java.applet.*;
import java.awt.event.*;


public class Applet1 extends Applet implements ActionListener, ItemListener, TextListener
{
  Label label1 = new Label("text för labeln");
  Button button1 = new Button("text för knappen");
  Checkbox check1 = new Checkbox("en checkbox");
  CheckboxGroup grp = new CheckboxGroup();
  Checkbox check2 = new Checkbox("ett",grp,false);
  Checkbox check3 = new Checkbox("två",grp,true);
  Choice choice1 = new Choice();
  List list1 = new List();
  TextField text1 = new TextField("skriv här");
  TextArea area1 = new TextArea("text", 10,30);

  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == button1)
    {
      // gör något
      label1.setText("HEPP");
    }
  }

  public void itemStateChanged(ItemEvent e)
  {
    if (e.getSource() == choice1)
    {
      area1.setText("choice1: " + choice1.getSelectedItem());
    }
    if (e.getSource() == check1)
    {
      area1.setText("check1: " + check1.getState());
    }
  }


  public void textValueChanged(TextEvent e)
  {
    if (e.getSource() == text1)
    {
      area1.setText("text1: " + text1.getText());
    }
  }


  public void init()
  {
     this.add(label1);
     this.add(button1);
     this.add(check1);
     this.add(check2);
     this.add(check3);
     this.add(choice1);
     choice1.add("en rad");
     choice1.add("en till rad");
     choice1.add("en tredje rad");
     this.add(list1);
     list1.add("en rad");
     list1.add("en till rad");
     list1.add("en tredje rad");
     this.add(text1);
     this.add(area1);
     this.add(area1);

     button1.addActionListener( this );
     choice1.addItemListener( this );
     check1.addItemListener( this );
     text1.addTextListener( this );

  }
}

Resultat:

Glömde du inte Scrollbar?

Jag överlåter åt er att själva lista ut hur man fångar upp "scrollhändelser" för en scrollbar. Det är en variant på samma tema, och allt finns beskrivet i hjälpen.

Värdena på de olika kontrollerna

I senare skede måste vi säkert kunna både kontrollera och sätta värdena för de olika komponenterna. Nedan har jag satt ut de vanligaste metoderna för kontrollerna

KlassSätta värdeFå värdeDatatyp
Labellabel.setText(texten)label.getText()String
Buttonbutton.setText(texten)button.getText()String
Checkboxcheckbox.setState(boolean)checkbox.getState()boolean
Choicechoice.select(int eller String)checkbox.getSelectedItem()String
ListSom Choice
Choicechoice.select(int eller String)checkbox.getSelectedItem()String
TextFieldtext.setText(texten)text.getText()String
TextAreatext.setText(texten)text.getText()String
Scrollbarscroll.setValue(int)scroll.getValue()int
  Introduktion av awtLayout
Grunderna
Första programmet
Utgångspunkten
Introduktion av awt
Händelser
Layout


Top
< Introduktion av awt Layout>
© Anders Enges, Vörå 2002  | 31.01.01 11:55 Visa asp koden