JavaFX的 - 如何让标签,按钮等的背景颜色 [英] JavaFX - how to get background color of Tab, Button, etc

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

问题描述

问题描述:我不能让物体的背景JavaFX中。我说的不是造型,而是普通节点如按钮,标签等。我不知道如何访问到他们的背景颜色。

我要什么?我正在开发的IDE,我想与该用户要打开和程序文件集合中已存在的文件运行标签颜色动画。做这个动画之前,我想读原来的标签背景颜色和色彩在动画结束返回标签。此外,我想回去悬停属性,消失的时候我在动画中设置一些颜色,他们从来没有得到背部。所有的颜色我设立的CSS文件,我不想改变它。

我的问题:如何获取和编程方式设置节点的颜色?或如何做彩动画与保存原有属性和动画的结尾得到这个属性恢复?

一个简单的例子:

sample.fxml

 <?XML版本=1.0编码=UTF-8&GT?;<进口的java.lang *过夜。?;
<进口javafx.scene.control *过夜。?;
<进口javafx.scene.layout *过夜。?;< TabPane了maxHeight = - 无限了maxWidth = - 无限了minHeight = - 无限了minWidth = - 无限prefHeight =480.0prefWidth =600.0样式=@ style.css文件tabClos​​ingPolicy =不可用的xmlns =http://javafx.com/javafx/8的xmlns:FX =http://javafx.com/fxml/1>
  <&标签GT;
    <标签的文本=样品标签1>
      <内容>
        < AnchorPane了minHeight =0.0了minWidth =0.0prefHeight =180.0prefWidth =200.0/>
      < /内容>
    < /标签>
    <标签的文本=样品标签2>
      <内容>
        < AnchorPane了minHeight =0.0了minWidth =0.0prefHeight =180.0prefWidth =200.0/>
      < /内容>
    < /标签>
      <标签的文本=样品标签3>
        <内容>
          < AnchorPane了minHeight =0.0了minWidth =0.0prefHeight =180.0prefWidth =200.0/>
        < /内容>
      < /标签>
  < /标签>
< / TabPane>

styles.css的

  .TAB {
-fx背景色:粉色;}.TAB:悬停{
-fx背景色:红色;}.TAB:选择{
-fx背景色:黄色;}


解决方案

据我所知,还有就是公共API中没有办法确定当前正在使用的背景颜色为地区(包括在控制)(除非你知道它是不是由一个内联样式设置,在这种情况下,你可以分析的结果的getStyle()或以的setBackground的调用(...))。但是,我认为没有理由你会想这一点;颜色将恢复到在CSS文件中定义的,如果你删除任何内嵌样式或背景属性。

下面是该幻灯片作为任务的进展,其中的背景颜色是由线性渐变(通过内联样式)中设置一个简单的例子:

 进口javafx.application.Application;
进口javafx.beans.binding.Bindings;
进口javafx.beans.binding.IntegerBinding;
进口javafx.concurrent.Task;
进口javafx.scene.Scene;
进口javafx.scene.control.Button;
进口javafx.scene.control.Tab;
进口javafx.scene.control.TabPane;
进口javafx.scene.layout.StackPane;
进口javafx.stage.Stage;公共类ColoredTabDemo扩展应用{    私人诠释tabCount;    @覆盖
    公共无效启动(阶段primaryStage){
        TabPane tabPane =新TabPane();
        对(INT I = 0; I&下; 4;我++){
            。tabPane.getTabs()加(CREATETAB());
        }
        一幕一幕=新场景(tabPane,600,400);
        。scene.getStylesheets()加(有色标签,demo.css);
        primaryStage.setScene(场景);
        primaryStage.show();
    }    私人标签CREATETAB(){
        TAB键的话=新标签(标签+(++ tabCount));
        Button按钮=新按钮(加载文件...);        button.setOnAction(E - > {
            任务<无效>任务=新任务<无效>(){
                @覆盖
                公共无效()调用抛出异常{                    //模拟负载:
                    的for(int i = 1; I< = 500;我++){
                        的UpdateProgress(I,500);
                        视频下载(20);
                    }                    返回null;                }
            };            IntegerBinding progressAsPercent = Bindings.createIntegerBinding(() - >
                (中间体)(task.getProgress()* 100),task.progressProperty());            tab.styleProperty()绑定(Bindings.format( - FX-背景色:
                    +线性渐变(右,-fx-口音0 %%,-fx-口音%d个%%,-fx背景%1 $ d个%%,-fx背景100 %%);,
                    progressAsPercent));            button.setDisable(真);            task.setOnSucceeded(EVT - > {
                tab.styleProperty()解除()。
                tab.setStyle();
                button.setDisable(假);
            });            新的线程(任务)。开始();
        });        tab.setContent(新StackPane(按钮));        返回标签;
    }    公共静态无效的主要(字串[] args){
        启动(参数);
    }
}

有色制表demo.css几乎完全一样的,你贴出来,但使用的是看向上的颜色,而不是设置 -fx背景色直接:

  .TAB {
    -fx背景色:-fx背景;
    -fx背景:粉红色;
}.TAB:悬停{
    -fx背景:红色;
}.TAB:选择{
    -fx背景:黄色;
}

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的 - 如何让标签,按钮等的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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