JavaFX - 如何获取Tab,Button等的背景颜色 [英] JavaFX - how to get background color of Tab, Button, etc

查看:494
本文介绍了JavaFX - 如何获取Tab,Button等的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题描述:我无法在JavaFX中获取对象的背景。我不是指形状,而是普通的节点,如按钮,Tabs和其他。我不知道如何访问他们的背景颜色。



我想要什么?我正在开发IDE,选项卡上的用户要打开并且已经存在于程序文件集合中的文件。在做这个动画之前,我想读取原始标签背景颜色,并且该颜色在动画结束时返回到标签。此外,我想回到 hover 选择属性,当我在动画中设置一些颜色时消失,背部。所有颜色我在CSS文件中设置,我不想改变它。



我的问题:如何获取和设置节点颜色?

一个简单的例子:





sample.fxml

 <?xml version =1.0encoding =UTF-8? 

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

< TabPane maxHeight = - InfinitymaxWidth = - InfinityminHeight = - InfinityminWidth = - InfinityprefHeight =480.0prefWidth =600.0stylesheets =@ style .csstabClos​​ingPolicy =UNAVAILABLExmlns =http://javafx.com/javafx/8xmlns:fx =http://javafx.com/fxml/1>
< tabs>
< Tab text =Sample tab 1>
< content>
< AnchorPane minHeight =0.0minWidth =0.0prefHeight =180.0prefWidth =200.0/>
< / content>
< / Tab>
< Tab text =Sample tab 2>
< content>
< AnchorPane minHeight =0.0minWidth =0.0prefHeight =180.0prefWidth =200.0/>
< / content>
< / Tab>
< Tab text =Sample tab 3>
< content>
< AnchorPane minHeight =0.0minWidth =0.0prefHeight =180.0prefWidth =200.0/>
< / content>
< / Tab>
< / tabs>
< / TabPane>

styles.css


$ b b

  .tab {
-fx-background-color:pink;}

.tab:hover {
-fx- background-color:red;}

.tab:selected {
-fx-background-color:yellow;}


解决方案

据我所知,公共API中没有办法确定当前用作背景颜色的 Region (包括 Control )(除非你知道它是由内联样式设置的,可以解析 getStyle()的结果,或者通过调用 setBackground(...))。但我看不到你会想要的理由;如果您删除任何内联样式或背景属性,颜色将恢复为css文件中定义的颜色。



一个简单的示例,其中背景颜色由线性渐变(通过内联样式)设置,其随着任务进行而滑动:

  import javafx.application.Application; 
import javafx.beans.binding.Bindings;
import javafx.beans.binding.IntegerBinding;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class ColoredTabDemo extends应用程序{

private int tabCount;

@Override
public void start(Stage primaryStage){
TabPane tabPane = new TabPane();
for(int i = 0; i <4; i ++){
tabPane.getTabs()。add(createTab());
}
场景scene = new Scene(tabPane,600,400);
scene.getStylesheets()。add(colored-tab-demo.css);
primaryStage.setScene(scene);
primaryStage.show();
}

private Tab createTab(){
Tab tab = new Tab(Tab+(++ tabCount));
按钮button = new按钮(加载文件...);

button.setOnAction(e - > {
Task< Void> task = new Task< Void>(){
@Override
public Void call throws Exception {

//模拟加载:
for(int i = 1; i <= 500; i ++){
updateProgress(i,500);
Thread.sleep(20);
}

return null;

}
};

Integer绑定progressAsPercent = Bindings.createIntegerBinding(() - >
(int)(task.getProgress()* 100),task.progressProperty());

tab.styleProperty .format( - fx-background-color:
+linear-gradient(to right,-fx-accent 0 %%,-fx-accent%d %%,-fx-background%1 $ d %%,-fx-background 100 %%);,
progressAsPercent));

button.setDisable(true);

task.setOnSucceeded(evt - > {
tab.styleProperty()。unbind();
tab.setStyle();
button.setDisable (false);
});

new Thread(task).start();
});

tab.setContent(new StackPane(button));

返回标签;
}

