java - 如何对 FXML 中的不同按钮使用相同的事件

标签 java events javafx fxml

我所说的事件很简单:当用户将光标移动到按钮所在的位置时,它将更改为不同的颜色。一旦退出,它就会变回原来的颜色。我已经实现了这个,我所问的是如何对每个按钮进行编程以使用相同的两个事件?请记住,每个按钮的行为都会有所不同,但每个按钮的此事件都是相同的。

这是相关代码

我的 FXML Controller 类

package millionairetriviagame;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class MenulayoutFXMLController implements Initializable
{   
    @FXML private Button playButton;

    @Override
    public void initialize(URL url, ResourceBundle rb) 
    {
         Media gameIntroTheme = new Media(getClass().getResource("/millionairetriviagame/AudioFiles/GameIntroTheme.mp3").toExternalForm());
         MediaPlayer mediaPlayer = new MediaPlayer(gameIntroTheme);
         mediaPlayer.setAutoPlay(true);
         mediaPlayer.setVolume(0.1);
    }     

    @FXML private void changeNewColor(MouseEvent event)
    {
        playButton.setStyle("-fx-background-color: #0f69b4;");
    }

    @FXML private void backToOldColor(MouseEvent event)
    {
        playButton.setStyle("-fx-background-color: #346699;");
    }
}

我的 FXML

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.media.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import java.net.URL?>

<StackPane fx:id="MainMenu" xmlns:fx="http://javafx.com/fxml/1" fx:controller="millionairetriviagame.MenulayoutFXMLController">
    <stylesheets>
       <URL value="@ButtonLayout.css"/>
   </stylesheets>
     <children>      
         <ImageView>
             <image>
                <Image url="@ImageFiles/BlueBackgroundColor.jpg"/>
            </image>
        </ImageView>
        <VBox fx:id="MainMenuLayout"  spacing="20"  alignment="TOP_CENTER" > 
            <children>
                 <ImageView>
                     <image>
                        <Image url="@ImageFiles/MillionaireLogo1.png"/>
                    </image>
                </ImageView>
                 <Button fx:id="playButton" onMouseEntered="#changeNewColor" onMouseExited="#backToOldColor"  prefWidth="200" prefHeight="30" text="Play" styleClass="ButtonLayout">
                     <shape>
                        <javafx.scene.shape.Rectangle  width="200" height="30" arcHeight="30" arcWidth="30" />  
                    </shape>
                </Button>              
                 <Button fx:id="optionButton" prefWidth="200" prefHeight="30" text="Option" styleClass="ButtonLayout">
                     <shape>
                        <javafx.scene.shape.Rectangle  width="200" height="30" arcHeight="30" arcWidth="30" />  
                    </shape>
                </Button>
                 <Button fx:id="aboutTheGameButton" prefWidth="200" prefHeight="30" text="How to Play" styleClass="ButtonLayout">
                     <shape>
                        <javafx.scene.shape.Rectangle  width="200" height="30" arcHeight="30" arcWidth="30" />  
                    </shape>
                </Button>
                 <Button fx:id="exitButton"  prefWidth="200" prefHeight="30" text="Exit" styleClass="ButtonLayout">
                     <shape>
                        <javafx.scene.shape.Rectangle  width="200" height="30" arcHeight="30" arcWidth="30" />  
                    </shape>
                </Button>
            </children>
        </VBox>   
    </children> 
</StackPane>

最佳答案

只需使用 CSS 即可完成此操作。您根本不需要任何事件处理:

.button {
    -fx-background-color: #346699;
}
.button:hover {
    -fx-background-color: #0f69b4;
}

关于java - 如何对 FXML 中的不同按钮使用相同的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29573506/

相关文章:

java - org.apache.xerces.dom.DeferredDocumentImpl 与 org.dom4j.Document 不兼容

c# - 防止 C# 事件对 VB6 代码造成死锁

java - 为什么 JavaFX 在 NetBeans 之外的工作方式不同?

java - TornadoFX 中设置 PrimaryStage 或 Scene 属性的方法

javafx - 如何在 JavaFX 中设置默认关闭操作?

带有 MultipartFile 的 Java jackson

java - android studio gradle更新后,使用Fuel库无法解析方法 'get(java.lang.String)'

java - 将 GUI 功能添加到编写 XML 文件的 Java 程序中

events - JavaFX - 为什么我的 FileChooser 允许我访问原始舞台?

c# - 在 Visual Studio 2012 中,页面控件和事件转到代码隐藏的哪个位置?