我创建了两个fxml屏幕,我将它们与一个javafx程序链接起来,我将从一个fxml文件转到另一个fxml文件(通过单击按钮)。第一个fxml的屏幕尺寸是1280x768,第二个fxml屏幕尺寸是820x420。问题在于,每当我执行第二个程序时,屏幕尺寸与第一个屏幕相同。 有没有办法改变第二屏幕尺寸。 或任何其他方式设置屏幕大小与我声明的相同。 我知道start()方法只在javafx文件执行时执行。
这是我的Main.java
package application;
import java.io.IOException;
//import com.sun.javafx.css.converters.URLConverter;
//import com.sun.javafx.css.StyleConverterImpl;
import javafx.stage.StageStyle;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
//import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Main extends Application{
// @FXML Button SkipButton;
@FXML Button exit;
@FXML TextField Logint1,Logint2;
@FXML Button LoginB1;
String s1,s2;
public void start(Stage st)
{
try{
//For Maximizing button
st.resizableProperty().setValue(Boolean.FALSE);
//For max and minimum button
st.initStyle(StageStyle.UTILITY);
//For disabling all button
//st.initStyle(StageStyle.UNDECORATED);
st.setTitle("Login Screen");
Parent root =FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene sc=new Scene(root,1280,800);
st.setScene(sc);
st.show();
}
catch(Exception e)
{
System.out.print("Exception="+e);
e.printStackTrace();
}
}
public void LoginB1(ActionEvent ae) throws IOException
{
String s1 = Logint1.getText();
String s2 = Logint2.getText();
Stage ps = null;
if(s1.equals("Admin") && s2.equals("Admin"))
{
System.out.println("Equals");
try{
FXMLLoader loader=new FXMLLoader(getClass().getResource("Home.fxml"));
Parent root=loader.load();
LoginB1.getScene().setRoot(root);
throw new IOException(" ");
}
catch (IOException ie)
{
}
}
else
{
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Login is Not Valid");
alert.setHeaderText("Username or Password not Valid");
alert.setContentText("Username and Password");
alert.showAndWait();
}
}
/*
public void SkipBtn(ActionEvent ae) throws IOException
{
try{
FXMLLoader loader=new FXMLLoader(getClass().getResource("FirstSignUp.fxml"));
Parent root=loader.load();
SkipButton.getScene().setRoot(root);
throw new IOException(" ");
}
catch (IOException ie)
{
}
}
*/
public static void main(String args[])
{
launch(args);
}
}
Login.fxml which is linked on button LoginB1
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.effect.Glow?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<BorderPane xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Main">
<center>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="640.0" style="-fx-background-image: url('E:/home/devil/Desktop/sdadsaffga/a.jpeg');">
<children>
<Pane layoutX="-2.0" layoutY="-4.0" prefHeight="501.0" prefWidth="640.0">
<children>
<ImageView fitHeight="476.0" fitWidth="640.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@teaser-bg-2.png" />
</image>
</ImageView>
<Label fx:id="txtcpc" layoutX="124.0" text="Central Purchassing System" textFill="#255bcc">
<font>
<Font size="33.0" />
</font>
</Label>
<TextField fx:id="Logint1" layoutX="351.0" layoutY="139.0" promptText="Enter Your Username">
<font>
<Font size="15.0" />
</font>
<effect>
<DropShadow />
</effect>
</TextField>
<PasswordField fx:id="Logint2" layoutX="351.0" layoutY="194.0" promptText="Enter Your Password">
<font>
<Font size="15.0" />
</font>
<effect>
<DropShadow />
</effect>
</PasswordField>
<Button fx:id="LoginB1" layoutX="256.0" layoutY="257.0" mnemonicParsing="false" onAction="#LoginB1" prefHeight="31.0" prefWidth="102.0" text="Login" textFill="#255bccfc">
<font>
<Font size="20.0" />
</font>
<effect>
<InnerShadow />
</effect>
</Button>
<Label layoutX="118.0" layoutY="199.0" text="Enter Your Password">
<font>
<Font size="15.0" />
</font>
</Label>
<Label layoutX="116.0" layoutY="139.0" text="Enter Your Username">
<font>
<Font size="15.0" />
</font>
</Label>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="41.0" layoutY="243.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@CollegeLogo.jpg" />
</image>
</ImageView>
</children>
<effect>
<Glow />
</effect>
</Pane>
</children>
</AnchorPane>
</center>
<top>
<MenuBar prefHeight="31.0" prefWidth="636.0" BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
第二个Fxml文件Home.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.effect.Blend?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>
<BorderPane xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Home">
<center>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="640.0">
<children>
<Button layoutX="44.0" layoutY="115.0" mnemonicParsing="false" prefHeight="36.0" prefWidth="76.0" text="Home" textFill="#255bcc">
<font>
<Font size="17.0" />
</font>
<effect>
<Blend />
</effect>
</Button>
<Button fx:id="createFile" layoutX="172.0" layoutY="112.0" mnemonicParsing="false" onAction="#createFile" prefHeight="47.0" prefWidth="159.0" text="Add a File" textFill="#255bcc">
<font>
<Font size="17.0" />
</font>
</Button>
<Button fx:id="ModifyFile" layoutX="381.0" layoutY="112.0" mnemonicParsing="false" onAction="#ModifyFile" prefHeight="47.0" prefWidth="168.0" text="Modify a File" textFill="#255bcc">
<font>
<Font size="17.0" />
</font>
</Button>
</children>
</AnchorPane>
</center>
<top>
<BorderPane prefHeight="98.0" prefWidth="640.0" BorderPane.alignment="CENTER">
<top>
<MenuBar prefHeight="0.0" prefWidth="640.0" BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Add New File" />
<MenuItem mnemonicParsing="false" text="History" />
<MenuItem mnemonicParsing="false" text="Save" />
<MenuItem mnemonicParsing="false" text="Save As" />
<MenuItem mnemonicParsing="false" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Modify Existing File" />
<MenuItem mnemonicParsing="false" text="Delete Existing File" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="Help" />
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<Label text="Admin" textFill="#255bcc" BorderPane.alignment="CENTER">
<font>
<Font size="38.0" />
</font>
<graphic>
<Label text="Welcome," textFill="#255bcc">
<font>
<Font size="32.0" />
</font>
</Label>
</graphic>
</Label>
</center>
</BorderPane>
</top>
</BorderPane>
Home.fxml的控制器文件
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Home extends Application {
@FXML Button createFile,ModifyFile;
@Override
public void start(Stage primaryStage) throws IOException {
Parent root =FXMLLoader.load(getClass().getResource("Home.fxml"));
Scene sc=new Scene(root,1280,800);
primaryStage.setScene(sc);
primaryStage.show();
}
public void createFile(ActionEvent ae)// throws IOException
{
try{
FXMLLoader loader=new FXMLLoader(getClass().getResource("HeadersLayout.fxml"));
Parent root=loader.load();
//Scene sc=new Scene(root,1280,800);
createFile.getScene().setRoot(root);
throw new IOException("");
}
catch(IOException ie)
{
}
}
public void ModifyFile(ActionEvent ae)
{
try{
FXMLLoader loader=new FXMLLoader(getClass().getResource("Modify.fxml"));
Parent root=loader.load();
//Scene sc=new Scene(root,1280,800);
createFile.getScene().setRoot(root);
throw new IOException("");
}
catch(IOException ie)
{
}
}
public static void main(String[] args) {
launch(args);
}
}