import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.scene.control.Button; import javafx.scene.paint.Color; import javafx.scene.layout.StackPane; import javafx.scene.shape.Rectangle; import javafx.scene.shape.Circle; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.layout.GridPane; import javafx.geometry.Pos; import javafx.event.*; import javafx.event.ActionEvent; public class HovedprogramJavafx extends Application { public void start(Stage teater){ GridPane rootPane = new GridPane(); MinKlasse handle = new MinKlasse(); for (int i = 0; i < 5; i++){ for (int j = 0; j < 5; j++){ Button knapp = new Button(); knapp.setOnAction(handle); knapp.setPrefSize(100,100); rootPane.add(knapp, i, j); } } Scene scene = new Scene(rootPane); teater.setScene(scene); teater.show(); } class MinKlasse implements EventHandler{ public void handle(ActionEvent e){ Button denne = (Button) e.getSource(); String tekst = denne.getText(); if (tekst == ""){ denne.setText("X"); } else { denne.setText(""); } } } public static void main(String[] args) { launch(); } } /* public class HovedprogramJavafx extends Application { String minTekst = "Du kan endre meg!"; Text endre; Button knapp1; Button knapp2; public void start(Stage teater){ VBox rootPane = new VBox(); Scene scene = new Scene(rootPane); StackPane paneTekst = new StackPane(); Text tekst = new Text("Hello!"); tekst.setFont(new Font(40)); paneTekst.getChildren().add(tekst); rootPane.getChildren().add(paneTekst); StackPane paneEndre = new StackPane(); endre = new Text(minTekst); endre.setFont(new Font(40)); paneEndre.getChildren().add(endre); rootPane.getChildren().add(paneEndre); knapp1 = new Button("Endre 1"); EndreTekst endreTekst = new EndreTekst(); knapp1.setOnAction(endreTekst); knapp2 = new Button("Endre 2"); knapp2.setOnAction(endreTekst); HBox paneH = new HBox(); paneH.getChildren().add(knapp1); paneH.getChildren().add(knapp2); paneH.setAlignment(Pos.CENTER); rootPane.getChildren().add(paneH); rootPane.setAlignment(Pos.CENTER); rootPane.setSpacing(50); teater.setWidth(400); teater.setHeight(400); teater.setTitle("Mitt program"); teater.setScene(scene); teater.show(); } class EndreTekst1 implements EventHandler { public void handle(ActionEvent e){ minTekst = "DU ENDRET MEG! :O"; endre.setText(minTekst); } } class EndreTekst2 implements EventHandler { public void handle(ActionEvent e){ minTekst = "HEHEHE"; endre.setText(minTekst); } } class EndreTekst implements EventHandler { public void handle(ActionEvent e){ Button denne = (Button) e.getSource(); if (denne == knapp1){ minTekst = "DU ENDRET MEG! :O"; endre.setText(minTekst); } if (denne == knapp2){ minTekst = "HEHEHE"; endre.setText(minTekst); } } } public static void main(String[] args) { launch(args); } } */