public static void main(String [] args){
launch(args);
}
}

colored-tab-demo.css几乎是与您张贴的相同,但使用查找的颜色,而不是直接设置 -fx-background-color

  .tab {
-fx-background-color:-fx-background;
-fx-background:pink;
}

.tab:hover {
-fx-background:red;
}

.tab:selected {
-fx-background:yellow;
}


Problem description: I can't get background of object in JavaFX. I don't mean Shapes, but normal Nodes like Buttons, Tabs and others. I don't know how to access to theirs background color.

What I want? I am developing IDE and I want to run Color animation on tab with file that user want to open and is already existing in program file collection. Before doing this animation I want to read original tab background color and that color is returned to tab at the end of animation. Also I want to get back hover and selected properties, which disappear when I set some color in animation and they never get back. All colors I am setting up in CSS file and I don't want to change it.

My question: How to get and set programmatically Node color? Or how to do color animation with save original properties and at the end of animation get this properties back?

One short example:

sample.fxml

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

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

<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="600.0" stylesheets="@style.css" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
  <tabs>
    <Tab text="Sample tab 1">
      <content>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
      </content>
    </Tab>
    <Tab text="Sample tab 2">
      <content>
        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
      </content>
    </Tab>
      <Tab text="Sample tab 3">
        <content>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
        </content>
      </Tab>
  </tabs>
</TabPane>

styles.css

.tab{
-fx-background-color:   pink;}

.tab:hover{
-fx-background-color:   red;}

.tab:selected{
-fx-background-color:   yellow;}

解决方案

As far as I know, there is no way in the public API to determine what is being currently used as the background color for a Region (including for a Control) (unless you know it is either set by an inline style, in which case you can parse the result of getStyle() or by a call to setBackground(...)). But I see no reason you would want this; the color will revert to that defined in the css file if you remove any inline styles or background property.

Here's a simple example where the background color is set by a linear gradient (via an inline style) which slides as a task progresses:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.IntegerBinding;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class ColoredTabDemo extends Application {

    private int tabCount ;

    @Override
    public void start(Stage primaryStage) {
        TabPane tabPane = new TabPane();
        for (int i = 0; i < 4; i++) {
            tabPane.getTabs().add(createTab());
        }
        Scene scene = new Scene(tabPane, 600, 400);
        scene.getStylesheets().add("colored-tab-demo.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Tab createTab() {
        Tab tab = new Tab("Tab "+(++tabCount));
        Button button = new Button("Load file...");

        button.setOnAction(e -> {
            Task<Void> task = new Task<Void>() {
                @Override
                public Void call() throws Exception {

                    // simulate loading:
                    for (int i=1; i <= 500; i++) {
                        updateProgress(i, 500);
                        Thread.sleep(20);
                    }

                    return null ;

                }
            };

            IntegerBinding progressAsPercent = Bindings.createIntegerBinding(() -> 
                (int) (task.getProgress() * 100), task.progressProperty());

            tab.styleProperty().bind(Bindings.format("-fx-background-color: "
                    + "linear-gradient(to right, -fx-accent 0%%, -fx-accent %d%%, -fx-background %1$d%%, -fx-background 100%%);", 
                    progressAsPercent));

            button.setDisable(true);

            task.setOnSucceeded(evt -> {
                tab.styleProperty().unbind();
                tab.setStyle("");
                button.setDisable(false);
            });

            new Thread(task).start();
        });

        tab.setContent(new StackPane(button));

        return tab ;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

colored-tab-demo.css is almost exactly the same as you posted, but using a looked-up color instead of setting -fx-background-color directly:

.tab{
    -fx-background-color:   -fx-background;
    -fx-background: pink ;
}

.tab:hover{
    -fx-background:   red;
}

.tab:selected{
    -fx-background:   yellow;
}

这篇关于JavaFX - 如何获取Tab,Button等的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