JavaFX使用FXML入门(一)

本文介绍了一个使用JavaFX创建图形界面应用程序的例子,通过FXML进行界面布局,展示了如何加载FXML文件并将其设置为场景的一部分,同时设置了主窗口的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值