Text Version: import javafx.event.ActionEvent; import javafx.event.EventHandler;
ID: 3847506 • Letter: T
Question
Text Version:
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
public class CheckBoxDemo extends ButtonDemo {
@Override // Override the getPane() method in the super class
protected BorderPane getPane() {
BorderPane pane = super.getPane();
Font fontBoldItalic = Font.font("Times New Roman",
FontWeight.BOLD, FontPosture.ITALIC, 20);
Font fontBold = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 20);
Font fontItalic = Font.font("Times New Roman", FontWeight.NORMAL, FontPosture.ITALIC, 20);
Font fontNormal = Font.font("Times New Roman", FontWeight.NORMAL, FontPosture.REGULAR, 20);
text.setFont(fontNormal);
VBox paneForCheckBoxes = new VBox(20); paneForCheckBoxes.setPadding(new Insets(5, 5, 5, 5)); paneForCheckBoxes.setStyle("-fx-border-color: green"); CheckBox chkBold = new CheckBox("Bold");
CheckBox chkItalic = new CheckBox("Italic"); paneForCheckBoxes.getChildren().addAll(chkBold, chkItalic); pane.setRight(paneForCheckBoxes);
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; // Return a new pane
}
}
Run CheckBoxDemo, p. 635. Note that since it extends ButtonDemo it inherits the window title "Button Demo." As described in the paragraph above the listing, an alternative approach to this would be to simply revise the ButtonQemo class and provide the title "Check Box Demo." Copy and paste your revised CheckBoxDemo here. There are at least two approaches to writing this program The first is to revise the preceding ButtonDeno class to insert the code for adding the check boxes and processing their events. The second is to define a subclass that extends ButtonDemo. Please implement the first approach as an exercise. Listing 16.3 gives the code to implement the second approach. LISTING 16.3 CheckBoxDemo.java import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geonetry.Insets; import javafx.scene.control.CheckBox; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; public class CheckBoxDeno extends ButtonOeno { @Override//Override the getPane() method in the super class protected BorderPane getPane() { BorderPane Pane = super. getPane(); Font fontBoldltalic = Font.font("Times New Rowan", FontWeight.BOLD, FontPosture.ITALIC, 20); Font fontBold = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 20); Font fontItalic = Font.font("Times New Roman", FontWeight.NORMAL. FontPosture.ITALIC, 20); Font fontNormal = Font.font("Times New Roman", FontWeight.NORMAL. FontPosture.REGULAR. 20); text.setFont(fontNormal); VBox paneForCheckBoxes = new VBox(20); paneForCheckBoxes.setPadding(new Insets(5, 5, 5, 5)); paneForCheckBoxes.setStyle("-fx-border-color: green"); CheckBox chkBold = new CheckBox("Bold"); CheckBox chkltalic = new CheckBox("Italic"); paneForCheckBoxes.getChi1dren().addAll(chkBold, chkltalic); pane.setRight(paneForCheckBoxes); EventHandler handler = e - > { if (chkBold.isSelected() && chkltalic.isSelected()) { text.setFont(fontBoldltalic);//Both check boxes checkec } else if (chkBold.IsSelected()) { text.setFont(fontBold);//The Bold check box checked } else if (chkltalic.isSelected()) { text.setFont(fontltalic);//The Italic check box checkec } else { text.setFont(fontNormal);//Both check boxes unchecked } }; chkBold.setOnAction(handler); chkltalic.set0nAction(handler); return pane;//Return a new pane } }Explanation / Answer
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
* Chapter 13 Exercise :
* <p>
* Created by Luiz Arantes Sa on 9/12/14.
*/
public class ButtonDemo extends Application {
protected Text text = new Text(50, 50, "JavaFX Programming");
protected BorderPane getPane() {
HBox paneForButtons = new HBox(20);
Button btLeft = new Button("Left",
new ImageView("image/left.gif"));
Button btRight = new Button("Right",
new ImageView("image/right.gif"));
paneForButtons.getChildren().addAll(btLeft, btRight);
paneForButtons.setAlignment(Pos.CENTER);
paneForButtons.setStyle("-fx-border-color: green");
BorderPane pane = new BorderPane();
pane.setBottom(paneForButtons);
Pane paneForText = new Pane();
paneForText.getChildren().add(text);
pane.setCenter(paneForText);
btLeft.setOnAction(e -> text.setX(text.getX() - 10));
btRight.setOnAction(e -> text.setX(text.getX() + 10));
return pane;
}
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a scene and place it in the stage
Scene scene = new Scene(getPane(), 450, 200);
primaryStage.setTitle("ButtonDemo"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.