GVKun编程网logo

css – JavaFX更改标签中一个单词的颜色(java设置标签颜色)

6

这篇文章主要围绕css–JavaFX更改标签中一个单词的颜色和java设置标签颜色展开,旨在为您提供一份详细的参考资料。我们将全面介绍css–JavaFX更改标签中一个单词的颜色的优缺点,解答java

这篇文章主要围绕css – JavaFX更改标签中一个单词的颜色java设置标签颜色展开,旨在为您提供一份详细的参考资料。我们将全面介绍css – JavaFX更改标签中一个单词的颜色的优缺点,解答java设置标签颜色的相关问题,同时也会为您带来C – 更改控制台应用程序中单个单词的文本颜色?、c# – 悬停并单击时更改标签的颜色、css – JavaFX中有两种颜色的背景?、css – 从句子到一个单词的过渡的实用方法。

本文目录一览:

css – JavaFX更改标签中一个单词的颜色(java设置标签颜色)

css – JavaFX更改标签中一个单词的颜色(java设置标签颜色)

我正在为桌面构建物业管理系统,目前我正在开发支付馈送功能.我希望我的标签中的付款金额以不同的颜色突出显示,以提高可读性.

我尝试了以下方法:

String datePaid  = "just Now";
Label amount = new Label("350");
Label label2 = new Label("paid £" + amount.getText() + " " + datePaid);

然后我尝试应用以下CSS

amount.setStyle("-fx-text-fill: #000 !important; -fx-highlight-text-fill: #000 !important; -fx-font-family: Arial");
    label2.setStyle("-fx-text-fill: #fff; -fx-font-size: 14px; -fx-translate-x: -36; -fx-translate-y: 24; -fx-font-family: 'Open Sans Light'");

我想通过声明!重要的是我会覆盖label2中应用的样式,而是所有文本呈现到#fff中的屏幕

我将如何实现预期的结果?

解决方法

请尝试使用Text inplace of Label作为金额.我希望它能解决这个问题.您也可以直接将颜色应用于文本.

Text amount = new Text("350");
amount.setFill(Color.RED);

C – 更改控制台应用程序中单个单词的文本颜色?

C – 更改控制台应用程序中单个单词的文本颜色?

我正在为学校开展一个项目,为了便于阅读,我希望能够以黄色文本的forms向用户显示返还给他们的金钱。 到目前为止,我已经find了一种方法来完成整个窗口的使用

system("COLOR 06");

但是这不是我想要的。 我在Windows中的控制台应用程序中运行,所以一些颜色将很好,以使其达到平等。 这可能在C做?

而不是调用系统命令,并假设您只处理Windows应用程序,则可以使用以下命令:

HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hStdOut,(FOREGROUND_BLUE | BACKGROUND_GREEN)); printf("I'm BLUE "); SetConsoleTextAttribute(hStdOut,(FOREGROUND_RED | BACKGROUND_GREEN)); printf("and I'm RED "); SetConsoleTextAttribute(hStdOut,(BACKGROUND_GREEN)); printf("and we both have green background!rn");

可用属性列表可在这里找到

#include <stdlib.h> ... int main() { system("COLOR #"); ...

# 分解:

0 =黑色,

1 =蓝色,

2 =绿色,

3 =水族馆

等等…

