- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更新:我想让媒体播放器静态,但如果我使其静态,它就不起作用。请注意,我想要 mediaPlayer 静态的原因是我想从其他类访问它。(该行已注释。)这是我的代码:
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;
}
}
对我的代码的任何其他修复将不胜感激。顺便说一下,这些是我得到的错误:
Exception in thread "Thread-0" java.lang.IllegalStateException: Toolkit not initialized at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273) at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268) at javafx.application.Platform.runLater(Platform.java:83) at 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) at com.sun.media.jfxmediaimpl.MetadataParserImpl.run(MetadataParserImpl.java:103) Exception in thread "main" 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) Caused by: java.lang.IllegalStateException: Toolkit not initialized at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273) at 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) at Main.(Main.java:22) ... 3 more
最佳答案
在没有上下文对象的情况下调用 getClass()
的解释与任何其他实例方法相同:this.getClass()
。
在静态上下文中,您可以使用ClassName.class
引用该类;即你可以这样做
static URL resource = Main.class.getResource("a.mp3");
但是,在这种情况下,根本不清楚为什么您希望这些变量是静态的;每个 JVM 实例只能创建一个 Application
子类的实例,并且这些是该实例的固有属性。
在您(更新的)问题的具体示例中,我将定义一个单独的类,封装 MediaPlayer
和您当前设为静态的其他属性。请注意,MediaPlayer
本身定义了 volume
属性和 muted
属性。所以你可以这样做:
public class SoundPlayer {
private final MediaPlayer mediaPlayer ;
public SoundPlayer(URL url) {
this.mediaPlayer = new MediaPlayer(new Media(url));
}
public void play() {
mediaPlayer.play();
}
public double getVolume() {
return mediaPlayer.getVolume();
}
public void setVolume(double volume) {
mediaPlayer.setVolume(volume);
}
public boolean isSoundOn() {
return ! mediaPlayer.isMuted();
}
public void setSoundOn(boolean soundOn) {
mediaPlayer.setMuted(! soundOn);
}
}
现在您的 Main
类可以是:
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
SoundPlayer soundPlayer = new SoundPlayer(getClass().getResource("song.mp3"));
soundPlayer.play();
primaryStage.setTitle("duet by Aran Mohyeddin");
GUI gui = new GUI(soundPlayer);
primaryStage.setScene(gui.getMainMenu().getScene());
primaryStage.show();
}
}
并更新您的 GUI
和 MainMenu
类以引用 SoundPlayer
:
public class MainMenu {
private Scene scene;
private final SoundPlayer soundPlayer ;
private MainMenu(SoundPlayer soundPlayer) {
this.soundPlayer = soundPlayer ;
// existing code omitted...
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) {
soundPlayer.setSoundOn(new_val);
}
});
// ...
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) {
soundPlayer.setVolumn(new_val.doubleValue());
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;
}
}
另请注意,如果您从 SoundPlayer
公开实际的属性对象,例如:
public class SoundPlayer {
private final MediaPlayer mediaPlayer ;
// ...
public DoubleProperty volumeProperty() {
return mediaPlayer.volumeProperty();
}
// ...
}
然后你可以简化一些代码:
Slider soundVolume = new Slider(0, 1, .5);
// instead of the listener, just do:
soundPlayer.volumeProperty().bindBidirectional(soundVolume.valueProperty());
(将 mutedProperty
转换为 soundOnProperty
有点不太优雅。)
关于java - 获取文件资源的静态方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37686524/
有人告诉我,如果我只有一个“东西”,比如家(不是多个家),我应该在 routes.rb 中使用资源 :home,而不是资源 :home。但是当我查看路由时,POST 函数似乎想要 home#creat
Activity 开始。这些代码框架顺利通过。 // Initialize array adapters. One for already paired devices and //
资源 search-hadoop.com search-hadoop.com索引所有邮件列表,非常适合历史搜索。当你遇到问题时首先在这里搜索,因为很可能有人已经遇到了你的问题。 邮件列表 在A
我是 WPF 的新手,正在努力使用位于单独程序集中的样式。这就是我正在做的:- 我有一个带有\Themes 文件夹的类库项目,其中包含一个“generic.xaml”,它合并了\Themes 内的子文
我正在编写一个使用虚拟树状文件结构的插件。基本上它就像一个包含文件的标准文件系统,区别在于这些文件实际上并不存在于文件系统中的特定位置,而只是 java 对象。 这些当前由使用 SettingProv
如果我在 XAML 中使用以下内容,我会收到错误消息: 错
我正在使用 laravel 资源来获取 api 的数据: return [ 'id' => $this->id, 'unread' =>
我有以下 pom.xml: 4.0.0 mycompany resource-fail 0.0.1-SNAPSHOT BazBat
许多GDI +类都实现IDisposable,但是我不确定何时应该调用Dispose。对于使用new或静态方法(例如Graphics.CreateGraphics)创建的实例来说,这很明显。但是,由属
我正在构建一组 RESTful 资源,其工作方式如下:(我将使用“people”作为示例): 获取/people/{key} - 返回一个人对象 (JSON) GET/people?first_nam
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一个使用 $resource 的简单 Controller : var Regions = $resource('mocks/regions.json'); $scope.regions =
在 Azure 门户中,如何查看不同资源之间的依赖关系。我特别想查看哪些资源正在使用我要删除的存储。 最佳答案 您可以使用应用程序洞察应用程序 map 来执行此操作: 您还可以打开存储帐户的日志记录:
我正在使用 ionic 生成资源(图标和启动画面)。我正在使用 ionic v2.1.0 和 cordova v6.4.0。 到目前为止我一直在使用(它在以前的版本中工作): cordova plat
是否可以使用 Assets 包含子文件夹中的文件? 示例:[base_url]/assets/css/pepper-grinder/jquery-ui-1.8.11.custom.min.css 最佳
我正在阅读一些尝试教授 Android 开发的书。在书中,作者概述了 res/下的一些目录。他提到 res/menu 包含基于 XML 的菜单规范。他还提到了保存“通用文件”的 res/raw。当我创
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我在服务器上使用 express-resource。在我的 AngularJS Controller 中: var User = $resource('/services/users/:use
因此,每当我运行我的应用程序时,它都会立即崩溃并给出以下错误: No package identifier when getting value for resource number 0x00000
对于我正在创建的(网络)应用程序,我需要使用基本身份验证在我的 UIWebView 中加载页面。 现在设置我使用的授权 header : NSString *result = [NSString st
我是一名优秀的程序员,十分优秀!