关于GUI开发的代码布局

时间:2014-01-18 14:42:33

标签: java layout user-interface javafx

我正在学习如何使用JavaFX创建图形界面 比Swing更强大,更容易手动编码而不必诉诸 GUIBuilder。 虽然这也包括Swing我到目前为止已阅读了不少教程,我总是看到 所有代码都是用main()或start()方法编写的。

例如来自java.about.com的代码示例:

//Imports are listed in full to show what's being used
//could just import javafx.*
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
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.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,0,20,20));

        //The FlowPane is a conatiner that uses a flow layout
        final FlowPane choicePane = new FlowPane();
        choicePane.setHgap(100);
        Label choiceLbl = new Label("Fruits");

        //The choicebox is populated from an observableArrayList
        ChoiceBox fruits = new ChoiceBox(FXCollections.observableArrayList("Asparagus", "Beans", "Broccoli", "Cabbage"
         , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom"
         , "Pepper", "Radish", "Shallot", "Spinach", "Swede"
         , "Turnip"));

        //Add the label and choicebox to the flowpane
        choicePane.getChildren().add(choiceLbl);
        choicePane.getChildren().add(fruits);

        //put the flowpane in the top area of the BorderPane
        componentLayout.setTop(choicePane);

        final FlowPane listPane = new FlowPane();
        listPane.setHgap(100);
        Label listLbl = new Label("Vegetables");

        ListView vegetables = new ListView(FXCollections.observableArrayList("Apple", "Apricot", "Banana"
         ,"Cherry", "Date", "Kiwi", "Orange", "Pear", "Strawberry"));
        listPane.getChildren().add(listLbl);
        listPane.getChildren().add(vegetables);
        listPane.setVisible(false);

        componentLayout.setCenter(listPane);

        //The button uses an inner class to handle the button click event
        Button vegFruitBut = new Button("Fruit or Veg");
        vegFruitBut.setOnAction(new EventHandler() {

            @Override
            public void handle(ActionEvent event) {
                //switch the visibility for each FlowPane
                choicePane.setVisible(!choicePane.isVisible());
                listPane.setVisible(!listPane.isVisible());
            }
        });

        componentLayout.setBottom(vegFruitBut);

        //Add the BorderPane to the Scene
        Scene appScene = new Scene(componentLayout,500,500);

        //Add the Scene to the Stage
        primaryStage.setScene(appScene);
        primaryStage.show();
    }
}

但是编写main()或start()中的所有内容都违背了我在学校学到的原则 我也是一件坏事。 因为我总是习惯用不同的方法将所有内容分开,而不是通过调用方法来保持干净的main(),所以很容易在程序执行路径上读取。正如我看到所有教程等反对这一点,通过将他们的GUI代码放在main或启动方法中我开始想知道这是关于GUI代码的常规做法还是我仍然应该尝试将所有内容放在自定义方法下并保持干净的主要()?

还有人可以分享关于JavaFX的整洁教程吗?我似乎在这件事上找不到多少。

编辑:我真的应该问一个关于高级应用程序界面的类层次结构如何获得一些想法的好例子。

希望这是一个明确的问题, 提前谢谢。

碧玉。

1 个答案:

答案 0 :(得分:0)

您可以创建一个GUI类,将所有GUI代码保存在一起,或者只创建一个位于main下面的GUI方法。然后,在main中,您可以调用:

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

public void start()
{
  launchGUI(Stage primaryStage)
}

然后在同一个类中创建launchGUI方法,或者专门为GUI创建一个新类,并使用import将其导入包含main / {的类中{1}}。