系统(“COLOR ##”)//这可以为控制台提供背景和前景色

我没有尝试过,但是可以尝试在打印之前将“颜色”设置为黄色显示,然后在打印之后将其重置为默认值,例如:

#include <stdlib.h> ... system("COLOR #"); printf("%s",money); system("COLOR #"); ...

c# – 悬停并单击时更改标签的颜色

c# – 悬停并单击时更改标签的颜色

我有关于改变标签外观的问题.这是截图:

enter image description here

这是你悬停鼠标时的颜色,我希望它像那样.
我想要的是它在我点击它时保持这种颜色.但由于我的鼠标控制,它不会像我想要的那样工作.

这是代码:

private void btnArchives_MouseEnter(object sender,EventArgs e)
    {
        lblArchives.BackColor = Color.FromArgb(9,18,28); //darkercolor
    }

   private void btnArchives_MouseLeave(object sender,EventArgs e)
   {
       lblArchives.BackColor = Color.FromArgb(15,34,53); //lightercolor
   }

我也试过鼠标悬停.虽然它看起来和鼠标一样.
底线是我希望颜色在悬停在它们上方时变为较暗的颜色,并在悬浮出来时变回较浅的颜色.但是当我点击它时,我也会保持深色.然后转回较浅的颜色然后我点击另一个按钮,其他按钮现在将变为更暗的颜色.谢谢!

编辑:我使用标签而不是按钮.我正在尝试下面的一些评论,非常感谢你.

解决方法

我使用这种技术,只是测试它,我认为它是你想要的.

Label clickedLabel;
    private void mouseEnter(object sender,EventArgs e)
    {
        Label theLabel = (Label)sender;
        if (theLabel != clickedLabel)
            theLabel.BackColor = Color.Red;
    }

    private void mouseLeave(object sender,EventArgs e)
    {
        Label theLabel = (Label)sender;
        if (theLabel != clickedLabel)
            theLabel.BackColor = Color.Yellow;
    }

    private void labelClick(object sender,EventArgs e)
    {
        setColor();//Calling this here so clickedLabel is still the old value
        Label theLabel = (Label)sender;
        clickedLabel = theLabel;
    }

    public void setColor()
    {
        if(clickedLabel != default(Label))
            clickedLabel.BackColor = Color.Yellow;
        //Resetting clicked label because another (or the same) was just clicked.
    }

说明:
这些事件仅设置为标签,因此我们可以执行(标签)发件人,这意味着激活事件的标签.我创建了一个Label clickedLabel变量并将其设置为单击的标签,一旦单击另一个变量,变量将更改并且检查将起作用.

关于这种方法的最好的事情是,你拥有多少标签并不重要,你永远不会将它们仅作为发件人引用.

css – JavaFX中有两种颜色的背景?

css – JavaFX中有两种颜色的背景?

在 JavaFX 2中,使用CSS,是否可以创建2种颜色的背景?想想例如一个高度为10像素的TableCell.我希望前2 px(垂直)为红色,其余8 px(垂直)应保持默认背景颜色.可以在JavaFX 2中使用CSS吗?怎么样?

例:

原始背景:

期望的结果:

(上面2个像素被红色取代)

感谢您提供任何暗示!

解决方法

我使用了一层简单的背景颜色来产生红色高光(类似于Stefan建议的解决方案).
/**
 * file: table.css
 *   Place in same directory as TableViewpropertyeditorWithCSS.java.
 *   Have your build system copy this file to your build output directory.
 **/

.highlighted-cell {
  -fx-text-fill: -fx-text-inner-color;
  -fx-background-color: firebrick,gainsboro;
  -fx-background-insets: 0,2 0 0 0;
}

对于像stackpane这样的标准区域,您真正需要做的就是应用上面的css(少了-fx-text-fill)来获得所需的结果.

以下是使用渐变定义颜色的另一种棘手方法:

-fx-background-color: 
  linear-gradient(
    from 0px 0px to 0px 2px,firebrick,firebrick 99%,gainsboro
  );

在下面的屏幕截图中,如果值为false,则突出显示值单元格(通过将高亮显示的单元格css类应用于它们).

突出显示单元格样式类切换逻辑:

public void updateItem(Object item,boolean empty) {
  super.updateItem(item,empty);
  if (empty) {
    ....
    getStyleClass().remove("highlighted-cell");
  } else {
    if (getItem() instanceof Boolean && (Boolean.FALSE.equals((Boolean) getItem()))) {
      getStyleClass().add("highlighted-cell");
    } else {
      getStyleClass().remove("highlighted-cell");
    }
    ...
  }
}

当突出显示的单元格样式类应用于标准表格单元格(在updateItem调用自定义单元格期间)时,它看起来很好,但确实有一些缺点.表着色方案非常微妙和复杂.它具有奇数/偶数值的突出显示,所选行的突出显示,所选悬停行的突出显示,聚焦行和单元格的突出显示等.此外,它具有上述所有内容的各种组合.只是直接在高亮单元类中设置背景颜色是一种蛮力的方式来实现你想要的东西,因为它没有考虑所有这些其他细微之处并且只是覆盖它们,因此使用这个突出显示的单元格无论临时css psuedo-class状态应用于什么样式,样式总是看起来一样.

它确实很好,但是一个更好的解决方案会根据伪类状态对突出显示的单元格进行不同的着色.这是一个相当棘手的事情,你可以浪费大量时间玩各种状态和CSS选择器组合,以尝试获得不错的变化突出显示.总而言之,对于这个例子来说,对我来说似乎不值得额外的努力,尽管它可能适合你.

测试程序(对此的长度和复杂性表示道歉,我更容易将样式突出显示逻辑集成到现有程序中):

import java.lang.reflect.*;
import java.util.logging.*;
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.util.Callback;
// click in the value column (a couple of times) to edit the value in the column.
// property editors are defined only for String and Boolean properties.
// change focus to something else to commit the edit.
public class TableViewpropertyeditorWithCSS extends Application {

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

  @Override
  public void start(Stage stage) {
    final Person aPerson = new Person("Fred",false,"Much Ado About nothing");
    final Label currentObjectValue = new Label(aPerson.toString());
    TableView<NamedProperty> table = new TableView();
    table.setEditable(true);
    table.setItems(createNamedProperties(aPerson));
    TableColumn<NamedProperty,String> nameCol = new TableColumn("Name");
    nameCol.setCellValueFactory(new PropertyValueFactory<NamedProperty,String>("name"));
    TableColumn<NamedProperty,Object> valueCol = new TableColumn("Value");
    valueCol.setCellValueFactory(new PropertyValueFactory<NamedProperty,Object>("value"));
    valueCol.setCellFactory(new Callback<TableColumn<NamedProperty,Object>,TableCell<NamedProperty,Object>>() {
      @Override
      public TableCell<NamedProperty,Object> call(TableColumn<NamedProperty,Object> param) {
        return new EditingCell();
      }
    });
    valueCol.setonEditCommit(
            new EventHandler<CellEditEvent<NamedProperty,Object>>() {
      @Override
      public void handle(CellEditEvent<NamedProperty,Object> t) {
        int row = t.getTablePosition().getRow();
        NamedProperty property = (NamedProperty) t.getTableView().getItems().get(row);
        property.setValue(t.getNewValue());
        currentObjectValue.setText(aPerson.toString());
      }
    });
    table.getColumns().setAll(nameCol,valueCol);
    table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    VBox layout = new VBox(10);
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
    layout.getChildren().setAll(
            currentObjectValue,table);
    VBox.setVgrow(table,Priority.ALWAYS);

    Scene scene = new Scene(layout,650,600);
    scene.getStylesheets().add(getClass().getResource("table.css").toExternalForm());
    stage.setScene(scene);
    stage.show();
  }

  private ObservableList<NamedProperty> createNamedProperties(Object object) {
    ObservableList<NamedProperty> properties = FXCollections.observableArrayList();
    for (Method method : object.getClass().getmethods()) {
      String name = method.getName();
      Class type = method.getReturnType();
      if (type.getName().endsWith("Property")) {
        try {
          properties.add(new NamedProperty(name,(Property) method.invoke(object)));
        } catch (illegalaccessexception | IllegalArgumentException | InvocationTargetException ex) {
          Logger.getLogger(TableViewpropertyeditorWithCSS.class.getName()).log(Level.SEVERE,null,ex);
        }
      }
    }
    return properties;
  }

  public class NamedProperty {

    public NamedProperty(String name,Property value) {
      nameProperty.set(name);
      valueProperty = value;
    }
    private StringProperty nameProperty = new SimpleStringproperty();

    public StringProperty nameproperty() {
      return nameProperty;
    }

    public StringProperty getName() {
      return nameProperty;
    }

    public void setName(String name) {
      nameProperty.set(name);
    }
    private Property valueProperty;

    public Property valueproperty() {
      return valueProperty;
    }

    public Object getValue() {
      return valueProperty.getValue();
    }

    public void setValue(Object value) {
      valueProperty.setValue(value);
    }
  }

  public class Person {

    private final SimpleStringProperty firstName;
    private final SimpleBooleanProperty married;
    private final SimpleBooleanProperty hasChildren;
    private final SimpleStringProperty favoriteMovie;

    private Person(String firstName,Boolean isMarried,Boolean hasChildren,String favoriteMovie) {
      this.firstName = new SimpleStringProperty(firstName);
      this.married = new SimpleBooleanProperty(isMarried);
      this.hasChildren = new SimpleBooleanProperty(hasChildren);
      this.favoriteMovie = new SimpleStringProperty(favoriteMovie);
    }

    public SimpleStringProperty firstNameproperty() {
      return firstName;
    }

    public SimpleBooleanProperty marriedproperty() {
      return married;
    }

    public SimpleBooleanProperty hasChildrenproperty() {
      return hasChildren;
    }

    public SimpleStringProperty favoriteMovieproperty() {
      return favoriteMovie;
    }

    public String getFirstName() {
      return firstName.get();
    }

    public void setFirstName(String fName) {
      firstName.set(fName);
    }

    public Boolean getMarried() {
      return married.get();
    }

    public void setMarried(Boolean isMarried) {
      married.set(isMarried);
    }

    public Boolean getHasChildren() {
      return hasChildren.get();
    }

    public void setHasChildren(Boolean hasChildren) {
      this.hasChildren.set(hasChildren);
    }

    public String getFavoriteMovie() {
      return favoriteMovie.get();
    }

    public void setFavoriteMovie(String movie) {
      favoriteMovie.set(movie);
    }

    @Override
    public String toString() {
      return firstName.getValue() + ",isMarried? " + married.getValue() + ",hasChildren? " + hasChildren.getValue() + ",favoriteMovie: " + favoriteMovie.get();
    }
  }

  class EditingCell extends TableCell<NamedProperty,Object> {

    private TextField textField;
    private CheckBox checkBox;

    public EditingCell() {
    }

    @Override
    public void startEdit() {
      if (!isEmpty()) {
        super.startEdit();
        if (getItem() instanceof Boolean) {
          createCheckBox();
          setText(null);
          setGraphic(checkBox);
        } else {
          createTextField();
          setText(null);
          setGraphic(textField);
          textField.selectAll();
        }
      }
    }

    @Override
    public void cancelEdit() {
      super.cancelEdit();
      if (getItem() instanceof Boolean) {
        setText(getItem().toString());
      } else {
        setText((String) getItem());
      }
      setGraphic(null);
    }

    @Override
    public void updateItem(Object item,boolean empty) {
      super.updateItem(item,empty);
      if (empty) {
        setText(null);
        setGraphic(null);
        getStyleClass().remove("highlighted-cell");
      } else {
        if (getItem() instanceof Boolean && (Boolean.FALSE.equals((Boolean) getItem()))) {
          getStyleClass().add("highlighted-cell");
        } else {
          getStyleClass().remove("highlighted-cell");
        }
        if (isEditing()) {
          if (getItem() instanceof Boolean) {
            if (checkBox != null) {
              checkBox.setSelected(getBoolean());
            }
            setText(null);
            setGraphic(checkBox);
          } else {
            if (textField != null) {
              textField.setText(getString());
            }
            setText(null);
            setGraphic(textField);
          }
        } else {
          setText(getString());
          setGraphic(null);
        }
      }
    }

    private void createTextField() {
      textField = new TextField(getString());
      textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
      textField.focusedproperty().addListener(new changelistener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable,Boolean oldValue,Boolean newValue) {
          if (!newValue) {
            commitEdit(textField.getText());
          }
        }
      });
    }

    private void createCheckBox() {
      checkBox = new CheckBox();
      checkBox.setSelected(getBoolean());
      checkBox.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
      checkBox.focusedproperty().addListener(new changelistener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable,Boolean newValue) {
          if (!newValue) {
            commitEdit(checkBox.isSelected());
          }
        }
      });
    }

    private String getString() {
      return getItem() == null ? "" : getItem().toString();
    }

    private Boolean getBoolean() {
      return getItem() == null ? false : (Boolean) getItem();
    }
  }
}

