JAVA_HA16.0: UI Controls Demo JavaFX provides many UI controls for developing a
ID: 3913163 • Letter: J
Question
JAVA_HA16.0: UI Controls Demo
JavaFX provides many UI controls for developing a comprehensive user interface. Write a program (UIControlsDemo.java) that uses a variety of JavaFX UI controls including button, checkbox, radio button, and text field. The code is combined and modified of the following source code files from the required textbook (Liang, 2015, 10th edition):
LISTING 16.2, ButtonDemo.java, pages 633-634
LISTING 16.3, CheckBoxDemo.java, pages 635-636
LISTING 16.4, RadioButton.java, pages 638-639
LISTING 16.5, TextFieldDemo.java, pages 639-640
Submit the source code file - UIControlsDemo.java - to the drop box HA16.0.
javafx.scene.contro1.LabeTed 16.3 Button 633 The getter and setter methods for property valucs and a getter for property itself are prov in the class, but omitted in thc UML diagram ObjectProperty ene.control.Button +ButtonO +Button(text: String) +Button(text: String, graphic: Node) Creates an empty button. Creates a button with the specified text. Creates a button with the specified text and graphic ButtonBase extends Labeled and defines common features for all buttons. sding 162 gives a program that uses the butons to control the movement of a text, as xwn in Figure 16.6 16.2 ButtonDemo.java iaport javafx.application.Application 2 inport javafx.stage. Stage import javafx.geometry. Po:s :inport javafx.scene.Scene: s import javafx.scene.control.Button; iaport javafx.scene. image. ImageView; ; inport javafx.scene.layout.BorderPane; isport javafx.scene.layout.HBox; g inport javafx.scene.layout.Pane; import javafx.scene. text.Text; Demo extends Application f l public class Button 3 protected Text text new Text (50, 50, "JavaFX Programming"); protected BorderPane getPane) [ 16 17 HBox paneForButtons = new HBox (20); Button btLeft = new Button("Left", new ImageView("image/left.gif")): 19 Button btRight new Button("Right". new ImageView("image/right.gif")): paneForButtons.getChildren ?.addA11(btLeft, btRight) paneForButtons.setAlignment (Pos. CENTER) paneForButtons.setStyle("-fx-border-color: green"): add buttons to pane BorderPane pane = new BorderPane(); create a border pane add buttons to the bottom l pane.setBottom(paneForButtons); Pane paneForText-new Pane O paneForText.getChildrenO.add(text): pane.setCenter(paneForText): add an action handler 4 tight.setOnAction(e>s text.setX(text.getXO +10)); 5 return pane; btleft. setonActionCe -> text.setx(text.getxO - 10));Explanation / Answer
here is your program : ------------->>>>>>>>>>>>
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.*;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.*;
import javafx.scene.control.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
import javafx.stage.Stage;
public class ApplicationWindow extends Application {
//JavaFX applicatoin still use the main method.
//It should only ever contain the call to the launch method
public static void main(String[] args) {
launch(args);
}
//starting point for the application
//this is where we put the code for the user interface
@Override
public void start(Stage primaryStage) {
//The primaryStage is the top-level container
primaryStage.setTitle("example Gui");
//The BorderPane has the same areas laid out as the
//BorderLayout layout manager
BorderPane componentLayout = new BorderPane();
componentLayout.setPadding(new Insets(20,20,20,20));
//The FlowPane is a conatiner that uses a flow layout
Font bifont = Font.font("TIMES NEW ROMAN",FontWeight.BOLD,FontPosture.ITALIC,20);
Font bfont = Font.font("TIMES NEW ROMAN",FontWeight.BOLD,FontPosture.REGULAR,20);
Font ifont = Font.font("TIMES NEW ROMAN",FontWeight.NORMAL,FontPosture.ITALIC,20);
Font nfont = Font.font("TIMES NEW ROMAN",FontWeight.NORMAL,FontPosture.REGULAR,20);
HBox new5 = new HBox(20);
FlowPane fnew = new FlowPane();
fnew.setAlignment(Pos.CENTER);
new5.setAlignment(Pos.CENTER);
new5.setStyle("-fx-border-color:green");
Text l2 = new Text(" ");
fnew.getChildren().add(l2);
new5.getChildren().add(fnew);
l2.setFont(nfont);
componentLayout.setCenter(new5);
HBox new4 = new HBox(20);
new4.setAlignment(Pos.CENTER);
new4.setStyle("-fx-border-color:green");
Label l1 = new Label("Enter a new Message : ");
l1.setAlignment(Pos.BASELINE_LEFT);
TextField ftmsg = new TextField(" ");
ftmsg.setOnAction(e->l2.setText(ftmsg.getText()));
ftmsg.setAlignment(Pos.BASELINE_RIGHT);
new4.getChildren().addAll(l1,ftmsg);
componentLayout.setTop(new4);
VBox new3 = new VBox(20);
new3.setStyle("-fx-border-width:2px; -fx-border-color:green");
new3.setAlignment(Pos.CENTER);
new3.setPadding(new Insets(5,5,5,5));
CheckBox bold= new CheckBox("Bold ");
CheckBox italic = new CheckBox("Italic");
EventHandler<ActionEvent> handler = e ->{
if(bold.isSelected() && italic.isSelected()){
l2.setFont(bifont);
}else if(bold.isSelected()){
l2.setFont(bfont);
}else if(italic.isSelected()){
l2.setFont(ifont);
}else{
l2.setFont(nfont);
}
};
bold.setOnAction(handler);
italic.setOnAction(handler);
new3.getChildren().addAll(bold,italic);
componentLayout.setRight(new3);
VBox new2 = new VBox(20);
new2.setStyle("-fx-border-width:2px; -fx-border-color:green");
new2.setAlignment(Pos.CENTER);
new2.setPadding(new Insets(5,5,5,5));
RadioButton red = new RadioButton("Red ");
RadioButton green = new RadioButton("Green");
RadioButton blue = new RadioButton("Blue ");
red.setOnAction(e->l2.setFill(Color.RED));
green.setOnAction(e->l2.setFill(Color.GREEN));
blue.setOnAction(e->l2.setFill(Color.BLUE));
new2.getChildren().add(red);
new2.getChildren().add(green);
new2.getChildren().add(blue);
ToggleGroup group = new ToggleGroup();
red.setToggleGroup(group);
green.setToggleGroup(group);
blue.setToggleGroup(group);
componentLayout.setLeft(new2);
HBox new1 = new HBox(20);
new1.setStyle("-fx-border-color:green");
new1.setAlignment(Pos.CENTER);
Button left = new Button("< left");
left.setOnAction(e->fnew.setAlignment(Pos.CENTER_LEFT));
Button right = new Button("right >");
right.setOnAction(e->fnew.setAlignment(Pos.CENTER_RIGHT));
new1.getChildren().add(left);
new1.getChildren().add(right);
componentLayout.setBottom(new1);
//componentLayout.setLeft(vegFruitBut);
//Add the BorderPane to the Scene
Scene appScene = new Scene(componentLayout,600,300);
//Add the Scene to the Stage
primaryStage.setScene(appScene);
primaryStage.show();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.