gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:37 27 4
gpt4 key购买 nike

我是 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”(表示表 1 - 3)的三个 EventHandler。我为每个表创建了一个类,每个类都是相同的。这是表 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 找到了答案,但我不想将其混淆。请帮助我:)

最佳答案

代码中GUI的典型操作有:

  1. 创建并显示应用程序窗口(根容器)
  2. 编写一个 GUI 组件并添加到某个容器
  3. 订阅/取消订阅 GUI 事件
  4. 更改 GUI 组件的状态

第一个通常在主应用程序类中完成,可能是包含 main 方法的类。其余的最好遵循 MVC 模式,其中 V 代表 View(JavaFX 或 Swing 类的实例,负责渲染和接收用户输入),M 是数据模型(例如餐厅的菜单项列表),C 是 Controller 类,负责组成 View 、订阅和处理 View 事件以及更新模型。所以你的代码可能看起来像这样(伪代码,因为我不熟悉 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();
}
}

`

关于Java:如何从主类调用GUI类(没有Swing/JFrame),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44746413/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com