我试图在同一窗口中打开一个新场景,但是出现了新窗口。我尝试了一些解决方法,但是打开新场景的按钮不起作用(单击时什么也没有发生),或者单击它时控制台中出现很多错误。我有什么可以做的吗?
主要:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
@Override
public void start(Stage stage) {
try {
AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,706,552);
stage.setResizable(false);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
stage.setScene(scene);
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
控制器:
package application;
import java.io.*;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.fxml.*;
import javafx.stage.*;
public class SampleController implements Initializable{
@FXML
private Button start;
public void startGame(ActionEvent e) throws IOException
{
FXMLLoader fxmlLoader = new
FXMLLoader(getClass().getResource("/application/BoardView.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
//set what you want on your stage
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("BoardView");
stage.setScene(new Scene(root1));
stage.setResizable(false);
stage.show();
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
}
BoardView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="552.0" prefWidth="706.0" style="-fx-background-color: #0033cc;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1">
<children>
<GridPane prefHeight="552.0" prefWidth="706.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="451.6666259765625" minWidth="10.0" prefWidth="448.6666259765625" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="356.33331298828125" minWidth="10.0" prefWidth="257.66670735677076" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="287.0" minHeight="10.0" prefHeight="287.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="287.0" minHeight="10.0" prefHeight="287.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<AnchorPane prefHeight="195.0" prefWidth="258.0" GridPane.columnIndex="1">
<children>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="36.0" layoutY="44.0" pickOnBounds="true" preserveRatio="true" />
<Label layoutX="69.0" layoutY="217.0" prefHeight="45.0" prefWidth="135.0" text="Label" textFill="#3adc5d">
<font>
<Font size="26.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="812.0" prefWidth="456.0">
<children>
<ImageView fitHeight="459.0" fitWidth="525.0" layoutY="353.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../img/55149794-ludo-board-game.jpg" />
</image>
</ImageView>
<ImageView fitHeight="78.0" fitWidth="458.0" layoutY="270.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../img/headingv0.3.png" />
</image>
</ImageView>
</children>
</AnchorPane>
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
<children>
<TextField alignment="BOTTOM_LEFT" layoutX="25.0" layoutY="29.0" prefHeight="20.0" prefWidth="208.0" />
<Label layoutX="29.0" layoutY="73.0" prefHeight="65.0" prefWidth="200.0" text="Right Translation" textFill="#3adc5d">
<font>
<Font size="26.0" />
</font>
</Label>
</children>
</AnchorPane>
</children>
</GridPane>
</children>
</AnchorPane>
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.Glow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="552.0" prefWidth="706.0" style="-fx-background-color: #0033cc;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<children>
<GridPane layoutX="4.0" layoutY="96.0" prefHeight="456.0" prefWidth="706.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="96.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="450.0" minWidth="10.0" prefWidth="450.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="345.60003662109375" minWidth="10.0" prefWidth="250.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<AnchorPane prefHeight="553.0" prefWidth="453.0" style="-fx-background-color: #0033cc;">
<children>
<ImageView fitHeight="276.0" fitWidth="326.0" layoutX="53.0" layoutY="129.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../img/games-clipart-3.png" />
</image>
</ImageView>
</children>
</AnchorPane>
<AnchorPane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #0033cc;" GridPane.columnIndex="1">
<children>
<Button layoutX="7.0" layoutY="173.0" mnemonicParsing="false" onAction="#startGame" prefHeight="70.0" prefWidth="209.0" style="-fx-background-color: #000099;" text="Start" textFill="#fdf8f8">
<font>
<Font size="33.0" />
</font>
<effect>
<Glow level="1.0" />
</effect>
</Button>
<Label layoutX="16.0" layoutY="418.0" prefHeight="18.0" prefWidth="209.0" text="Copyrighted abc" />
</children>
</AnchorPane>
</children>
</GridPane>
<ImageView fitHeight="165.0" fitWidth="751.0" layoutX="-4.0" layoutY="1.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../img/headingv0.3.png" />
</image>
</ImageView>
</children>
</AnchorPane>