Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对FXML中的不同按钮使用相同的事件_Java_Events_Javafx_Fxml - Fatal编程技术网

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

Java 如何对FXML中的不同按钮使用相同的事件,java,events,javafx,fxml,Java,Events,Javafx,Fxml,我所说的事件很简单:当用户将光标移动到按钮所在的位置时,它将变为不同的颜色。一旦退出,它将变回原始颜色。我已经实现了这一点,我要问的是如何编程每个按钮以使用相同的两个事件?请记住,每个按钮的行为都不同,但每个按钮的此事件都相同 这是相关代码 我的FXML控制器类 package millionairetriviagame; import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXML; import

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

这是相关代码

我的FXML控制器类

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;
}

当鼠标悬停在选定节点上时,它处于活动状态。好的,我得到了一个javax.xml.stream.XMLStreamException:ParseError at[row,col]:[1,10]再次感谢您的帮助