0
我以前問過這個問題,但我無法提供一個最好的,最完整的和可驗證的例子。所以我做了另一個正確工作並且完全最小化的樣本。順便說一句,我想改變一個菜單框的顏色,點擊其中的一個,並且當我點擊另一個項目(如按鈕)時,它將保持持久性。下面是低於樣本,請me..again幫助..JavaFX:如何保持VBox背景顏色持久?
FXMLDocumentController.java
package javafxapplication1;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
/**
*
* @author James
*/
public class FXMLDocumentController implements Initializable {
@FXML
private VBox menuVBox1 = new VBox();
@FXML
private VBox menuVBox2 = new VBox();
@FXML
private VBox menuVBox3 = new VBox();
@FXML
private VBox parentMenuVBox = new VBox(menuVBox1, menuVBox2, menuVBox3);
private final Background focusBackground = new Background(new BackgroundFill(Color.web("#000000"), CornerRadii.EMPTY, Insets.EMPTY));
private final Background unfocusBackground = new Background(new BackgroundFill(Color.web("#F4F4F4"), CornerRadii.EMPTY, Insets.EMPTY));
private void setMenuBoxColor (VBox menu) {
VBox menuVBox = menu;
menuVBox.requestFocus();
for (Node child : parentMenuVBox.getChildren()) {
VBox vb = (VBox) child;
vb.backgroundProperty().bind(Bindings
.when(vb.focusedProperty())
.then(focusBackground)
.otherwise(unfocusBackground)
);
}
}
@FXML
private void handleSelectMenus(MouseEvent event) {
//Change the color of clicked VBox
setMenuBoxColor((VBox)event.getSource());
System.out.println("Menu clicked");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="132.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.FXMLDocumentController">
<left>
<VBox fx:id="parentMenuVBox" prefHeight="200.0" prefWidth="100.0" spacing="10.0" BorderPane.alignment="CENTER">
<children>
<VBox fx:id="menuVBox1" onMouseClicked="#handleSelectMenus" prefHeight="200.0" prefWidth="100.0" style="-fx-border-color: #000000;">
<children>
<Label text="MENU1" />
</children>
</VBox>
<VBox fx:id="menuVBox2" layoutX="10.0" layoutY="10.0" onMouseClicked="#handleSelectMenus" prefHeight="200.0" prefWidth="100.0" style="-fx-border-color: #000000;">
<children>
<Label text="MENU2" />
</children>
</VBox>
<VBox fx:id="menuVBox3" layoutX="10.0" layoutY="160.0" onMouseClicked="#handleSelectMenus" prefHeight="200.0" prefWidth="100.0" style="-fx-border-color: #000000;">
<children>
<Label text="MENU3" />
</children>
</VBox>
</children>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</VBox>
</left>
<bottom>
<HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Button mnemonicParsing="false" text="Next" />
</children>
</HBox>
</bottom>
</BorderPane>
javaFXApplication1.java
package javafxapplication1;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author James
*/
public class JavaFXApplication1 extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}