【问题标题】:How do I change the color of an anchorpane from the controller class?如何从控制器类更改锚窗格的颜色?
【发布时间】:2019-03-26 05:44:05
【问题描述】:

对于 GUI,我通过为每个“按钮”使用锚窗格来制作类似于 Spotify 的最小化、最大化、关闭按钮。我通过场景构建器创建了按钮,并通过 fxml 将它们加载到类中。我不知道如何在控制器类中调用锚窗格的特定实例来更改鼠标进入或退出时的背景颜色。

public static Stage primaryStage;

@Override

public void start(Stage primaryStage) throws Exception
{

    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

    primaryStage.setScene(new Scene(root, 1280, 800));

    ...

    this.primaryStage = primaryStage;

    etc. 

是如何设置 UI 类的

我希望鼠标进入边界时锚窗格的颜色会发生变化,但到目前为止我不知道如何调用它。

【问题讨论】:

  • 或许为该锚窗格添加一个事件处理程序?

标签: java javafx fxml scenebuilder


【解决方案1】:

解决了,必须为锚窗格设置一个 fxid,然后在 @FXML 之后在控制器类中启动它。

有点像这样:

@FXML
Anchorpane someButton;

@FXML
public void makeButtonWhite(MouseEvent event)
{
    someButton.setStyle("-fx-background-color: #ffffff");
}

【讨论】:

    【解决方案2】:

    恕我直言,最方便的方法是使用样式表来分配背景。

    <AnchorPane xmlns:fx="http://javafx.com/fxml/1" stylesheets="@style.css">
        <children>
           <AnchorPane prefWidth="30" prefHeight="20" styleClass="my-button"/>
        </children>
    </AnchorPane>
    

    style.css(与 fxml 相同的目录)

    /* default style */
    .my-button {
        -fx-background-color: blue;
    }
    
    /* style when mouse is inside the region */
    .my-button:hover {
        -fx-background-color: red;
    }
    

    这让您可以轻松地将样式添加到多个Regions;你只需要添加样式类就可以了(styleClass="my-button")。

    【讨论】:

      【解决方案3】:

      你需要的代码必须是起始类

      public class Class extends Application {
      
      public static void main(String[] args) {
          launch(args);
      }
      
      @Override
      public void start(Stage primaryStage) {
          try {
              // Load root layout from fxml file.
              FXMLLoader loader = new FXMLLoader();
      
              loader.setLocation(this.getClass().getResource("/Folder/File.fxml"));
              loader.setController(yourControllerClass());
              //only if you do something with the controller class afterwards
              YourControllerClass controller = loader.setController();
      
              Parent parent = loader.load();
      
              // Show the scene containing the root layout.
              Scene scene = new Scene(parent);
              primaryStage.setScene(scene);
              primaryStage.show();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      

      }

      【讨论】:

        【解决方案4】:

        您知道scenebuilder中的代码方法,您可以在鼠标进入和退出时设置按钮、标签等。然后,例如,ID为“closeButton”的按钮和OnMouseEntered“closeButtonOnEntered”以及OnMouseExited“closeButtonOnExited” " 这将是您需要的代码

        public yourcontrollerclass {
             @FXML
             Button closeButton;
        
             @FXML
             private void closeButtonOnEntered() {
                   //sets button red
                   button.setStyle("-fx-background-color: #ff0000");
             }
        
             @FXML
             private void closeButtonOnExited() {
                  //sets button your first color
                  button.setStyle("-fx-background-color: transparent");
             }
        }
        

        sceneBuilder 中的几乎所有东西都可以做到这一点

        我希望我能帮助你(对不起我的英语不好)

        【讨论】:

        • 如果你想使用这个,你需要我的另一篇文章!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-30
        • 2020-05-26
        • 2015-10-12
        • 1970-01-01
        • 1970-01-01
        • 2016-06-06
        • 1970-01-01
        相关资源
        最近更新 更多