我正在使用Netbeans 7.2和Scene Builder1.0开发一个JavaFX应用程序。我有我的主屏幕设置,我想有它,所以我点击一个按钮,它将关闭主窗口和打开另一个。主Stage对象位于main类中,但controller类是独立的,不能访问它,因为它不是静态的,并且位于不同的类中。我如何改变场景或舞台?
下载JavaFX示例找到项目FXML-LoginDemo,这就是您所需要的。为了快速参考,我在这里复制粘贴主java类;)
/*
* Copyright (c) 2008, 2011 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
* This file is available and licensed under the following license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
* - Neither the name of Oracle Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package demo;
import demo.model.User;
import demo.security.Authenticator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
* Main Application. This class handles navigation and user session.
*/
public class App extends Application {
private Stage stage;
private User loggedUser;
private static App instance;
public App() {
instance = this;
}
public static App getInstance() {
return instance;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override public void start(Stage primaryStage) {
try {
stage = primaryStage;
gotoLogin();
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
public User getLoggedUser() {
return loggedUser;
}
public boolean userLogging(String userId, String password){
if (Authenticator.validate(userId, password)) {
loggedUser = User.of(userId);
gotoProfile();
return true;
} else {
return false;
}
}
public void userLogout(){
loggedUser = null;
gotoLogin();
}
private void gotoProfile() {
try {
replaceSceneContent("profile.fxml");
} catch (Exception ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void gotoLogin() {
try {
replaceSceneContent("login.fxml");
} catch (Exception ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}
}
private Parent replaceSceneContent(String fxml) throws Exception {
Parent page = (Parent) FXMLLoader.load(App.class.getResource(fxml), null, new JavaFXBuilderFactory());
Scene scene = stage.getScene();
if (scene == null) {
scene = new Scene(page, 700, 450);
scene.getStylesheets().add(App.class.getResource("demo.css").toExternalForm());
stage.setScene(scene);
} else {
stage.getScene().setRoot(page);
}
stage.sizeToScene();
return page;
}
}
我正在制作一个计算器。到目前为止,我已经创建了几个类。有一个IntegerButton,当它被按下时,应该在监视器上显示它的值。 但是,我不明白如何做到这一点。 当我在每个按钮的构造函数中定义句柄(ActionEvent事件)时,我无法引用监视器。此外,我完全迷失了EventHandler的概念。我的监视器应该监听事件吗?我的Button是否应该在start(舞台初选阶段)方法中调用它的setOn
问题内容: 当我使用WebEngine创建播放YouTube视频的新舞台时,关闭它之后-Youtube继续在backgroung上播放。如果我使用“ Platform.exit”-它会关闭我的所有JavaFX App,但我只想关闭为YouTube创建的阶段。 这是我针对YouTube播放器的课程: 在“主舞台”中单击按钮后,我的YouTube播放器舞台正在创建: 问题答案: 您无法处置WebEng
我怎么绕过这个?我需要以某种方式再次实现那个方法,但突然间我不能通过作为一个论点的阶段。
我正在创建一个应用程序,当我单击一个按钮时,它将在new中打开一个表。但我的问题是,当我关闭该表的时,应用程序不会释放内存。JavaFX有什么问题吗?还是我得做点别的? 我尝试在该阶段结束时将所有内容设置为空,但仍然没有释放内存。 表的舞台上的关闭事件: 表视图; 舞台我的舞台; 我已经创建了一个名为replaceScene方法来使用文件为Stage加载场景。它将返回它的控制器和设置的场景进入舞台
我试图在我的项目中使用依赖注入,这样我就可以在场景之间传输数据。例如,在一个控制器中,我得到了名为的字段。这是我在舞台上根据某种逻辑得出的。当我改变阶段时,我想通过新的阶段控制器来访问这个变量。新控制器具有相同的字段。 我用标记了这些字段,但我不明白当控制器发生变化时如何存储值。我用构造函数、getter和setter创建了实体,我假设它是存储变量的