簡體   English   中英

無法執行控制器類中的按鈕樣式方法(FXML按鈕)。 如何設置FXML按鈕的樣式而不為此使用CSS?

[英]Can't execute button style methods (FXML button) from controller class. How can I setstyles of a FXML button and not using CSS for this?

我不能僅在if-then語句或方法中設置按鈕的樣式。

它可以很好地編譯,但是在運行應用程序時會出錯。 想法是在參數“ setnrColor”等於7時給按鈕着色。該按鈕分配有@FXML標簽。 當我不使用引用按鈕的setStyle / setTextFill方法時,邏輯工作良好。

運行時,出現以下錯誤:線程“ JavaFX Application Thread” java.lang.NullPointerException中的異常。 當調用“ setstyle方法時,會發生這種情況。

一個簡單的解釋說這是不可能的,這是有道理的。 到目前為止,我的所有搜索都沒有得到這個答案。 任何幫助表示贊賞。

public class FXMLDocumentController implements Initializable {

@FXML private Label label; 
@FXML private Button btn;
@FXML private TextField text;
private int NrColor; 

public void setButtonColor (int setnrColor){

boolean bln1;

NrColor = setnrColor;

if(NrColor==7){ //Expression is of type boolean
System.out.println("SAME NUMBER: SET COLOR BUTTON TO BLACK");
//btn.setStyle("-fx-base:black;"); //THIS LINE DOESNT WORK
bln1 = true; 
//btn.getStyleClass().remove("armed");
}  
else {
System.out.println("DIFFERENT NUMBER: SET COLOR BUTTON TO RED");
//btn.setStyle("-fx-base:red;"); //DOESNT WORK
bln1 = false; 
trigger();

}

System.out.println("OUTPUT =" + bln1);

}


java.lang.NullPointerException:
at test_issue.FXMLDocumentController.trigger(FXMLDocumentController.java:64)
at test_issue.FXMLDocumentController.setButtonColor(FXMLDocumentController.java:53)
at test_issue.FlashingLight.lambda$start$1(FlashingLight.java:25)
at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:239)
at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:180)
at javafx.animation.Timeline.impl_playTo(Timeline.java:176)
at javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.java:39)
at com.sun.scenario.animation.shared.FiniteClipEnvelope.timePulse(FiniteClipEnvelope.java:124)
at javafx.animation.Animation.impl_timePulse(Animation.java:1102)
at javafx.animation.Animation$1.lambda$timePulse$25(Animation.java:186)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.Animation$1.timePulse(Animation.java:185)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:506)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

/////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test_issue.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<Button fx:id="btn" layoutX="104.0" layoutY="72.0" mnemonicParsing="false" prefHeight="56.0" prefWidth="136.0" text="Button" />
<TextField fx:id="text" layoutX="41.0" layoutY="14.0" promptText="HALLO" />
</children>
</AnchorPane>

您不應該創建控制器類的對象,需要從加載程序中提取以下內容:

FXMLLoader loader = new FXMLLoader(getClass().getResource("/main.fxml"));
AnchorPane root = loader.load();

FXMLDocumentController controller = loader.getController();
controller.setButtonColor(...);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM