这个应用程序是一个秒表游戏,按下开始时会出现五个图像之一。我遇到的问题是我无法在ImageViewer中显示图像,我相信这是因为当我尝试将按钮和ImageViewer的id放入scenebuilder fx:id时,它告诉我它不存在,即使拼写是正确的。我查看了大量的在线资源,但找不到任何信息。
package application;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.File;
public class ClickController{
//creating the buttons
@FXML
private Button one;
@FXML
private Button two;
@FXML
private Button three;
@FXML
private Button four;
@FXML
private Button five;
@FXML
private Button start;
@FXML
private MenuItem close;
@FXML
private ImageView imgView;
Main main;
Stopwatch stopwatch;
boolean stopwatchactive = false;
boolean stopwatchrunning = false;
ArrayList<Image> imgList = new ArrayList<>();
Image firstImage = new Image("/images/1.png");
Image secondImage = new Image("/images/2.png");
Image thirdImage = new Image("/images/3.png");
Image fourthImage = new Image("/images/4.png");
Image fifthImage = new Image("/images/5.png");
@FXML
private void chooseImage(ActionEvent event){
imgView.setImage(firstImage);
}
@FXML
private void oneButtonPush(ActionEvent event) throws IOException{
imgView.setImage(firstImage);
System.out.println("one");
if(stopwatchrunning = true){
stopwatch.stopTimer();
stopwatchrunning = false;
}
}
@FXML
private void twoButtonPush(ActionEvent event) throws IOException{
System.out.println("two");
if(stopwatchrunning = true){
stopwatch.stopTimer();
stopwatchrunning = false;
}
}
@FXML
private void threeButtonPush(ActionEvent event) throws IOException{
System.out.println("three");
if(stopwatchrunning = true){
stopwatch.stopTimer();
stopwatchrunning = false;
}
}
@FXML
private void fourButtonPush(ActionEvent event) throws IOException{
System.out.println("four");
if(stopwatchrunning = true){
stopwatch.stopTimer();
stopwatchrunning = false;
}
}
@FXML
private void fiveButtonPush(ActionEvent event) throws IOException{
System.out.println("five");
if(stopwatchrunning = true){
stopwatch.stopTimer();
stopwatchrunning = false;
}
}
@FXML
private void startButtonPush(ActionEvent event){
System.out.println("start");
try{
if(stopwatchactive != true){
stopwatchactive = true;
stopwatch = new Stopwatch();
}
stopwatch.startTimer();
}
catch(Exception e){
}
}
@FXML
private void closeButtonPush(ActionEvent event) throws IOException{
stopwatchactive = false;
stopwatch.logTimes();
Platform.exit();
System.exit(0);
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.ClickController">
<children>
<Button fx:id="start" layoutX="271.0" layoutY="120.0"
mnemonicParsing="false" onAction="#startButtonPush" text="Start"
AnchorPane.bottomAnchor="253.0" AnchorPane.leftAnchor="271.0"
AnchorPane.rightAnchor="270.0" AnchorPane.topAnchor="120.0" />
<Button fx:id="four" contentDisplay="GRAPHIC_ONLY" layoutX="382.0" layoutY="276.0" mnemonicParsing="false" onAction="#fourButtonPush" text="four" AnchorPane.bottomAnchor="97.0" AnchorPane.leftAnchor="382.0" AnchorPane.rightAnchor="159.0" AnchorPane.topAnchor="276.0">
<graphic>
<ImageView>
<image>
<Image url="@../images/4.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="two" contentDisplay="GRAPHIC_ONLY" layoutX="155.0" layoutY="276.0" mnemonicParsing="false" onAction="#twoButtonPush" text="two" AnchorPane.bottomAnchor="97.0" AnchorPane.leftAnchor="155.0" AnchorPane.rightAnchor="386.0" AnchorPane.topAnchor="276.0">
<graphic>
<ImageView>
<image>
<Image url="@../images/2.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="one" contentDisplay="GRAPHIC_ONLY" layoutX="50.0" layoutY="276.0" mnemonicParsing="false" onAction="#oneButtonPush" text="one" AnchorPane.bottomAnchor="97.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="491.0" AnchorPane.topAnchor="276.0">
<graphic>
<ImageView>
<image>
<Image url="@../images/1.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="three" contentDisplay="GRAPHIC_ONLY" layoutX="271.0" layoutY="276.0" mnemonicParsing="false" onAction="#threeButtonPush" text="three" AnchorPane.bottomAnchor="97.0" AnchorPane.leftAnchor="271.0" AnchorPane.rightAnchor="270.0" AnchorPane.topAnchor="276.0">
<graphic>
<ImageView>
<image>
<Image url="@../images/3.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="five" contentDisplay="GRAPHIC_ONLY" layoutX="489.0" layoutY="276.0" mnemonicParsing="false" onAction="#fiveButtonPush" text="five" AnchorPane.bottomAnchor="97.0" AnchorPane.leftAnchor="489.0" AnchorPane.rightAnchor="52.0" AnchorPane.topAnchor="276.0">
<graphic>
<ImageView>
<image>
<Image url="@../images/5.png" />
</image>
</ImageView>
</graphic>
</Button>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="close" mnemonicParsing="false" onAction="#closeButtonPush" text="Close" />
</items>
</Menu>
</menus>
</MenuBar>
<Label layoutX="191.0" layoutY="74.0" prefHeight="17.0" prefWidth="66.0"
text="Cick on:" />
<ImageView fx:id="imgView" fitHeight="50.0" fitWidth="50.0" layoutX="287.0"
layoutY="58.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>