2017-04-02 5 views
-1

Mein Programm soll ein Bild sichtbar machen, wenn man eine Taste drückt (rbutton1), und ich soll setVisible(true) verwenden, aber ich habe Probleme, den Ereignishandler für die rbutton1 Arbeit zu machen.Wie verwendet man die .setVisible() Methode?

Die Methode public static ArrayList<Piece> read(String fileName) liest die Binärdatei.

package project4; 

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.util.ArrayList; 
import javafx.application.Application; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.CheckBox; 
import javafx.scene.control.ComboBox; 
import javafx.scene.control.Label; 
import javafx.scene.control.ListView; 
import javafx.scene.control.RadioButton; 
import javafx.scene.control.SelectionMode; 
import javafx.scene.control.ToggleGroup; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.Pane; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 
import javafx.util.Duration; 


public class Project4 extends Application { 

    Body b; 
    Head head1, head2; 
    Hat hat1, hat2; 
    Weapon w1, w2, w3; 
    Companion c1, c2, c3; 

    @Override 
    public void start(Stage primaryStage) { 
     //fetch objects 
     ArrayList<Piece> pieces = read("pieces.dat"); 

     //parent pane, one row only, 
     //contains Pane int he left column for images, and a grid pane in the right for controls 
     GridPane root = new GridPane(); 

     //pane for images 
     Pane granny = new Pane(); 
     root.add(granny, 0, 0); 

     //load images 
     for (Piece p : pieces) { 
      //only the body starts visible 
      if (p instanceof Body) { 
       granny.getChildren().add(((Body) p).image); 
       b = (Body) p; 
      } //everything else is added in the right place but is made invisible 
      else if (p instanceof Head) { 
       granny.getChildren().add(((Head) p).image); 
       if (head1 == null) { 
        head1 = (Head) p; 
       } else { 
        head2 = (Head) p; 
       } 
      } else if (p instanceof Hat) { 
       granny.getChildren().add(((Hat) p).image); 
       if (hat1 == null) { 
        hat1 = (Hat) p; 
       } else { 
        hat2 = (Hat) p; 
       } 
      } else if (p instanceof Weapon) { 
       granny.getChildren().add(((Weapon) p).image); 
       if (w1 == null) { 
        w1 = (Weapon) p; 
       } else if (w2 == null) { 
        w2 = (Weapon) p; 
       } else { 
        w3 = (Weapon) p; 
       } 
      } else if (p instanceof Companion) { 
       granny.getChildren().add(((Companion) p).image); 
       if (c1 == null) { 
        c1 = (Companion) p; 
       } else if (c2 == null) { 
        c2 = (Companion) p; 
       } else { 
        c3 = (Companion) p; 
       } 
      } 

     } 

     //controls pane 
     GridPane controls = new GridPane(); 
     controls.setAlignment(Pos.TOP_CENTER); 
     controls.setPadding(new Insets(10, 10, 10, 10)); 
     controls.setHgap(15); 
     controls.setVgap(15); 

     Label headLabel = new Label("Choose Head:"); 
     RadioButton rbutton1 = new RadioButton("Esme with bees"); 
     CheckBox check1 = new CheckBox("Face the other way"); 
     RadioButton rbutton2 = new RadioButton("Esme normal"); 
     CheckBox check2 = new CheckBox("Face the other way"); 
     Label hatLabel = new Label("Choose Hat:"); 
     RadioButton rbutton3 = new RadioButton("Disreputable hat"); 
     RadioButton rbutton4 = new RadioButton("Hat with pins"); 
     RadioButton rbutton5 = new RadioButton("No hat"); 
     Label weaponLabel = new Label("Weapon:"); 
     ComboBox< String> cbox = new ComboBox<>(); 
     cbox.getItems().addAll(
       "broom", 
       "scythe", 
       "stick" 
     ); 
     Label companionLabel = new Label("Companion:"); 
     ListView<String> clv = new ListView<>(); 
     clv.getItems().addAll("DEATH of Rats", "Gaspode", "Luggage"); 
     clv.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); 
     clv.setPrefSize(2, 90); 

     Button exportButton = new Button("Export Character"); 
     Label exportFileName = new Label("export.oscar.dat"); 

     ToggleGroup group = new ToggleGroup(); 
     rbutton1.setToggleGroup(group); 
     rbutton2.setToggleGroup(group); 

     ToggleGroup group2 = new ToggleGroup(); 
     rbutton3.setToggleGroup(group2); 
     rbutton4.setToggleGroup(group2); 
     rbutton5.setToggleGroup(group2); 

     controls.add(headLabel, 1, 5); 
     controls.add(rbutton1, 2, 6); 
     controls.add(check1, 3, 6); 
     controls.add(rbutton2, 2, 7); 
     controls.add(check2, 3, 7); 
     controls.add(hatLabel, 1, 8); 
     controls.add(rbutton3, 2, 9); 
     controls.add(rbutton4, 2, 10); 
     controls.add(rbutton5, 2, 11); 
     controls.add(weaponLabel, 1, 13); 
     controls.add(cbox, 2, 14); 
     controls.add(companionLabel, 1, 15); 
     controls.add(clv, 2, 16); 
     controls.add(exportButton, 2, 17); 
     controls.add(exportFileName, 3, 17); 





     rbutton1.setOnAction(new EventHandler<ActionEvent>() { 


      @Override 
      public void handle(ActionEvent event) { 





      } 

     }); 





     root.add(controls, 1, 0); 

     Scene scene = new Scene(root, 900, 700); 

     primaryStage.setScene(scene); 

     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

    /* read the file - Binary I/O 
    * input - String fileName 
    * returns ArrayList<Piece> of objects 
    */ 
    public static ArrayList<Piece> read(String fileName) { 
     ArrayList<Piece> pieces = new ArrayList<>(); 
     //try with resources 
     try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName))) {; 
      //flag should keep reading 
      boolean go = true; 
      //if the flag still gives the go ahead 
      while(go) { 
       //try to read 
       try{ 
        //read, set the image dimensions and position, add to arraylist 
        Piece p = (Piece)in.readObject(); 
        p.setImage(); 
        pieces.add(p); 
       //if there is no more left to read, an IO exception will be thrown 
       } catch(IOException ex) { 
        //set the flag to signal end of file 
        go = false; 
       } 
      } 
     //neede dexception catches for this type of stream 
     } catch(IOException ioex) { 
      System.out.println("Input file is missing. " + ioex.getMessage()); 
      System.exit(-1); 
     } catch (ClassNotFoundException cnfex) { 
      System.out.println("Incompatible class. "); 
      System.exit(-1); 
     } 

     //return the list with objects from the file 
     return pieces; 
    } 

Antwort

0

Sie wollen nicht Handler auf die Schaltfläche setzen, stattdessen setzen sie auf Gruppe:

group1.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){ 
    @Override 
    public void changed(ObservableValue<? extends Toggle> ov, Toggle t, Toggle t1) 
     { 
     // Do what you want inside 

    }); 
Verwandte Themen