更新: 我希望媒体播放器是静态的,但如果我制作是静态的,它不起作用。 请注意,我想媒体播放静态的原因是,我想从其他类访问它(行注释)。 这是我的代码:获取文件资源的静态方法
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.net.URL;
public class Main extends Application {
static boolean isSoundOn = false;
static double soundVolume = .5;
MediaPlayer mediaPlayer = new MediaPlayer(new Media(Main.class.getResource("song.mp3").toString()));
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
mediaPlayer.play();
primaryStage.setTitle("duet by what");
// primaryStage.setFullScreen(true);
//Group gamePaused = new Group();
//Scene _gamePaused = new Scene(gamePaused, 1200, 700);
//Group gameOver = new Group();
//Scene _gameOver = new Scene(gameOver, 1200, 700);
//Group game = new Group();
//Scene _game = new Scene(game, 1200, 700);
GUI gui = new GUI();
primaryStage.setScene(gui.getMainMenu().getScene());
primaryStage.show();
}
}
class GUI {
private MainMenu mainMenu = new MainMenu();
public class MainMenu {
private Scene scene;
private MainMenu() {
VBox vBox = new VBox();
scene = new Scene(vBox, 400, 500);
scene.getStylesheets().add("stylesheet.css");
Label info = new Label(
"welcome the the what version\n" +
"of the well known Duet game!\n\n" +
"press \"I wanna play!\" to begin the game.\n\n" +
"please note that you can change\n" +
"the sound settings.");
info.setId("info");
vBox.getChildren().add(info);
Button startGame = new Button("i wanna play right now!");
startGame.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("game started!");
}
});
vBox.getChildren().add(startGame);
Label highScore = new Label("__highScore should be added here__");
highScore.setId("highScore");
vBox.getChildren().add(highScore);
Button quitGame = new Button("get me out of this game!");
quitGame.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("game quitted!");
}
});
vBox.getChildren().add(quitGame);
CheckBox soundOn = new CheckBox("soundOn?");
Tooltip tooltip = new Tooltip("if this box is checked, music will be played!");
tooltip.setFont(new Font("Arial", 16));
soundOn.setTooltip(tooltip);
soundOn.selectedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> ov,
Boolean old_val, Boolean new_val) {
Main.isSoundOn = soundOn.isSelected();
System.out.println(Main.isSoundOn);
}
});
vBox.getChildren().add(soundOn);
HBox changeVolumeWrapper = new HBox();
changeVolumeWrapper.setId("hBox");
Label sliderLabel = new Label("sound volume: ");
changeVolumeWrapper.getChildren().add(sliderLabel);
Slider soundVolume = new Slider(0, 1, .5);
soundVolume.valueProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> ov,
Number old_val, Number new_val) {
Main.soundVolume = new_val.doubleValue();
//Main.mediaPlayer.setVolume(Main.soundVolume); here is why i need media player static.
System.out.printf("%.2f\n", Main.soundVolume);
}
});
changeVolumeWrapper.getChildren().add(soundVolume);
vBox.getChildren().add(changeVolumeWrapper);
}
public Scene getScene() {
return scene;
}
}
public MainMenu getMainMenu() {
return mainMenu;
}
}
任何其他修复我的代码可以理解的。 顺便说一句,这些都是错误的,我得到:
异常在线程“线程0” java.lang.IllegalStateException: 工具包在 com.sun.javafx.application.PlatformImpl.runLater未初始化( PlatformImpl.java:273) 在 com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268) 在javafx.application.Platform.runLater(Platform.java:83)在 javafx.scene.media .Media $ _MetadataListener.onMetadata(Media.java:541) at com.sun.media.jfxmediaimpl.MetadataParserImpl.done(MetadataParserImpl.java:120) at com.sun.media.jfxmediaimpl.platform.java.ID3MetadataParser.parse(ID3MetadataParser.java:237) 在 com.sun.media.jfxmediaimpl.MetadataParserImpl.run(MetadataParserImpl.java:103) 异常在线程“主“java.lang.ExceptionInInitializerError at java.lang.Class.forName0(Native Method)at java.lang.Class.forName(Class.java:264)at com.intellij.rt.execution.application.AppMain.main (AppMain.java:122) 引起:java.lang.IllegalStateException:工具包不 com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273) 在 com.sun.javafx初始化 。 application.PlatformImpl.runLater(PlatformImpl.java:268) at javafx.application.Platform.runLater(Platform.java:83)at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:515)at javafx.scene.media.MediaPlayer。(MediaPlayer.java:414 。)在 主(Main.java:22)... 3更
您的MediaPlayer并不需要是静态的在这里。请参阅下面的James_D的答案。尽可能避免使用静态变量。 – ManoDestra
是的BC这不是我的整个代码我想从其他类访问这个媒体播放器:) –
正如我下面发布,'静态'与无障碍/可见性无关 –