css – 从句子到一个单词的过渡

css – 从句子到一个单词的过渡

我想要实现的是为元素宽度设置几个单词的宽度,这是在元素中心悬停单词的方式,其余部分顺利地超出边界.我还会在HTML中尽可能保持清晰,而不是使用固定像素数量的边距/宽度来定位元素.

我脑子里的一张非常糟糕的草图在这里:

* {
  transition: all 1.5s;
}

div {
  min-width: 150px;
  width: 30%;
  height: 25px;
  line-height: 25px;
  background: orange;
  text-align: center;
  overflow: hidden;
}

div:hover {
  background: white;
  word-spacing: 300px;
}

a:hover::after,a:hover::before {
  content: ' ';
}
irst

悬停时的每个单词都应该居中(当其他单词消失时,可能不会出现那些“跳跃”现在可见).你有什么想法?我很确定我尝试使用字间距的方式是错误的.

最佳答案
问题是当增加单词间距时,文本会转到新行并创建这个跳转的东西.所以你可以添加white-space:Nowrap,你也可以使用padding-left推送文本并将其放在中心:

div {
  min-width: 150px;
  width: 180px;
  height: 25px;
  line-height: 25px;
  background: orange;
  text-align: center;
  overflow: hidden;
  transition: all 0.5s;
  Box-sizing: border-Box;
  white-space: Nowrap;
}

