首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于GUI开发的代码布局

关于GUI开发的代码布局
EN

Stack Overflow用户
提问于 2014-01-18 14:42:32
回答 1查看 127关注 0票数 0

我目前正在学习如何使用JavaFX创建图形界面,因为我觉得它比Swing更强大,而且更容易手工编写代码,而不必求助于GUIBuilder。虽然这也包括Swing,但到目前为止,我已经阅读了相当多的教程,我总是看到所有的代码都是用main()或start()方法编写的。

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

代码语言:javascript
复制
//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()干净?

还有人能分享关于JavaFX的整洁教程吗?在这件事上我似乎找不到什么。

编辑:我应该问一个很好的例子,说明高级应用程序接口的类层次结构是如何获得一些想法的。

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

贾斯珀。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-18 14:48:21

您可以创建一个GUI类,它将所有GUI代码放在一起,或者简单地创建一个驻留在main下面的GUI方法。然后,大体上,您可以调用:

代码语言:javascript
复制
public static void main(String[] args)
{
  launch(args);
}

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21205565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档