java代码:
package xyz.hashdog.class112;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileInputStream;
/**
* @author th
* @description: TODO
* @projectName hashdog
* @date 2020/2/1620:48
*/
public class Launch extends Application {
public static void main(String[] args) {
Application.launch(Launch.class,args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader fx = new FXMLLoader();
fx.setLocation(fx.getClassLoader().getResource("fxml/class112.fxml"));
AnchorPane load = fx.load();
// AnchorPane load = (AnchorPane)fx.load(getClass().getClassLoader().getResourceAsStream("fxml/class112.fxml"));
Scene s= new Scene(load);
primaryStage.setScene(s);
primaryStage.setTitle("hashdog");
primaryStage.setWidth(500);
primaryStage.setHeight(300);
//设置窗口不可拉伸
primaryStage.setResizable(false);
primaryStage.show();
}
}
fxml代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" prefHeight="400.0" prefWidth="600.0">
<children>
<Button text="一个按钮" prefWidth="100" prefHeight="100">
<AnchorPane.leftAnchor>100</AnchorPane.leftAnchor>
<AnchorPane.topAnchor>100</AnchorPane.topAnchor>
</Button>
</children>
</AnchorPane>