Linux - Friheden til at programmere i Java: Version 0.7.20040516 - 2020-12-31
forrige Kapitel 11. Grafiske brugergrænseflader næste

11.3. Eksempel

Herunder et eksempel (genereret med JBuilder), der viser komponenterne omtalt i forrige afsnit. På billedet ses det resulterende skærmbillede under Windows (sidst i kapitlet ses, hvordan det ser ud under Linux).

Figur 11-10. Java

{linuxbog:java:Nordfalk1215429.png}

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

public class GrafiskeKomponenter extends Applet
{ //// opret alle komponenterne og husk dem i nogle objektvariabler// Button button1 = new Button();
  Checkbox checkbox1 = new Checkbox();
  Checkbox checkbox2 = new Checkbox();
  Checkbox checkbox3 = new Checkbox();
  Checkbox checkbox4 = new Checkbox();
  Checkbox checkbox5 = new Checkbox();
  CheckboxGroup checkboxGroup1 = new CheckboxGroup();
  Choice choice1 = new Choice();
  TextField textField1 = new TextField();
  TextArea textArea1 = new TextArea();
  List list1 = new List();
  Label label1 = new Label(); //// JBuilder og JDeveloper definerer metoden jbInit() hvor de//  //// initialiserer komponenterne. Det kunne dog lige så godt//  //// ligge direkte i init()// public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  } //// initialisering af komponenterne med deres startværdier// private void jbInit() throws Exception {
    button1.setLabel("OK");

    checkbox1.setLabel("En"); //// Sæt afkrydsningsfelternes navne// checkbox2.setLabel("To");
    checkbox3.setLabel("Tre");

    checkbox4.setLabel("Radio1"); //// Sæt radioknappernes navne og// checkbox5.setLabel("Radio2");
    checkbox4.setCheckboxGroup(checkboxGroup1); //// gruppen de tilhører// checkbox5.setCheckboxGroup(checkboxGroup1);
    checkboxGroup1.setSelectedCheckbox(checkbox4);

    choice1.add("Choice Rød");
    choice1.add("Choice Grøn");
    choice1.add("Choice Blå");

    textField1.setColumns(10);
    textField1.setText("Et TextField");

    textArea1.setColumns(15);
    textArea1.setRows(5);
    textArea1.setText("Et TextArea");

    label1.setText("En Label");

    list1.add("List Rød");
    list1.add("List Grøn");
    list1.add("List Blå");

    this.add(button1, null); //// til sidst skal komponenterne føjes// this.add(checkbox1, null); //// til containeren (se senere)// this.add(checkbox2, null);
    this.add(checkbox3, null);
    this.add(checkbox4, null);
    this.add(checkbox5, null);
    this.add(choice1, null);
    this.add(textArea1, null);
    this.add(textField1, null);
    this.add(label1, null);
    this.add(list1, null);
  }
}

forrige hjem næste
Komponenter op Containere