javafx gui 示例_如何构建简单的GUI应用程序(带有示例JavaFX代码)

曾经终败给现在 2022-12-07 12:12 321阅读 0赞

javafx gui 示例

背景 ( Background )

This code uses a BorderPane as a container for two FlowPanes and a Button. The first FlowPane contains a Label and ChoiceBox, the second FlowPane a Label and a ListView. The Button switches the visibility of each FlowPane.

此代码将BorderPane用作两个FlowPanes和一个Button的容器。 第一个FlowPane包含LabelChoiceBox ,第二个FlowPane包含LabelListView 。 该Button切换每个FlowPane的可见性。

JavaFX代码 ( JavaFX Code )

Picture of a woman typing on a keyboard

© Stepan Popov / E+ / Getty Images ©Stepan Popov / E + /盖蒂图片社

  1. //Imports are listed in full to show what's being used
  2. //could just import javafx.*
  3. import javafx.application.Application;
  4. import javafx.collections.FXCollections;
  5. import javafx.event.ActionEvent;
  6. import javafx.event.EventHandler;
  7. import javafx.geometry.Insets;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.ChoiceBox;
  11. import javafx.scene.control.Label;
  12. import javafx.scene.control.ListView;
  13. import javafx.scene.layout.BorderPane;
  14. import javafx.scene.layout.FlowPane;
  15. import javafx.stage.Stage;
  16. public class ApplicationWindow extends Application {
  17. //JavaFX applicatoin still use the main method.
  18. //It should only ever contain the call to the launch method
  19. public static void main(String[] args) {
  20. launch(args);
  21. }
  22. //starting point for the application
  23. //this is where we put the code for the user interface
  24. @Override
  25. public void start(Stage primaryStage) {
  26. //The primaryStage is the top-level container
  27. primaryStage.setTitle("example Gui");
  28. //The BorderPane has the same areas laid out as the
  29. //BorderLayout layout manager
  30. BorderPane componentLayout = new BorderPane();
  31. componentLayout.setPadding(new Insets(20,0,20,20));
  32. //The FlowPane is a conatiner that uses a flow layout
  33. final FlowPane choicePane = new FlowPane();
  34. choicePane.setHgap(100);
  35. Label choiceLbl = new Label("Fruits");
  36. //The choicebox is populated from an observableArrayList
  37. ChoiceBox fruits = new ChoiceBox(FXCollections.observableArrayList("Asparagus", "Beans", "Broccoli", "Cabbage"
  38. , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom"
  39. , "Pepper", "Radish", "Shallot", "Spinach", "Swede"
  40. , "Turnip"));
  41. //Add the label and choicebox to the flowpane
  42. choicePane.getChildren().add(choiceLbl);
  43. choicePane.getChildren().add(fruits);
  44. //put the flowpane in the top area of the BorderPane
  45. componentLayout.setTop(choicePane);
  46. final FlowPane listPane = new FlowPane();
  47. listPane.setHgap(100);
  48. Label listLbl = new Label("Vegetables");
  49. ListView vegetables = new ListView(FXCollections.observableArrayList("Apple", "Apricot", "Banana"
  50. ,"Cherry", "Date", "Kiwi", "Orange", "Pear", "Strawberry"));
  51. listPane.getChildren().add(listLbl);
  52. listPane.getChildren().add(vegetables);
  53. listPane.setVisible(false);
  54. componentLayout.setCenter(listPane);
  55. //The button uses an inner class to handle the button click event
  56. Button vegFruitBut = new Button("Fruit or Veg");
  57. vegFruitBut.setOnAction(new EventHandler() {
  58. @Override
  59. public void handle(ActionEvent event) {
  60. //switch the visibility for each FlowPane
  61. choicePane.setVisible(!choicePane.isVisible());
  62. listPane.setVisible(!listPane.isVisible());
  63. }
  64. });
  65. componentLayout.setBottom(vegFruitBut);
  66. //Add the BorderPane to the Scene
  67. Scene appScene = new Scene(componentLayout,500,500);
  68. //Add the Scene to the Stage
  69. primaryStage.setScene(appScene);
  70. primaryStage.show();
  71. }
  72. }

翻译自: https://www.thoughtco.com/how-to-build-a-simple-gui-application-javafx-code-2034067

javafx gui 示例

发表评论

表情:
评论列表 (有 0 条评论,321人围观)

还没有评论,来说两句吧...

相关阅读