即使我将第二个FXML表单加载到GUI中,我也无法弄清楚为什么变量为null。第二个fxml表单包含一个TabPane, theImageDisplayer ,以及两个ImageViews, editedImage 和 orignialImage 。第二个FXML中的所有变量都没有初始化,它阻止我的GUI执行某些操作,例如“从计算机导入图像。”
这是我的代码:
private void initializeRootLayout() {
Scene scene = new Scene(this.rootLayout);
this.primaryStage.setScene(scene);
this.primaryStage.setResizable(false);
this.primaryStage.sizeToScene();
this.initializeImageView(); <-- Initializes the Tab Layout
-->this.initializePictureController();<-- Initializes my controller PictureController
this.primaryStage.show();
}
private void initializeImageView() {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/edu/westga/SignboardProjectXXX/view/PictureView.fxml"));
this.tabs = (TabPane) loader.load();<-- Initializes the TabLayout with the null variables
this.rootLayout.setCenter(this.tabs); <-- Adds the Tab Layout to the root layout
这是我的控制器PictureContoller崩溃的地方:
public class PictureController {
@FXML
---->private TabPane theImageDisplayer;<---- The variable is located here.
@FXML
private ListView<Picture> imageHistoryList;
//Textfields:
@FXML
private TextField imageDimensions;
@FXML
private TextField imageLinkBox;
@FXML
private TextField imageName;
// Labels:
@FXML
private Label pixelsPerHeight;
@FXML
private Label resizePercentage;
@FXML
private Label zoomEditedImagePercentage;
@FXML
private Label pixelsPerLength;
@FXML
private Label pixelsPerWidth;
// Check-box Button:
@FXML
private CheckBox replaceOriginalImage;
@FXML
private CheckBox eraseHistoryOnImageChange;
// Sliders:
@FXML
private Slider imageResizeSlider;
@FXML
private Slider zoomEditedImageSlider;
@FXML
private Slider pixelsPerHeightSlider;
@FXML
private Slider pixelsPerWidthSlider;
// Accordion Menu:
@FXML
private Accordion theMainMenu;
// Toggle Buttons:
@FXML
private ToggleGroup fileType;
@FXML
private ToggleButton toggleEditedImageVisibility;
// Buttons:
@FXML
private Button restoreImageButton;
@FXML
private Button downloadImageButton;
@FXML
private Button uploadImageButton;
// Radio Buttons:
@FXML
private RadioButton pngRadioButton;
@FXML
private RadioButton gifRadioButton;
@FXML
private RadioButton jpegRadioButton;
@FXML
private RadioButton jpgRadioButton;
// Image Displays:
@FXML
private ImageView originalImage;
@FXML
private ImageView historyPreviewImage;
@FXML
private ImageView editedImage;
// Instance Variables:
private UploadDownloadController loadSaveController;
private ImageFilterController filterController;
private ObservableList<Picture> imageHistory;
private String imageSaveExtension;
private Picture originalPic;
private Picture currEditedPic;
private PictureIO io;
private ArrayList<RadioButton> saveTypesExtension;
private static final int ORIGINAL_IMAGE_TAB = 0;
private static final int EDITED_IMAGE_TAB = 1;
public void initialize() {
this.io = new PictureIO();
this.loadSaveController = new UploadDownloadController();
this.originalPic = new Picture("Default", 100, 100);
this.currEditedPic = new Picture("Default", 100, 100);
this.saveTypesExtension = this.buildTemporarySaveTypeList();
}
@FXML
private void toggleSelection(MouseEvent event) {
System.out.println("hello6");
}
@FXML
private void changeRadioButtonSetValue(ActionEvent event) {
this.setImageSaveExtentionValue();
}
@FXML
private void changePixelWidthPerSquare(MouseEvent event) {
this.setValueOfLabel(this.pixelsPerWidthSlider, this.pixelsPerWidth);
}
@FXML
private void changePixelHeightPerSquare(MouseEvent event) {
this.setValueOfLabel(this.pixelsPerHeightSlider, this.pixelsPerHeight);
}
@FXML
private void changeImageSize(MouseEvent event) {
this.setValueOfLabel(this.imageResizeSlider, this.resizePercentage);
}
@FXML
private void changeEditedZoomLevel(MouseEvent event) {
this.setValueOfLabel(this.zoomEditedImageSlider, this.zoomEditedImagePercentage);
}
@FXML
private void toggleEditedImageVisibility(ActionEvent event) {
this.toggleVisibility(this.editedImage);
}
@FXML
private void uploadImageFromComputer(ActionEvent event) {
this.loadFromComputer();
this.selectTab(EDITED_IMAGE_TAB);
}
@FXML
private void downloadImageFromWeb(ActionEvent event) {
this.getImageFromWeb();
this.selectTab(EDITED_IMAGE_TAB);
System.out
.println("Warn users a better way. This does not go away after a few seconds.");
}
private void selectTab(final int theTab) {
SingleSelectionModel<Tab> sinlgeSelection = this.theImageDisplayer.getSelectionModel();
sinlgeSelection.select(theTab);
}
@FXML
private void displayImageInformation(MouseEvent event) {
this.getInformationFromPicture();
}
@FXML
private void restoreSelectedImage(ActionEvent event) {
this.revertImageWithPrevImage();
}
@FXML
private void resizeImage(ActionEvent event) {
double realPercentageValue = this.io.changeStringToNumber(this.resizePercentage.getText()) / 100.0;
System.out.println("Result: " + realPercentageValue);
this.filterController = new ImageFilterController(this.currEditedPic);
Picture picture = this.filterController.resizedPicture(realPercentageValue);
this.changeEditedImage(picture);
this.io.convertPictureToImage(picture);
}
// private void warnUser() {
// Tooltip warningToUser = new
// Tooltip("Please enter value in the text area above.");
// Tooltip.install(this.downloadImageButton, warningToUser);
// warningToUser.show(this.downloadImageButton, 0, 0);
// //TODO make tooltip work
// }
private void setImageSaveExtentionValue() {
for (RadioButton selectedRadioButton : this.saveTypesExtension) {
if (selectedRadioButton.isSelected()) {
this.imageSaveExtension = selectedRadioButton.getText();
}
}
}
private ArrayList<RadioButton> buildTemporarySaveTypeList() {
ArrayList<RadioButton> imagesaveTypes = new ArrayList<RadioButton>();
imagesaveTypes.add(this.gifRadioButton);
imagesaveTypes.add(this.pngRadioButton);
imagesaveTypes.add(this.jpgRadioButton);
imagesaveTypes.add(this.jpegRadioButton);
return imagesaveTypes;
}
private void setValueOfLabel(Slider theSlider, Label theLabel) {
theSlider.valueChangingProperty().addListener(
new ChangeListener<Boolean>() {
@Override
public void changed(
ObservableValue<? extends Boolean> observableValue,
Boolean wasChanging, Boolean changing) {
if (wasChanging || changing) {
theLabel.setText((int) theSlider.getValue() + "");
}
}
});
}
private void toggleVisibility(Node targetObject) {
boolean currVisibility = targetObject.isVisible();
targetObject.setVisible(!currVisibility);
}
private void revertImageWithPrevImage() {
try {
Picture prevPicture = this.io.convertImageToPicture(this.editedImage);
this.imageHistory.add(prevPicture);
Picture restoredPicture = this.imageHistoryList.getSelectionModel()
.getSelectedItem();
this.currEditedPic = new Picture(restoredPicture.getName(), restoredPicture.getImage());
Image theImage = this.io.convertPictureToImage(restoredPicture);
this.editedImage.setImage(theImage);
} catch (Exception nullImage) {
nullImage.getMessage();
}
}
private void refreshHistoryList(Picture picture) {
this.imageHistory = this.imageHistoryList.getItems();
this.imageHistory.add(picture);
this.imageHistoryList.setItems(this.imageHistory);
}
private void getInformationFromPicture() {
Picture picture = this.imageHistoryList.getSelectionModel()
.getSelectedItem();
if (picture != null) {
this.imageName.setText(picture.getName());
this.imageDimensions.setText(picture.getWidth() + " x "
+ picture.getHeight());
Image theImage = this.io.convertPictureToImage(picture);
this.historyPreviewImage.setImage(theImage);
}
}
private void loadFromComputer() {
Picture picture = this.loadSaveController.uploadImage();
this.changeAndStoreImage(picture);
}
private void getImageFromWeb() {
String imageWebLink = this.imageLinkBox.getText();
if (!imageWebLink.equals("")) {
Picture picture = this.loadSaveController
.downloadImage(this.imageLinkBox.getText());
this.changeAndStoreImage(picture);
this.imageLinkBox.clear();
}
}
private void changeAndStoreImage(Picture picture) {
if (!picture.getName().equals("No Image")) {
this.changeEditedImage(picture);
this.setOriginalImage(picture);
this.refreshHistoryList(picture);
}
}
private void changeEditedImage(Picture picture) {
this.currEditedPic = new Picture(picture.getName(), picture.getImage());
------>this.theImageDisplayer.getTabs().get(1).setDisable(false);<------ It crashes here*
Image theImage = this.io.convertPictureToImage(picture);
this.editedImage.setImage(theImage);
this.adjustImageSize(this.editedImage);
}
private void setOriginalImage(Picture picture) {
this.originalPic = new Picture(picture.getName(), picture.getImage());
Image theImage = this.io.convertPictureToImage(picture);
this.originalImage.setImage(theImage);
this.adjustImageSize(this.originalImage);
}
private void adjustImageSize(ImageView image) {
this.adjustWidth(image);
this.adjustHeight(image);
}
private void adjustHeight(ImageView image) {
double imageDisplayHeight = this.theImageDisplayer.getHeight();
double theImageHeight = image.getImage().getHeight();
if (imageDisplayHeight < theImageHeight) {
image.setFitWidth(imageDisplayHeight);
}
}
private void adjustWidth(ImageView image) {
double theImageWidth = image.getImage().getWidth();
double imageDisplayWidth = this.theImageDisplayer.getWidth();
if (imageDisplayWidth < theImageWidth) {
image.setFitWidth(imageDisplayWidth);
}
}
}
这是第二个FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.Region?>
<TabPane fx:id="theImageDisplayer" prefHeight="643.0" prefWidth="614.0" tabClosingPolicy="UNAVAILABLE" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="edu.westga.SignboardProjectMichaelMorguarge.view.PictureController">
<tabs>
<Tab text="Original Image">
<content>
<Pane prefHeight="614.0" prefWidth="607.0">
<children>
<ScrollPane prefHeight="614.0" prefViewportHeight="180.0" prefViewportWidth="200.0" prefWidth="607.0">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="612.0" prefWidth="605.0">
<children>
<ImageView fx:id="originalImage" fitHeight="588.0" fitWidth="477.0" layoutX="64.0" layoutY="271.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="271.00199127197266" AnchorPane.leftAnchor="64.0" AnchorPane.rightAnchor="64.0" AnchorPane.topAnchor="271.0">
<effect>
<DropShadow />
</effect>
<image>
<Image url="@../../../../../Welcome.png" />
</image>
</ImageView>
</children>
</AnchorPane>
</content>
</ScrollPane>
</children>
</Pane>
</content>
</Tab>
<Tab disable="true" text="Signboard">
<content>
<Pane>
<children>
<ScrollPane prefHeight="614.0" prefViewportHeight="180.0" prefViewportWidth="200.0" prefWidth="607.0">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="612.0" prefWidth="605.0">
<children>
<ImageView fx:id="editedImage" fitHeight="588.0" fitWidth="477.0" layoutX="64.0" layoutY="12.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="12.0" AnchorPane.leftAnchor="64.0" AnchorPane.rightAnchor="64.0" AnchorPane.topAnchor="12.0">
<effect>
<DropShadow />
</effect>
</ImageView>
</children>
</AnchorPane>
</content>
</ScrollPane>
</children>
</Pane>
</content>
</Tab>
</tabs>
</TabPane>
如果您想了解更多信息,请对我大喊......我会关注此事并接受建议。