我是 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 找到了答案,但我不想将其混淆。请帮助我:)
我是一名优秀的程序员,十分优秀!