Java:JavaFX项目FXML文件应用实例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/mouday/article/details/100189823

布局文件和逻辑文件分离

可以使用 Scene Builder进行编辑

IDEA中使用scene builder

项目结构

── src   
    ├── com
    │   └── company
    │       ├── Controller.java   # 控制器文件
    │       ├── Main.java         # 主文件
    │       └── sample.fxml       # 布局文件
    └── style
        └── main.css               # 样式文件

1、主文件入口文件 Main.java
引入布局文件 sample.fxml


package com.company;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
         // 此处引入布局文件
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

2、布局文件 sample.fxml
(1)绑定控制器类 com.company.Controller
(2)绑定控制器文件中的方法 com.company.Controller.clickButton
(3)引入样式文件 style/main.css

<?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 prefHeight="400.0" prefWidth="600.0" stylesheets="@../../style/main.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.company.Controller">
   <children>
      <Button layoutX="271.0" layoutY="165.0" mnemonicParsing="false" onAction="#clickButton" text="这是一个按钮" />
      <Label fx:id="label" layoutX="308.0" layoutY="126.0" />
   </children>
</AnchorPane>


3、Controller.java控制器文件

package com.company;

import javafx.scene.control.Label;

public class Controller {
    public Label label;

    public void clickButton(){
        label.setText("按钮被点击了");
        System.out.println("按钮被点击了");
    }
}

4、样式css 文件
src/style/main.css

.label{
    -fx-text-fill: red;
}

打包jar

IDEA 下操作

1、配置:
工具栏点击Project Structure -> Artifacts -> + ->
JAR -> From modules with dependencies 选中有main方法的类

2、打包:
Build -> Build artifacts

3、运行程序
文件夹 out/artifacts 中生成jar文件,双击即可打开
在装有jre 的 Mac和Windows 均可正常运行

文章创建于: 2019-09-01 23:46:39
展开阅读全文

JavaFX 匯入 fxml檔錯誤

02-28

fxml檔中包含event事件類別 SMSeventrnrn錯誤訊息 : [color=#FF0000]java.lang.IllegalAccessException: Class javafx.fxml.FXMLLoader$ValueElement can not access a member of class JavaFXAppTest.SMSevent with modifiers ""[/color]rnfile:/C:/Users/DogKing/Documents/NetBeansProjects/JavaFXAppTest/dist/JavaFXAppTest.jar!/JavaFXAppTest/sms.fxml:13rn at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:661)rn at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:572)rn at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2314)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2131)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2742)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2694)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2683)rn at JavaFXAppTest.MyFXML.start(MyFXML.java:23)rn at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)rn at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)rn at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)rn at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)rn at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)rn at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)rn at java.lang.Thread.run(Thread.java:722)rnrnException in Application start methodrnException in thread "main" java.lang.RuntimeException: Exception in Application start methodrn at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)rn at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)rn at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)rn at java.lang.Thread.run(Thread.java:722)rnCaused by: java.lang.RuntimeException: java.lang.IllegalAccessException: Class javafx.fxml.FXMLLoader$ValueElement can not access a member of class JavaFXAppTest.SMSevent with modifiers ""rn at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:661)rn at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:572)rn at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2314)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2131)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2742)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2694)rn at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2683)rn at JavaFXAppTest.MyFXML.start(MyFXML.java:23)rn at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)rn at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)rn at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)rn at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)rn at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)rn at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)rn ... 1 morernCaused by: java.lang.IllegalAccessException: Class javafx.fxml.FXMLLoader$ValueElement can not access a member of class JavaFXAppTest.SMSevent with modifiers ""rn at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)rn at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)rn at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)rn at java.lang.reflect.Field.doSecurityCheck(Field.java:983)rn at java.lang.reflect.Field.getFieldAccessor(Field.java:927)rn at java.lang.reflect.Field.set(Field.java:680)rn at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:659)rn ... 17 morernJava Result: 1rnjfxsa-run:rnBUILD SUCCESSFUL (total time: 6 seconds)rnrn我覺得主要是紅色部分那裏需要解決rnrn[color=#0000FF]-------------------------------------------------------------------[/color]rn以下為主程式:rnpackage JavaFXAppTest; rnimport javafx.fxml.FXML; rnimport javafx.application.Application; rnimport javafx.fxml.FXMLLoader;rnimport javafx.scene.Parent;rnimport javafx.scene.Scene;rnimport javafx.stage.Stage; rnrnpublic class MyFXML extends Application rn @Override rn rn public void start(Stage stage) throws Exception rn rn Parent root = FXMLLoader.load(getClass().getResource("sms.fxml")); //載入fxml檔,所有UI介面設定都在 //fxml檔裡 rn Scene scene = new Scene(root); rn rn stage.setScene(scene); rn stage.show(); rn rn public static void main(String[] args) rn launch(args); rn rn rn[color=#0000FF]------------------------------------------------------------[/color]rnSMSevent class :rnpackage JavaFXAppTest;rnrnimport javafx.event.ActionEvent;rnimport javafx.fxml.FXML;rnimport javafx.fxml.Initializable;rnimport javafx.scene.control.*;rnimport javafx.scene.layout.*;rnpublic class SMSevent extends AnchorPanern @FXMLrn TextField user,pass,phone;rn TextArea content;rn Button send,clear;rn Label result;rn public void sendMessage(ActionEvent event) rn SMS SMStest = new SMS();rn SMStest.setmessage(user.getText(),pass.getText(),phone.getText(),content.getText());rn System.out.println(SMStest.go());rn rn rnrn public void clearAll(ActionEvent event) rn user.setText(null);rn pass.setText(null);rn phone.setText(null);rn content.setText(null);rn rnrn[color=#0000FF]------------------------------------------------------[/color]rnsms.fxml:rnrnrnrnrnrnrnrnrnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rnrn 论坛

没有更多推荐了,返回首页