div:hover {
  word-spacing: 80px;
  padding-left: 80px;
  background: white;
}
irst

实际上,单词间距应用于div,因此您不能将鼠标悬停在单词上.在第一个单词上应用这种技术也更容易,因为第二个单词将被溢出隐藏,但我不确定如何使用单词间距对第二个单词做同样的操作.

这是关于如何在没有字间距的情况下如何做的另一个想法.我使用了一些填充动画和伪元素来隐藏第一个单词时悬停第二个单词.

div {
  min-width: 150px;
  width: 180px;
  height: 25px;
  line-height: 25px;
  background: orange;
  text-align: center;
  overflow: hidden;
  transition: all 0.5s;
  Box-sizing: border-Box;
  white-space: Nowrap;
}

.afirst,.asecond {
  position:relative;
  display: inline-block;
  transition: all 0.5s;
}

.afirst:hover {
  padding: 0 44%;
  background: white;
}
.asecond:hover {
  padding: 0 50% 0 0;
  background: white;
}
.asecond:hover::before {
  content:" ";
  position:absolute;
  left:-50%;
  width:50%;
  top:0;
  bottom:0;
  z-index:99;
  background:#fff;
}
irst

我认为你可以通过使用:左边的元素和右边的元素之后隐藏其他所有内容来概括这个解决方案.

