有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何编辑来自不同类的主类中标签的颜色?

所以我是一名java程序员初学者,我正在制作一个小应用程序,在这里我可以输入我的学校分数并查看我的总分。我一共有2个类(1个用于javafx布局,1个用于我的方法),我正试图用Methods类中的方法更改主类中的标签(.setTextFill)的颜色

我尝试过为main类创建一个对象,但这会产生一个错误:java。朗,反思一下。调用TargetException

public void start(Stage primaryStage)throws Exception{
  totalPercent = new Label(Methods.getTotalPercent());
  totalPercent.setTextFill(Color.web("#4ecf17"));
}

//next code is in a different class
public void setPercentColor(){
    if(percent <  50){
        totalPercent.setTextFill(Color.web("#ff0000"));
    }else if(percent >=  50 && percent <  60){
        totalPercent.setTextFill(Color.web("#ff7700"));
    }else if (percent >=  60 && percent <  100){
        totalPercent.setTextFill(Color.web("#59ff00"));
    }else{
        totalPercent.setTextFill(Color.web("#000000"));
    }
}

所以基本上我不知道如何用另一个类中的方法改变标签“totalPercent”的颜色


共 (2) 个答案

  1. # 1 楼答案

    考虑到setPercentColor方法,类的设计做得不好:它控制由另一个类管理的某些视图的表示的细节

    我建议重新设计Methods以提供一个属性,并让另一个类决定如何处理这个值

    在这种情况下,您可以添加ReadOnlyIntegerProperty percent而不是使用字段。这样,另一个类就可以使用绑定/侦听器来更新视图,Methods唯一需要担心的就是更新属性

    Methods

    // private int percent;
    private final ReadOnlyIntegerWrapper percent = new ReadOnlyIntegerWrapper();
    
    public ReadOnlyIntegerProperty percentProperty() {
        return percent.getReadOnlyProperty();
    }
    
    public int getPercent() {
        return percent.get();
    }
    
    ...
    // percent = i
    percent.set(i); // replace assinment to int field with setting the property
    

    其他类别

    public void start(Stage primaryStage)throws Exception{
        totalPercent = new Label();
    
        totalPercent.textProperty().bind(Methods.percentProperty().asString());
    
        final Color color560 = Color.web("#ff7700");
        final Color color100 = Color.web("#59ff00");
        totalPercent.textFillProperty().bind(Bindings.createObjectBinding(() -> {
                  int percent = Methods.getPercent();
                  if(percent <  50) {
                      return Color.RED;
                  } else if(percent < 60) {
                      return color60;
                  }else if (percent < 100){
                      return color100;
                  } else {
                      return Color.BLACK;
                  }
              }, Methods.percentProperty()));
    }
    

    顺便说一句:我建议为Methods类选择一个更有意义的名称:大多数类都包含方法实现,因此对于阅读代码的人来说,该名称基本上不包含任何有价值的信息

  2. # 2 楼答案

    问得好。我建议将totalPercent标签传递给新类,以便修改它。例如

    public class FirstClass {
        Label totalPercent;
        SecondClass secondClass = new SecondClass();
    
        public void start(Stage primaryStage) throws Exception {
          totalPercent = new Label(Methods.getTotalPercent());
          totalPercent.setTextFill(Color.web("#4ecf17"));
          secondClass.setTotalPercent(totalPercent);
        }
    }
    
    public class SecondClass {
        Label totalPercent;
    
        //next code is in a different class
        public void setPercentColor() {
            if(percent <  50){
                totalPercent.setTextFill(Color.web("#ff0000"));
            }else if(percent >=  50 && percent <  60){
                totalPercent.setTextFill(Color.web("#ff7700"));
            }else if (percent >=  60 && percent <  100){
                totalPercent.setTextFill(Color.web("#59ff00"));
            }else{
                totalPercent.setTextFill(Color.web("#000000"));
            }
        }
    
        public void setTotalPercent(Label totalPercent) {
            this.totalPercent = totalPercent;
        }
    }
    

    最后,作为我自己的观点。我建议对变量的命名更具描述性。例如,将标签变量命名为totalPercent,但我建议totalPercentLabel。我还建议setPercentColor()类似于setTotalPercentLabelColor()。这是一个更具描述性的方法,当我单独看这个方法时,它立即清楚地表明了发生了什么。这个方法是描述性的,我知道你在对totalPercentLabel做什么。现在我不确定setPercentColor()在做什么,也不确定totalPercent代表什么对象,除非我查看代码的其他部分