你能把两个不同的Java FX场景写成两个独立的类吗? [英] Can you write two different Java FX scenes as two separate classes?

查看:151
本文介绍了你能把两个不同的Java FX场景写成两个独立的类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名Java程序员,在当地大学完成了Java 101课程。我也在努力学习一些额外的主题,包括Java FX。我已经在Oracle的网站上完成了Java FX教程,还看了一些YouTube视频,还读了Java FX for Dummies(这是我能为初学者找到的最好的书。)所有这些材料教会了我很多基础知识,但一些(应该)相对简单的东西让我感到厌烦。

I'm a beginning Java programmer, finishing up the "Java 101" class at my local university. I'm also pushing myself to learn some extra topics on the side, including Java FX. I've worked through the Java FX tutorials on Oracle's website, plus sat through some YouTube videos, plus read "Java FX for Dummies" (which was the best book I could find for a beginner.) All of this material has taught me a lot of the basics, but some stuff that (should be) relatively simple escapes me.

例如:假设我有一个Java FX程序在一个上使用多个场景阶段。当用户点击切换!时按钮,第二个场景换出第一个。简单。我可以在一个.java文件中完成所有这些,没问题。 (参见下面的代码)

For example: Let's say I have a Java FX program that uses multiple scenes on one stage. When the user clicks a "Switch!" button, the second scene is swapped out for the first. Easy. I can do all of this in one .java file, no problem. (See code below)

但是我的.java类文件变得非常冗长而且很麻烦。如果我可以将一个场景定义/声明/初始化为一个.java文件中的一个类而将另一个场定义为另一个.java文件中的另一个类,那将会很棒。这将使得更加容易地跟踪每个场景的组件。问题是,我无法弄清楚如何做到这一点。

But my .java class file is getting really long and cumbersome to troubleshoot. It would be great if I could define/declare/initialize one scene as one class in one .java file and the second scene as another class in another .java file. This would make keeping track of the components of each scene much, much easier. The problem is, I can't figure out how to do this.

我想你会编写一个Scene1.java类然后编写一个Scene2.java类,当你想切换场景时,只需在两者之间传递舞台对象。但我找不到这样做的例子,我的所有尝试都会导致编译器错误或真正可怕的运行时错误。

I'd imagine that you would write a Scene1.java class and then a Scene2.java class, and simply pass the stage object between the two when you want to switch scenes. But I can't find an example of how this is done, and all my attempts result in compiler errors or really scary runtime errors.

有谁知道这是怎么回事做了什么?如果是这样,我需要做些什么来修改下面的 SwitchScenes2()方法来创建新的 Scene2 对象和把它传给舞台?

Does anyone know how this can be done? If so, what would I have to do to modify the SwitchScenes2() method below to create the new Scene2 object and pass it the stage?

谢谢! RAO

/*
    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();
    }
}


推荐答案

Pete据我所知,你希望将一个大的java文件分成小文件,在每个类中创建Java类,创建方法(函数)将返回布局(HBox,VBox,Flowpane或....)然后在你的主要创建一个对象那个Java类并使用这些方法构建大型应用程序。

Pete as I understand you wish to separate one big java file into small files,create Java classes in each class create method(function) that will return layout(HBox,VBox, Flowpane or ....)then in your main create an object of that Java class and use those methods to build on big application.

在我的示例中我用一个函数创建了一个main和一个单独的类,只是为了向您展示它的工作原理。在我的主要有2个标签,2个按钮一个布局和一个分离类的对象,通过单击按钮场景将更改
我的主要:

in my sample I made one main and one separated class with one function,just to show you how its works. In my main there is 2 lables, 2 buttons one layout and one object of the separated class, by clicking the buttons scenes will change My Main:

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;
}

}

这篇关于你能把两个不同的Java FX场景写成两个独立的类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