可以将两个不同的JavaFX场景作为两个独立的类编写吗?
我是一名初级Java程序员,在本地大学完成了“Java101”课程。我也在努力学习一些额外的话题,包括JavaFX。我在甲骨文网站上浏览了Java FX教程,还观看了一些YouTube视频,还读了《Java FX for Dummies》(这是我能为初学者找到的最好的书)。所有这些材料都教会了我很多基础知识,但一些(应该)相对简单的东西让我不知所措 例如:假设我有一个JavaFX程序,在一个阶段上使用多个场景。当用户单击“切换!”按钮时,第二个场景将替换为第一个场景。容易的。我可以在一个.java文件中完成所有这些,没问题。(见下面的代码) 但是,我的.java类文件变得非常长,很难解决问题。如果我能将一个场景定义/声明/初始化为一个.java文件中的一个类,将第二个场景定义为另一个.java文件中的另一个类,那就太好了。这将使跟踪每个场景的组件更加容易。问题是,我不知道怎么做 我可以想象,您将编写一个Scene1.java类,然后编写一个Scene2.java类,当您想要切换场景时,只需在这两个类之间传递stage对象。但我找不到这样做的例子,我所有的尝试都会导致编译器错误或非常可怕的运行时错误 有人知道如何做到这一点吗?如果是这样,我需要做什么来修改下面的可以将两个不同的JavaFX场景作为两个独立的类编写吗?,java,javafx,scene,stage,Java,Javafx,Scene,Stage,我是一名初级Java程序员,在本地大学完成了“Java101”课程。我也在努力学习一些额外的话题,包括JavaFX。我在甲骨文网站上浏览了Java FX教程,还观看了一些YouTube视频,还读了《Java FX for Dummies》(这是我能为初学者找到的最好的书)。所有这些材料都教会了我很多基础知识,但一些(应该)相对简单的东西让我不知所措 例如:假设我有一个JavaFX程序,在一个阶段上使用多个场景。当用户单击“切换!”按钮时,第二个场景将替换为第一个场景。容易的。我可以在一个.jav
SwitchScenes2()
方法来创建新的Scene2
对象并将其传递给stage
谢谢!饶
/*
JavaFXExample.java
*/
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.event.*;
import javafx.geometry.*;
public class JavaFXExample extends Application{
public static void main(String[] args){
launch(args);
}
Button btnSw1;
Button btnSw2;
Button btnClose;
HBox hbox1;
VBox vbox1;
Scene scene1;
Scene scene2;
Stage stage;
@Override public void start(Stage primaryStage){
btnSw1 = new Button("Switch Scenes!");
btnSw1.setOnAction(
e -> SwitchScenes2() );
btnSw2 = new Button("Switch back!");
btnSw2.setOnAction(
e -> SwitchScenes1() );
btnClose = new Button();
btnClose.setText("Close me!");
btnClose.setOnAction(e -> CloseWindowClick());
hbox1 = new HBox(10);
hbox1.getChildren().addAll(btnSw1);
vbox1 = new VBox(10);
vbox1.getChildren().addAll(btnSw2, btnClose);
scene1 = new Scene(hbox1, 300, 300);
scene2 = new Scene(vbox1, 200, 400);
stage = primaryStage;
stage.setScene(scene1);
stage.setTitle("Example App");
stage.show();
}
public void SwitchScenes1(){
stage.setScene(scene1);
}
public void SwitchScenes2(){
stage.setScene(scene2);
}
public void CloseWindowClick(){
stage.close();
}
}
您要做的是创建单独的类,这两个类都有返回场景的函数。从那里,您将需要初始化这些类,并使用按钮调用一个函数,该函数将向这些场景添加数据或创建一个新的空白场景(作为“删除”场景的快速方式)。但是,如果您想以更专业的方式在这样的场景之间切换,您需要查看TabPane()
private Parent content;
private static VMCSManager instance;
public VMCSManager() {
instance=this;
}
public static void main(String[] args) {
launch(args);
}
public static VMCSManager getInstance() {
return instance;
}
@Override
public void start(Stage primaryStage) throws Exception {
initializePanel();
Scene scene = new Scene(content);
stageStyle(primaryStage);
primaryStage.setScene(scene);
primaryStage.show();
}
private void initializePanel() throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("fxml/SimulatorDisplay.fxml"));
content = loader.load();
}
public void openCustomerPanel() throws IOException{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("fxml/CustomerDisplay.fxml"));
content = loader.load();
Scene scene = new Scene(content);
primaryStage.setScene(scene);
primaryStage.show();
}
}
@FXML
public void clickCustomer (ActionEvent event) throws IOException{
log.info("Starting Customer Panel");
VMCSManager.getInstance().openCustomerPanel();
}
@FXML
public void clickMaintainer(ActionEvent event) throws IOException{
log.info("Starting Maintainer Panel");
VMCSManager.getInstance().openMaintainerPanel();
}
}
@FXML
private Label brand1Lbl;
@FXML
private Label brand2Lbl;
@FXML
private Label brand3Lbl;
@FXML
private Label brand4Lbl;
@FXML
private Label brand5Lbl;
@FXML
private Label statusLbl1;
@FXML
private Label statusLbl2;
private static final Logger log=LoggerFactory.getLogger(CustomerController.class);
public CustomerController() {
context= new BuyingStateContext();
}
public void initialize(URL location, ResourceBundle resources) {
this.location = location;
this.rb = resources;
coinsValidityFlash.setVisible(false);
insertCoinTxt.setDisable(true);
brand1Btn.setStyle("-fx-background-color: #CACACA;");
brand2Btn.setStyle("-fx-background-color: #CACACA;");
brand3Btn.setStyle("-fx-background-color: #CACACA;");
brand4Btn.setStyle("-fx-background-color: #CACACA;");
brand5Btn.setStyle("-fx-background-color: #CACACA;");
populateVending();
}
.
.
.
}
`Pete据我所知,您希望将一个大java文件分割成小文件,在每个类中创建java类创建方法(函数),该方法将返回布局(HBox、VBox、Flowpane或…),然后在主类中创建该java类的对象,并使用这些方法构建大应用程序 在我的示例中,我用一个函数创建了一个主类和一个独立类,只是为了向您展示它是如何工作的。在我的主要有2个标签,2个按钮一个布局和一个对象的分离类,通过点击按钮场景将发生变化 我的主要意见是: } 我的第二节课:
public class LayoutOne {
public VBox sceneView1(Label label, Button button) {
// Layout 1 - children are laid out in vertical column
VBox layout1 = new VBox(20);
layout1.getChildren().addAll(label, button);
return layout1;
}
}相关问题:
public class SwitchSceneSample extends Application {
public static void main(String[] args) {
launch(args);
}
Stage window;
Scene scene1, scene2;
@Override
public void start(Stage primaryStage) throws Exception {
// I am using window as primaryStage
window = primaryStage;
// Label 1
Label label1 = new Label("Welcome to the first scene!");
// Label 2
Label label2 = new Label("This is second scene!");
// Button 1, by pressing this button primaryStage will be set as scene 2
Button button1 = new Button("Go to scene 2");
button1.setOnAction(e -> window.setScene(scene2));
// Button 2, by pressing this button primaryStage will be set as scene 1
Button button2 = new Button("Click to go scene 1");
button2.setOnAction(e -> window.setScene(scene1));
// Creating an object of the class'LayoutOne.java'
LayoutOne l1 = new LayoutOne();
// set my scene 1(by calling method called 'sceneView1()' from class 'LayoutOne.java')
scene1 = new Scene(l1.sceneView1(label1, button1), 200, 200);
// Set my scene 2 inside my main class
StackPane layout2 = new StackPane();
layout2.getChildren().addAll(label2, button2);
scene2 = new Scene(layout2, 600, 300);
// Making my
window.setScene(scene1);
window.setTitle("Scene Switch Sample");
window.show();
}
public class LayoutOne {
public VBox sceneView1(Label label, Button button) {
// Layout 1 - children are laid out in vertical column
VBox layout1 = new VBox(20);
layout1.getChildren().addAll(label, button);
return layout1;
}