我不知道如何设计分隔符的样式.我不理解JavaFX CSS样式website的文档--更具体地说是如何使用子 struct (在本例中是一行).
我是这样创建我的分隔符的:
Separator separator = new Separator(Orientation.HORIZONTAL);
separator.setStyle("-fx-fill: white; -fx-background-color: white; -fx-border-color: null;");
The -fx-fill: white;
and -fx-border-color: null;
don't do anything. This is how it looks with this styling:
这是它没有造型的样子:
我希望它看起来只有一行字.
我对垂直分隔符也有同样的问题:
Separator separator = new Separator(Orientation.VERTICAL);
separator.setStyle("-fx-background-color: white; -fx-border-color: null;");
-fx-border-color: null;
再一次没有任何作用.这是这个造型的外观:
这是它没有造型的样子:
我希望它看起来只有一行字.
我的主要代码是:
import java.io.FileNotFoundException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Slider;
import javafx.scene.effect.BoxBlur;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Visualisation extends Application {
private static final int GRID_SIZE = 40;
private static CheckBox checkBox;
private static double zoomFactor = 1;
private static VBox settings = new VBox();
private static AtomicInteger delay = new AtomicInteger(250);
private static AtomicBoolean isPaused = new AtomicBoolean(true);
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws FileNotFoundException {
StackPane root = new StackPane();
root.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, javafx.geometry.Insets.EMPTY)));
Scene scene = new Scene(root, 400, 400);
Group content = new Group();
root.getChildren().add(content);
root.getChildren().add(new Separator(Orientation.HORIZONTAL)); // here I can set horizontal or vertical Seperator
// root.getChildren().add(new Separator(Orientation.VERTICAL));
setupSettings(root, stage, content);
makeZoomable(scene, content);
makeKeyPresses(stage, scene, content);
scene.widthProperty().addListener(e -> resetSize(scene, content));
scene.heightProperty().addListener(e -> resetSize(scene, content));
stage.setScene(scene);
stage.setTitle("Visualisation");
stage.setFullScreen(true);
stage.setFullScreenExitHint("");
stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
stage.show();
}
private void makeZoomable(Scene scene, Group content) {
scene.setOnScroll((ScrollEvent event) -> {
double deltaY = event.getDeltaY();
if (deltaY > 0) {
zoomFactor += 0.01;
} else {
zoomFactor -= 0.01;
if (zoomFactor <= 0) {
zoomFactor = 0.01;
}
}
updateTransforms(content);
});
}
private void setupSettings(StackPane root, Stage stage, Group content) {
checkBox = new CheckBox("Fullscreen");
checkBox.setStyle("-fx-text-fill: WHITE; -fx-font-size: 20; -fx-font-weight: bold");
checkBox.fire();
checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
if (stage.isFullScreen()) {
stage.setFullScreen(false);
} else {
stage.setFullScreen(true);
}
});
Text label = new Text("Speed slider");
label.setStyle("-fx-fill: WHITE; -fx-font-size: 20");
Slider slider = new Slider(10, 1000, delay.get());
slider.setBlockIncrement(100);
slider.setSnapToTicks(true);
slider.valueProperty().addListener((observable, oldValue, newValue) -> delay.set(newValue.intValue()));
VBox speedControler = new VBox();
speedControler.setAlignment(Pos.CENTER);
speedControler.getChildren().addAll(label, slider);
Button button1 = new Button("Back");
button1.setOnAction(e -> openCloseSettings(content));
Button button2 = new Button("Exit");
button2.setOnAction(e -> System.exit(0));
HBox buttons = new HBox();
buttons.setAlignment(Pos.CENTER);
buttons.getChildren().addAll(button1, button2);
buttons.setSpacing(30);
settings.setAlignment(Pos.CENTER);
settings.setMaxWidth(Math.min(root.getWidth(), root.getHeight()));
settings.getChildren().addAll(checkBox, speedControler, buttons);
settings.setVisible(false);
settings.setSpacing(30);
root.getChildren().add(settings);
}
private void makeKeyPresses(Stage stage, Scene scene, Group content) {
scene.setOnKeyPressed(event -> {
switch (event.getCode()) {
case SPACE:
if (!settings.isVisible()) {
isPaused.set(!isPaused.get());
}
break;
case F5:
checkBox.fire();
break;
case ESCAPE:
openCloseSettings(content);
break;
default:
break;
}
});
}
private static void resetSize(Scene scene, Group root) {
boolean isPausedRemember = isPaused.get();
isPaused.set(true);
root.setLayoutX((scene.getWidth() - root.prefWidth(-1)) / 2);
root.setLayoutY((scene.getHeight() - root.prefHeight(-1)) / 2);
double scaleX = scene.getWidth() / root.getBoundsInLocal().getWidth();
double scaleY = scene.getHeight() / root.getBoundsInLocal().getHeight();
zoomFactor = Math.min(scaleX, scaleY) - 0.01;
updateTransforms(root);
isPaused.set(isPausedRemember);
}
private static void updateTransforms(Group root) {
root.setScaleX(zoomFactor);
root.setScaleY(zoomFactor);
}
private void openCloseSettings(Group content) {
if (settings.isVisible()) {
content.setEffect(null);
settings.setVisible(false);
} else {
isPaused.set(true);
BoxBlur blur = new BoxBlur(20, 20, 2);
content.setEffect(blur);
settings.setVisible(true);
}
}
}
以下是使用水平分隔符时的外观:
这是垂直分隔符的外观:
这是使用两个分隔符时的外观:
我希望它看起来只像一条线(相同的 colored颜色 和宽度为1;在水平情况下,它看起来像1条灰线,1条白线和1条黑线,在垂直情况下,它看起来像1条灰线,1条白线和1条灰线,在两种情况下都是3的宽度).
我也注意到一件事,如果你算上第三条黑线,水平分隔符可能居中(也许),但垂直分隔符肯定不是.我不知道为什么.我对此感到困惑,因为我的场景集中在放置/添加到Group content
的元素上.
setStyle()
的答案几乎是完美的,但它有一个问题-阴影仍然存在,这就是为什么差距仍然存在: