Advertisement
iemadxi

Lab22

Apr 5th, 2021
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. import java.util.List;
  2. import javafx.application.Application;
  3. import javafx.geometry.Pos;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.CheckBox;
  6. import javafx.scene.control.ComboBox;
  7. import javafx.scene.control.Label;
  8. import javafx.scene.layout.BorderPane;
  9. import javafx.scene.layout.HBox;
  10. import javafx.scene.text.Font;
  11. import javafx.scene.text.FontPosture;
  12. import javafx.scene.text.FontWeight;
  13. import javafx.stage.Stage;
  14.  
  15.  
  16. // Lab 21
  17. public class main extends Application {
  18.  
  19. private Label label = new Label("Programming is fun");
  20.  
  21. private ComboBox<String> cboFontName = new ComboBox<>();
  22. private ComboBox<Integer> cboFontSize = new ComboBox<>();
  23. private CheckBox chkBold = new CheckBox("Bold");
  24. private CheckBox chkItalic = new CheckBox("Italic");
  25.  
  26.  
  27.  
  28. @Override
  29. public void start(Stage primaryStage) throws Exception {
  30.  
  31. BorderPane pane = new BorderPane();
  32. pane.setCenter(label);
  33.  
  34. List<String> fontNames = Font.getFontNames();
  35. cboFontName.getItems().addAll(fontNames);
  36. cboFontName.setValue(fontNames.get(6));
  37.  
  38. for (int i = 1; i <= 100; i++)
  39. cboFontSize.getItems().add(i);
  40.  
  41. cboFontSize.setValue(36);
  42. label.setFont(Font.font(cboFontName.getValue(), FontWeight.NORMAL, FontPosture.REGULAR,cboFontSize.getValue()));
  43.  
  44. HBox hBox = new HBox(10);
  45. hBox.getChildren().addAll(new Label("Font Name"),
  46. cboFontName, new Label("Font Size"), cboFontSize);
  47. hBox.setAlignment(Pos.CENTER);
  48. pane.setTop(hBox);
  49.  
  50. HBox hBox2 = new HBox(10);
  51. hBox2.getChildren().addAll(chkBold, chkItalic);
  52. hBox2.setAlignment(Pos.CENTER);
  53. pane.setBottom(hBox2);
  54.  
  55. Scene scene = new Scene(pane, 600, 300);
  56. primaryStage.setTitle("Lab 22");
  57. primaryStage.setScene(scene);
  58. primaryStage.show();
  59.  
  60. cboFontName.setOnAction(e -> {
  61. setFont();
  62. });
  63.  
  64. cboFontSize.setOnAction(e -> {
  65. setFont();
  66. });
  67.  
  68. chkBold.setOnAction(e -> {
  69. setFont();
  70. });
  71.  
  72. chkItalic.setOnAction(e -> {
  73. setFont();
  74. });
  75. }
  76.  
  77.  
  78. private void setFont() {
  79. FontWeight weight;
  80. if (chkBold.isSelected())
  81. weight = FontWeight.BOLD;
  82. else
  83. weight = FontWeight.NORMAL;
  84.  
  85. FontPosture posture;
  86. if (chkItalic.isSelected())
  87. posture = FontPosture.ITALIC;
  88. else
  89. posture = FontPosture.REGULAR;
  90.  
  91.  
  92. label.setFont(Font.font(cboFontName.getValue(), weight, posture,cboFontSize.getValue()));
  93.  
  94. }
  95.  
  96. public static void main(String[] args) {
  97. launch(args);
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement