jeudi 17 novembre 2016

CheckBox issues in JavaFX

I am working on an assignment and I am using check boxes to convert my text from normal to bold, italics or both. However when I run my code, I get some errors saying my checkbox has no constructor that accepts strings when I checked on Oracle and there is a constructor that accepts strings, and check boxes cannot be converted to a nodes when I try to add them into my gridpanes when I think check boxes inherits the node properties naively. Here is my code. Thanks for any possible help.

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.geometry.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import java.util.*;
import javafx.scene.text.*;
import javafx.scene.control.*;
import java.lang.*;
import javafx.beans.binding.*;
import javafx.event.*;
//import javafx.scene.control.CheckBox;
//import javafx.scene.control.Button;

public class test extends Application{
  protected Text name = new Text(50, 50, "Hello World");

  protected BorderPane getPane() {
    BorderPane masterPane = new BorderPane();

    Pane textPane = new Pane();
    textPane.getChildren().add(name);
    //masterPane.setCenter(textPane);

    GridPane gridPane = new GridPane();
    gridPane.setPadding(new Insets(10));
    gridPane.setHgap(5);
    gridPane.setVgap(5);
    gridPane.add(textPane,0,0);
    GridPane.setHalignment(textPane, HPos.CENTER);
    GridPane.setValignment(textPane, VPos.CENTER);
    masterPane.setCenter(gridPane);

    // HBox buttonPane = new HBox(1000);
    Button upBtn = new Button("Up");
    Button downBtn = new Button("Down");
    GridPane buttonGridPane = new GridPane();
    buttonGridPane.add(upBtn,0,0);
    buttonGridPane.add(downBtn,1,0);
    buttonGridPane.setPadding(new Insets(10));
    buttonGridPane.setHgap(5);
    buttonGridPane.setVgap(5);
    buttonGridPane.setHalignment(upBtn, HPos.CENTER);
    buttonGridPane.setValignment(upBtn, VPos.CENTER);
    buttonGridPane.setHalignment(downBtn, HPos.CENTER);
    buttonGridPane.setValignment(downBtn, VPos.CENTER);
    // buttonPane.getChildren().addAll(upBtn, downBtn);
    // buttonPane.setAlignment(Pos.CENTER);
    buttonGridPane.setStyle("-fx-border-color: cyan");
    masterPane.setBottom(buttonGridPane);

    upBtn.setOnAction(e -> name.setY(name.getY() - 10));
    downBtn.setOnAction(e -> name.setY(name.getY() + 10));

    return masterPane;
  }

  protected StackPane setPane(){

    BorderPane newMasterPane = new BorderPane(getPane());
    GridPane gridPane = new GridPane();
    gridPane.setMinSize(1250, 500);
    gridPane.setMaxSize(1250, 500);
    StackPane root = new StackPane(gridPane);
    NumberBinding maxScale = Bindings.min(root.widthProperty().divide(1250), root.heightProperty().divide(500));
    gridPane.scaleXProperty().bind(maxScale);
    gridPane.scaleYProperty().bind(maxScale);
    gridPane.add(newMasterPane,0,0);

    return root;
  }

  @Override
   public void start(Stage primoStage) {

     Scene scene = new Scene(setPane(), 1250, 500);
     primoStage.setTitle("Assignment #7");
     primoStage.setScene(scene);
     primoStage.show();

   }

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

class CheckBox extends test {
  @Override
  protected BorderPane getPane() {
    BorderPane pane = super.getPane();

    Font fontBoldItalic = Font.font("Comic Sans MS",
      FontWeight.BOLD, FontPosture.ITALIC, 20);
    Font fontBold = Font.font("Comic Sans MS",
      FontWeight.BOLD, FontPosture.REGULAR, 20);
    Font fontItalic = Font.font("Comic Sans MS",
      FontWeight.NORMAL, FontPosture.ITALIC, 20);
    Font fontNormal = Font.font("Comic Sans MS",
      FontWeight.NORMAL, FontPosture.REGULAR, 20);

      name.setFont(fontNormal);

      GridPane fontGridPane = new GridPane();
      fontGridPane.setPadding(new Insets(10));
      fontGridPane.setHgap(5);
      fontGridPane.setVgap(5);
      fontGridPane.setStyle("-fx-border-color: cyan");

      // VBox checkPane = new VBox(20);
      // checkPane.setPadding(new Insets(5, 5, 5, 5));
      // checkPane.setStyle("-fx-border-color: cyan");
      CheckBox chkBold = new CheckBox("Bold");
      CheckBox chkItalic = new CheckBox("Italic");
      // checkPane.getChildren().addAll(chkBold, chkItalic);
      // pane.setLeft(checkPane);
      fontGridPane.add(chkBold,0,0);
      fontGridPane.add(chkItalic,0,1);
      fontGridPane.setHalignment(chkBold, HPos.CENTER);
      fontGridPane.setValignment(chkBold, VPos.CENTER);
      fontGridPane.setHalignment(chkItalic, HPos.CENTER);
      fontGridPane.setValignment(chkItalic, VPos.CENTER);

      EventHandler<ActionEvent> handler = e -> {
        if (chkBold.isSelected() && chkItalic.isSelected()) {
          text.setFont(fontBoldItalic); // Both check boxes checked
        }
        else if (chkBold.isSelected()) {
          text.setFont(fontBold); // The Bold check box checked
        }
        else if (chkItalic.isSelected()) {
          text.setFont(fontItalic); // The Italic check box checked
        }
        else {
          text.setFont(fontNormal); // Both check boxes unchecked
        }
      };

      chkBold.setOnAction(handler);
      chkItalic.setOnAction(handler);

      return pane;
  }
}




Aucun commentaire:

Enregistrer un commentaire