- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
创建一个基于俄勒冈小道的小型角色扮演游戏。在 Controller IntroController
上,我无法使按钮在 handle
内使用react,而 LeaderBoardController
工作正常。
IntroController
包含从 LeaderBoardController
复制的两个按钮。
public class Main extends Application {
public Stage stage;
@Override
public void start(Stage primaryStage) {
try {
this.stage = primaryStage;
initLayout();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* initLayout contains the functionality to set up the root and scene of the
* JavaFX view using the desired FXML file, without the mess in start.
*
*/
public void initLayout() {
try {
System.out.println("Working Directory = " + System.getProperty("user.dir"));
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Intro.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
stage.setMaxHeight(500);
stage.setMaxWidth(750);
stage.setResizable(false);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* switchScene added to clean up mounts of code in regards to switching a scene usually from an actionevent. Takes in a string
* .fxml location and the Stage.
*/
public void switchScene(String fxml, Stage stage) {
try {
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource(fxml));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
stage.setMaxHeight(500);
stage.setMaxWidth(750);
stage.setResizable(false);
} catch (IOException e) {
// TODO handle exception
e.printStackTrace();
System.out.println("ERROR: Unable to open " + fxml);
stage.close();
}
}
public static void main(String[] args) {
launch(args);
}
}
public class IntroController implements EventHandler<ActionEvent>, Initializable {
@FXML
Button mainMenuButton;
@FXML
Button exitButton;
@FXML
AnchorPane background;
@FXML
ImageView picture;
Main main = new Main();
@Override
public void initialize(URL location, ResourceBundle resources) {
showImage("images/cilantroRiceLogo.png");
}
/*
* handle is used as the primary ActionEvent handler for actions such as
* "when" something is clicked, "then" do so.
*
* @param ActionEvent event
*/
@Override
public void handle(ActionEvent event) {
Button selected = (Button) event.getSource();
Stage stage = (Stage) selected.getScene().getWindow();
if(selected == mainMenuButton)
main.switchScene("fxml/Title.fxml", stage);
if(selected == exitButton)
stage.close();
background.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ENTER) {
main.switchScene("fxml/Title.fxml", stage);
}
});
}
private void showImage(String imageName) {
Image image;
try {
image = new Image(new FileInputStream(imageName));
if (imageName.equals("images/cilantroRiceLogo.png"))
picture.setImage(image);
} catch (FileNotFoundException e) {
// TODO handle exception!!
e.printStackTrace();
}
}
}
public class LeaderboardController implements EventHandler<ActionEvent>, Initializable {
@FXML
Button exitButton, mainMenuButton;
@FXML
TableView leaderTable;
@FXML
TableColumn rankColumn, nameColumn, difficultyColumn, scoreColumn;
Main main = new Main();
private ObservableList<Score> scores = FXCollections.observableArrayList();
private Leaderboard leaderboard;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
leaderboard = new Leaderboard();
setScores();
rankColumn.setCellValueFactory(new PropertyValueFactory<Score, String>("rank"));
nameColumn.setCellValueFactory(new PropertyValueFactory<Score, String>("userName"));
difficultyColumn.setCellValueFactory(new PropertyValueFactory<Score, String>("difficulty"));
scoreColumn.setCellValueFactory(new PropertyValueFactory<Score, String>("finalScore"));
leaderTable.setItems(scores);
}
@Override
public void handle(ActionEvent event) {
Button selected = (Button) event.getSource();
Stage stage = (Stage) selected.getScene().getWindow();
if(selected == mainMenuButton)
main.switchScene("fxml/Title.fxml", stage);
if(selected == exitButton)
stage.close();
}
/**
* populates scores with the Scores from leaderboard.scores
*/
public void setScores() {
for(Score score : leaderboard.getScores()) {
scores.add(score);
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controller.IntroController">
<children>
<AnchorPane fx:id="background" disable="true" onKeyTyped="#handle" prefHeight="500.0" prefWidth="750.0" style="-fx-background-color: #303030;">
<children>
<ImageView fx:id="picture" fitHeight="327.0" fitWidth="446.0" layoutX="176.0" layoutY="25.0" onMouseClicked="#handle" pickOnBounds="true" preserveRatio="true">
</ImageView>
<Button fx:id="exitButton" layoutX="444.0" layoutY="387.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#handle" prefHeight="60.0" prefWidth="239.0" style="-fx-background-color: #7f7f7f;" text="Exit Game">
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
<font>
<Font name="Magneto Bold" size="29.0" />
</font>
</Button>
<Button fx:id="mainMenuButton" alignment="CENTER" layoutX="53.0" layoutY="387.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#handle" prefHeight="60.0" prefWidth="239.0" style="-fx-background-color: #7f7f7f;" text="Main Menu" textAlignment="CENTER" wrapText="true">
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
<font>
<Font name="Magneto Bold" size="29.0" />
</font>
</Button>
</children>
</AnchorPane>
</children>
</AnchorPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="750.0" style="-fx-background-color: #000000;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.controller.LeaderboardController">
<children>
<TableView fx:id="leaderTable" layoutX="122.0" layoutY="110.0" prefHeight="287.0" prefWidth="510.0" style="-fx-background-color: #666666; -fx-overflow-x: none;" AnchorPane.bottomAnchor="103.0" AnchorPane.topAnchor="110.0">
<columns>
<TableColumn fx:id="rankColumn" minWidth="20.0" prefWidth="49.0" text="Rank" />
<TableColumn fx:id="nameColumn" editable="false" minWidth="173.0" prefWidth="232.0" resizable="false" sortable="false" text="Name" />
<TableColumn fx:id="difficultyColumn" editable="false" minWidth="30.0" prefWidth="89.0" resizable="false" sortable="false" text="Difficulty" />
<TableColumn fx:id="scoreColumn" editable="false" minWidth="90.0" prefWidth="138.0" resizable="false" sortType="DESCENDING" sortable="false" text="Score" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<Button fx:id="mainMenuButton" alignment="CENTER" layoutX="85.0" layoutY="419.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#handle" prefHeight="60.0" prefWidth="239.0" style="-fx-background-color: #7f7f7f;" text="Main Menu" textAlignment="CENTER" wrapText="true" AnchorPane.bottomAnchor="21.0" AnchorPane.leftAnchor="85.0" AnchorPane.rightAnchor="426.0">
<font>
<Font name="Magneto Bold" size="29.0" />
</font>
<cursor>
<Cursor fx:constant="HAND" />
</cursor></Button>
<Button fx:id="exitButton" layoutX="421.0" layoutY="419.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#handle" prefHeight="60.0" prefWidth="239.0" style="-fx-background-color: #7f7f7f;" text="Exit Game" AnchorPane.bottomAnchor="21.0" AnchorPane.leftAnchor="426.0" AnchorPane.rightAnchor="85.0">
<font>
<Font name="Magneto Bold" size="29.0" />
</font>
<cursor>
<Cursor fx:constant="HAND" />
</cursor></Button>
<Label alignment="CENTER" layoutX="169.0" layoutY="14.0" prefHeight="88.0" prefWidth="410.0" text="Leaderboard" textAlignment="CENTER" textFill="#f5f5f5" wrapText="true" AnchorPane.leftAnchor="169.0" AnchorPane.rightAnchor="171.0">
<font>
<Font name="Old English Text MT" size="72.0" />
</font>
<effect>
<DropShadow blurType="ONE_PASS_BOX" offsetX="2.0" offsetY="2.0" />
</effect>
</Label>
</children>
</AnchorPane>
最佳答案
如果不加载项目就很难判断。通常我要做的就是在 init 方法中设置 Handler 并查看是否可以触发它。
另一个随机的尝试是让你的处理程序使用基本事件:https://docs.oracle.com/javase/8/javafx/api/javafx/event/Event.html 。
关于JavaFX - 按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51771725/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!