Javafx设置背景
我正试图在fxml的帮助下为我的borderpane提供一个背景。 我有一个视图和一个控制器。 我想你必须在fxml中说:borderPane.setBackground(新背景(myBI)) 但我不知道怎么做。 使用css不是一种选择 视图: 控制器:Javafx设置背景,java,javafx,background,fxml,Java,Javafx,Background,Fxml,我正试图在fxml的帮助下为我的borderpane提供一个背景。 我有一个视图和一个控制器。 我想你必须在fxml中说:borderPane.setBackground(新背景(myBI)) 但我不知道怎么做。 使用css不是一种选择 视图: 控制器: public class PlayController { @FXML private Label label; @FXML private TextField textfield; @FXML
public class PlayController {
@FXML
private Label label;
@FXML
private TextField textfield;
@FXML
private BackgroundImage myBI= new BackgroundImage(new Image("/Background.png",1280,720,false,true),
BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT);
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello " + textfield.getText());
}
}
作为最后一个fxml
<BorderPane fx:controller="application.PlayController" xmlns:fx="http://javafx.com/fxml"
prefHeight="200" prefWidth="320" >
<top>
<Text text="java-Buddy"/>
</top>
<left>
<Label text="Who are you?"/>
</left>
<center>
<TextField id="textfield" fx:id="textfield"/>
</center>
<right>
<Button id="button" text="Click Me!"
onAction="#handleButtonAction" fx:id="button"/>
</right>
<bottom>
<Label id="label" fx:id="label"/>
</bottom>
</BorderPane>
既然你不能使用CSS,这就不太好了 您已经将
BackgroundImage
定义为myBI
,可用于为元素设置背景
例如,给您的边框窗格一个fx:id
<BorderPane fx:id="bPane" fx:controller="application.PlayController" xmlns:fx="http://javafx.com/fxml"
prefHeight="200" prefWidth="320" >
到您的控制器类
最后,创建方法
@FXML
public void initialize(){
bPane.setBackground(new Background(myBI));
}
使用
initialize
方法也不是一个选项???为什么使用CSS不是一个选项?CSS是这方面的推荐方法,学校不允许使用CSS。initialize方法是如何工作的?Fabian只是引用了中的标准initialize()
方法。您可能也可以在FXML中执行此操作,但它确实很难看。我不打算把它作为一个答案,因为它在现实生活中没有任何用处,imho。从java 10.0.1开始,FXMLLoader无法使用构造函数。
@FXML
private Borderpane bPane;
@FXML
public void initialize(){
bPane.setBackground(new Background(myBI));
}