javaFX如何用Button实现跳转到一个已创建的fxml界面

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaFX如何用Button实现跳转到一个已创建的fxml界面相关的知识,希望对你有一定的参考价值。

JavaFX中设置某个Button的事件为打开另外一个窗口,如何设置这个窗口为我已经创建好的fxml文件?即如何直接通过Button调用另外一个我已经创建好的界面?

可以在“按钮点击事件”中写上读取fmxl为节点对象
Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));

然后把父节点放入某个new Scene(root,320,200);
然后在显示在所用的舞台stage上。追问

"显示在所用的舞台stage上"是什么意思呢?不明白。。。

追答

JavaFX 2.x的所有控件都是在stage上显示,最上层的stage由底层平台提供。
比如Application提供,或者Swing接口JFXPanel提供。
读取fxml后产生的节点Node需要放在一个场景Scene中,而场景Scene要放在某个舞台stage上。
舞台stage由平台(Application, Swing, SWT)产生和提供

参考技术A Parent root = FXMLLoader.load(getClass().getResource("/fxml/dh.fxml"))//getResource(“”)里面是fxml的地址
Stage dh=new Stage();//新建Stage
dh.setTitle("菜单导航");//给舞台赋予名称
Scene scene = new Scene(root, 600, 400);
dh.setScene(scene);
dh.show();//打开新的窗口
//获取当前按钮所在的窗口,其中login是按钮的fx:id
Stage main = (Stage) login.getScene().getWindow();
//关闭窗口
main.close();

javafx怎么实现页面的跳转

参考技术A 1. 后台
public class Check
public static boolean checkreturn(String account,String password)
boolean checkbool = false;
if("account".equals(account)&&"password".equals(password))
checkbool = true;

return checkbool;



import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;

public class LoginController implements Initializable
@FXML private TextField account;
@FXML private PasswordField password;
private FXMLTest application;

public void setApp(FXMLTest application)
this.application = application;

@FXML
public void LOGIN_M(ActionEvent event)
application.userlogin(account.getText(), password.getText());


@FXML
private void CLEAR_M(ActionEvent event)
account.setText(null);
password.setText(null);


@Override
public void initialize(URL url, ResourceBundle rb)
// TODO



import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

public class MainController implements Initializable
private FXMLTest application;

public void setApp(FXMLTest application)
this.application = application;


@FXML
private void OUT_M(ActionEvent event)
application.useroutmain();


@Override
public void initialize(URL url, ResourceBundle rb)





import check.Check;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class FXMLTest extends Application
private Stage stage;
private final double MINIMUM_WINDOW_WIDTH = 400.0;
private final double MINIMUM_WINDOW_HEIGHT = 250.0;

@Override
public void start(Stage primaryStage) throws Exception
stage = primaryStage;
stage.setTitle("FXML Login Sample");
stage.setMinWidth(MINIMUM_WINDOW_WIDTH);
stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);
gotologin();
stage.show();

public void gotologin()
try
LoginController login = (LoginController) replaceSceneContent("FXML_LOGIN.fxml");
login.setApp(this);
catch (Exception ex)
Logger.getLogger(FXMLTest.class.getName()).log(Level.SEVERE, null, ex);


public void gotomain()
try
MainController main = (MainController) replaceSceneContent("FXML_MAIN.fxml");
main.setApp(this);
catch (Exception ex)
Logger.getLogger(FXMLTest.class.getName()).log(Level.SEVERE, null, ex);


public void userlogin(String account,String password)
if(Check.checkreturn(account,password))
gotomain();


public void useroutmain()
gotologin();

private Initializable replaceSceneContent(String fxml) throws Exception
FXMLLoader loader = new FXMLLoader();
InputStream in = FXMLTest.class.getResourceAsStream(fxml);
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(FXMLTest.class.getResource(fxml));
AnchorPane page;
try
page = (AnchorPane) loader.load(in);
finally
in.close();

Scene scene = new Scene(page, 800, 600);
stage.setScene(scene);
stage.sizeToScene();
return (Initializable) loader.getController();

public static void main(String[] args)
launch(args);


2. xml配置

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="250.0" prefWidth="400.0"
xmlns:fx="http://javafx.com/fxml" fx:controller="fxmltest.LoginController">
<children>
<VBox layoutX="3.0" layoutY="0.0" prefHeight="250.0" prefWidth="390.0" spacing="20.0">
<children>
<HBox minHeight="37.0" prefHeight="37.0" prefWidth="411.0">
<children>
<Label text="登陆界面">
<effect>
<DropShadow height="7.845238095238096" radius="4.1815476190476195" width="10.880952380952381" />
</effect>
<font>
<Font name="System Bold" size="30.0" />
</font>
<HBox.margin>
<Insets left="140.0" />
</HBox.margin>
</Label>
</children>
</HBox>
<GridPane alignment="TOP_RIGHT" prefWidth="380.0">
<children>
<Label alignment="TOP_RIGHT" text="Account" GridPane.columnIndex="0" GridPane.halignment="RIGHT" GridPane.rowIndex="0">
<effect>
<DropShadow blurType="TWO_PASS_BOX" height="7.845238095238096" radius="3.675595238095238" width="8.857142857142858" />
</effect>
<font>
<Font size="25.0" fx:id="x1" />
</font>
<GridPane.margin>
<Insets right="8.0" fx:id="x2" />
</GridPane.margin>
</Label>
<Label font="$x1" text="password" GridPane.columnIndex="0" GridPane.halignment="RIGHT" GridPane.margin="$x2" GridPane.rowIndex="1">
<effect>
<DropShadow blurType="TWO_PASS_BOX" height="7.845238095238094" radius="3.6755952380952372" width="8.857142857142854" />
</effect>
</Label>
<TextField fx:id="account" prefHeight="26.0" prefWidth="268.0" promptText="please input your account" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<PasswordField fx:id="password" prefWidth="223.0" promptText="please input your password" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Button id="btn_login" mnemonicParsing="false" onAction="#LOGIN_M" prefWidth="80.0" text="登陆" GridPane.columnIndex="0" GridPane.halignment="RIGHT" GridPane.rowIndex="2">
<effect>
<DropShadow />
</effect>
<GridPane.margin>
<Insets top="15.0" />
</GridPane.margin>
</Button>
<Button id="btn_clear" mnemonicParsing="false" onAction="#CLEAR_M" prefWidth="80.0" text="清除" GridPane.columnIndex="1" GridPane.rowIndex="2">
<effect>
<DropShadow />
</effect>
<GridPane.margin>
<Insets left="80.0" top="15.0" />
</GridPane.margin>
</Button>
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="205.0" minWidth="10.0" prefWidth="147.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="318.0" minWidth="10.0" prefWidth="243.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
<padding>
<Insets top="30.0" />
</padding>
</VBox>
</children>
</AnchorPane>

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns:fx="http://javafx.com/fxml" fx:controller="fxmltest.MainController">
<children>
<Hyperlink layoutX="86.0" layoutY="111.0" text="退出登录!" onAction="#OUT_M">
<font>
<Font size="45.0" />
</font>
</Hyperlink>
</children>
</AnchorPane>

以上是关于javaFX如何用Button实现跳转到一个已创建的fxml界面的主要内容,如果未能解决你的问题,请参考以下文章

如何用window.scrollTo实现打开网页跳转到DIV?

Thinkphp后台如何用js跳转到指定页面 怎么写

如何用js添加点击事件,点击后跳转到指定动态页面

如何用jquery实现页面跳转

从微信直接跳转到我们的APP下载如何用MaxJump,VJump和JumpTool来实现

如何用html代码 实现页面跳转