【问题标题】: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?无法从控制器类执行按钮样式方法(FXML 按钮)。如何设置 FXML 按钮的样式而不使用 CSS?
【发布时间】:2016-11-10 13:32:03
【问题描述】:

我不能简单地在 if-then-statement 或方法中设置按钮的样式。

它编译良好,但在运行应用程序时出错。这个想法是当参数“setnrColor”等于 7 时为按钮着色。 该按钮分配有@FXML 标签。当我不使用引用按钮的 setStyle / setTextFill 方法时,逻辑运行良好。

运行时,我收到以下错误:线程“JavaFX 应用程序线程”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>

【问题讨论】:

  • 请显示空指针异常的堆栈跟踪
  • 有两种可能性:a) fxml 中的&lt;Button&gt; 元素没有fx:id="btn" 属性b) 您使用的控制器类的不同实例不是由FXMLLoader/ 创建的在使用实例方法加载 fxml 之前设置为 FXMLLoader 的控制器。根据您在此问题中调用setButtonColor 的位置,可能已在此处回答:stackoverflow.com/questions/14187963/…
  • 感谢您的回答。我检查了 fx:id 的 fxml 文件,它被命名为“btn”。但问题是存在的。
  • Edit 您的问题包括完整的堆栈跟踪和 FXML 文件。
  • @fabian,我正在研究可能性 b。 FXMLDocumentController 在 fxml 文件中设置,因此它应该知道这是要使用的正确控制器。但是我正在另一个类中创建 FXMLDocumentController 的实例,因为我不能使用“setButtonColor”的静态方法调用,因为 btn 对象不是静态的。这就是我创建 FXMLDocumentController 实例的原因。

标签: java button javafx fxml


【解决方案1】:

您不应该创建控制器类的对象,您需要从加载器中提取 if:

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

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

【讨论】:

  • 我真的很高兴您编辑了您的最后一条评论。那成功了。真的很开心,谢谢你 :)
  • 有没有办法调用这个controller.setButtonColor(..);基于另一个类的触发器的方法?因为现在它只在应用程序启动时被调用一次。基本上,该方法需要每隔几秒从另一个类触发一次。
  • @Bram 您可以将控制器作为参数传递给您需要的类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
  • 2017-01-16
  • 1970-01-01
  • 2019-11-09
  • 2014-04-21
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多