Java:如何从主类调用GUI类(没有Swing / JFrame)

时间:2017-06-25 12:33:59

标签: java javafx

我是Java新手。我想建立一个餐馆应用程序。在主类中,我想要做的是从其他类调用GUI(以及后来的方法,但这不是问题)。 这是主类的代码(评论是德语):

package Restaurant;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Tisch_Auswahl extends Application {
  // Zutaten für die erste (primary) stage / Auswahlseite
  GridPane pane0 = new GridPane();
  Scene scene0 = new Scene(pane0);

  Button button1 = new Button("Tisch 1");
  Button button2 = new Button("Tisch 2");
  Button button3 = new Button("Tisch 3");



  EventHandler<ActionEvent> Tisch1 = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
  Tisch1.show(); // Zweite Bühne wird angezeigt
  // In diesen EventHandler muss eigentlich Befehl für Abruf der Klasse?
}
  };

  EventHandler<ActionEvent> Tisch2 = new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
      Tisch2.show(); // Zweite Bühne wird angezeigt
      // In diesen EventHandler muss eigentlich Befehl für Abruf der Klasse?
    }
      };

  EventHandler<ActionEvent> Tisch3 = new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
          Tisch3.show(); // Zweite Bühne wird angezeigt
          // In diesen EventHandler muss eigentlich Befehl für Abruf der Klasse?
        }
      };



  public void start(Stage primaryStage) throws Exception {
// Erste stage vorbereiten
primaryStage.setScene(scene0);
primaryStage.setTitle("Bestellsystem");

pane0.setPadding(new Insets(10));
pane0.setHgap(20);
pane0.setVgap(20);

pane0.addRow(0, button1, button2, button3);




button1.setOnAction(Tisch1);
button2.setOnAction(Tisch2);
button3.setOnAction(Tisch3);

primaryStage.show();

  }

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

在主类中有三个用于“tisch 1 - 3”的EventHandler(表示表1 - 3)。我为每个表创建了一个类,每个类都是一样的。这是表1的类的代码:

package Restaurant;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
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.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;


public class Tisch1 extends Application {
  GridPane pane1 = new GridPane(); 
  Scene szene_1 = new Scene(pane1, 600, 650);

  Label l0 = new Label("Menü");
Label l1 = new Label("Vorspeise");
Label l2 = new Label("Hauptspeise");
Label l3 = new Label("Dessert");
Label l4 = new Label("Getränke");

Label l5 = new Label("Suppe");
Label l6 = new Label("Bruschetta");
Label l7 = new Label("Dip");

Label l8 = new Label("Pide");
Label l9 = new Label("Pizza");
Label l10 = new Label("Pasta");

Label l11 = new Label("Rote Grütze");
Label l12 = new Label("Tiramisu");
Label l13 = new Label("Eis");

Label l14 = new Label("Bier");
Label l15 = new Label("Wasser");
Label l16 = new Label("Wein");

ObservableList<String> Menge1 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge1 = new ComboBox<>(Menge1);

ObservableList<String> Menge2 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge2 = new ComboBox<>(Menge2);

ObservableList<String> Menge3 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge3 = new ComboBox<>(Menge3);

ObservableList<String> Menge4 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge4 = new ComboBox<>(Menge4);

ObservableList<String> Menge5 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge5 = new ComboBox<>(Menge5);

ObservableList<String> Menge6 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge6 = new ComboBox<>(Menge6);

ObservableList<String> Menge7 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge7 = new ComboBox<>(Menge7);

ObservableList<String> Menge8 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge8 = new ComboBox<>(Menge8);

ObservableList<String> Menge9 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge9 = new ComboBox<>(Menge9);

ObservableList<String> Menge10 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge10 = new ComboBox<>(Menge10);

ObservableList<String> Menge11 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge11 = new ComboBox<>(Menge11);

ObservableList<String> Menge12 = FXCollections.observableArrayList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10");
ComboBox<String> menge12 = new ComboBox<>(Menge12);

Button b1 = new Button("Nächste Bestellung");
Button b2 = new Button("Rechnung drucken");


  public void start (Stage Tisch1) {
  pane1.add(l1, 0, 1);
  pane1.add(l2, 0, 5);
  pane1.add(l3, 0, 9);
  pane1.add(l4, 0, 13);

  pane1.add(l5, 1, 1);
  pane1.add(l6, 1, 2);
  pane1.add(l7, 1, 3);

  pane1.add(l8, 1, 5);
    pane1.add(l9, 1, 6);
    pane1.add(l10, 1, 7);

    pane1.add(l11, 1, 9);
    pane1.add(l12, 1, 10);
    pane1.add(l13, 1, 11);

    pane1.add(l14, 1, 13);
    pane1.add(l15, 1, 14);
    pane1.add(l16, 1, 15);

    pane1.add(menge1, 2, 1);
    pane1.add(menge2, 2, 2);
    pane1.add(menge3, 2, 3);
    pane1.add(menge4, 2, 5);
    pane1.add(menge5, 2, 6);
    pane1.add(menge6, 2, 7);
    pane1.add(menge7, 2, 9);
    pane1.add(menge8, 2, 10);
    pane1.add(menge9, 2, 11);
    pane1.add(menge10, 2, 13);
    pane1.add(menge11, 2, 14);
    pane1.add(menge12, 2, 15);

    pane1.add(b1, 2, 17);
    pane1.add(b2, 3, 17);

    menge1.getSelectionModel().select("0");
    menge2.getSelectionModel().select("0");
    menge3.getSelectionModel().select("0");
    menge4.getSelectionModel().select("0");
    menge5.getSelectionModel().select("0");
    menge6.getSelectionModel().select("0");
    menge7.getSelectionModel().select("0");
    menge8.getSelectionModel().select("0");
    menge9.getSelectionModel().select("0");
    menge10.getSelectionModel().select("0");
    menge11.getSelectionModel().select("0");
    menge12.getSelectionModel().select("0");

    pane1.setPadding(new Insets(10));
    pane1.setHgap(12);
    pane1.setVgap(12);

    EventHandler<ActionEvent> Nächste_Bestellung = new EventHandler<ActionEvent>() {
          public void handle(ActionEvent event) {
              Tisch1.close();

          }
        };

        EventHandler<ActionEvent> Rechnung_erstellen = new EventHandler<ActionEvent>() {
              public void handle(ActionEvent event) {

              }
            };

        b1.setOnAction(Nächste_Bestellung);
        b2.setOnAction(Rechnung_erstellen);

Tisch1.setScene(szene_1);
Tisch1.setTitle("Tisch 1");
Tisch1.show();
  }

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

那么如何在我的主类中为表1调用GUI? 我使用Swing找到了答案,但我不想混淆它。 请帮助我:)

1 个答案:

答案 0 :(得分:0)

代码中使用GUI的典型操作是:

  1. 创建并显示应用程序窗口(根容器)
  2. 撰写GUI组件并添加到某个容器
  3. 订阅/取消订阅GUI活动
  4. 更改GUI组件的状态
  5. 第一个通常在主应用程序类中完成,可能是包含main方法的类。 对于其他人来说,遵循MVC模式是个好主意,其中V代表View(JavaFX或Swing类的实例,负责呈现和接收用户输入),M是您的数据模型(例如菜单列表)餐厅中的项目,C是控制器类,负责组成视图,订阅和处理视图事件以及更新模型。所以你的代码看起来像这样(在伪代码中,因为我不熟悉javafx API):

    interface Controller {
         Node getView();
    }
    
    class TableController {
       ...
    }
    
    class MainScreenController implements Controller, ReservationListener {
        private Restaurant model;
        private StackPane view;
        private List<TableController> children;
        public MainScreenController(Restaurant restaurant) {
            view = new StackPane();
            // compose this view from table views for each table
            for (Table table : model.getTables()) {
                 TableController controller = new TableController(table);
                 view.getChildren().add(controller.getView());
                 // when Reserve button is clicked in TableController, we want
                 // to receive reservation events here and update restaurant
                 // status
                 controller.addReservationListener(this);
            }
            // add more components here and subscriptions on events
        }
        // receives reservation events from tables
        public void tableReserved(Table table) { 
            ...
        }
    }
    
    class MyApplication extends Application {
       // a model that could be loaded from file or database
       private Restaurant restaurant = new Restaurant(); 
       // a controller for the main view
       private RestaurantController controller = new RestaurantController(restaurant);
       public void start(Stage stage) {
          // here you just initialize the stage with the view
          // from your main controller
          stage.setScene(new Scene(main.getView(), 400, 300));
          // you only need to show the root, the rest is already part of it
          stage.show(); 
       } 
    }
    

    `