这是一个包含多个单词的示例(但没有正确地给出中心对齐,仍然需要改进):

div {
  min-width: 150px;
  height: 25px;
  line-height: 25px;
  background: orange;
  text-align: center;
  overflow: hidden;
  transition: all 0.5s;
  Box-sizing: border-Box;
  white-space: Nowrap;
}

.word {
  position: relative;
  display: inline-block;
  transition: all 0.5s;
}

.word:hover {
  background: white;
  padding: 0 40%;
}

.word:hover::before {
  content: " ";
  position: absolute;
  left: -50%;
  width: 50%;
  top: 0;
  bottom: 0;
  z-index: 99;
  background: #fff;
}

.word:hover::after {
  content: " ";
  position: absolute;
  right: -50%;
  width: 50%;
  top: 0;
  bottom: 0;
  z-index: 99;
  background: #fff;
}
irst

另一种解决方案具有完美的居中效果,但动画效果较少,换句

div {
  position:relative;
  height: 25px;
  width:500px;
  margin:0 auto;
  line-height: 25px;
  background: orange;
  text-align: center;
  overflow: hidden;
  transition: all 0.5s;
  Box-sizing: border-Box;
  white-space: Nowrap;
}

.word {
  position: relative;
  z-index:9;
  display: inline-block;
  transition: all 0.5s;
}

.word:hover {
  position:absolute;
  right:0;
  left:0;
  top:0;
  bottom:0;
  text-align:center;
  background: white;
  z-index:99;
}
irst

关于css – JavaFX更改标签中一个单词的颜色java设置标签颜色的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于C – 更改控制台应用程序中单个单词的文本颜色?、c# – 悬停并单击时更改标签的颜色、css – JavaFX中有两种颜色的背景?、css – 从句子到一个单词的过渡的相关信息,请在本站寻找。

本文标签: