Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将ImageView调整为所有可用空间_Java_Javafx_Fxml_Image Resizing - Fatal编程技术网

Java 将ImageView调整为所有可用空间

Java 将ImageView调整为所有可用空间,java,javafx,fxml,image-resizing,Java,Javafx,Fxml,Image Resizing,在过去的一周里,我看到了很多不同的答案,但没有一个能帮上忙。目前,我在这里为ImageView设置了一个奇怪的“类”: package com.grimlytwisted.programs.screens.editor; import com.grimlytwisted.programs.Systats; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Label; i

在过去的一周里,我看到了很多不同的答案,但没有一个能帮上忙。目前,我在这里为ImageView设置了一个奇怪的“类”:

package com.grimlytwisted.programs.screens.editor;

import com.grimlytwisted.programs.Systats;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.net.URL;
import java.util.ResourceBundle;

public class Editor implements Initializable {

    @FXML private ImageView imageDisplay;
    @FXML private Label nameOfImage;
    @FXML private Label sizeOfImage;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.setDefaultImage();
    }

    private void setDefaultImage() {

        System.out.println(Systats.getScreenHeight());
        imageDisplay.autosize();
        imageDisplay.setImage(new Image("images/dirt.jpg", imageDisplay.getFitWidth(), imageDisplay.getFitHeight(), true, false));
    }
}
…如果它对我的FXML文件有帮助:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.geometry.Rectangle2D?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>

<BorderPane fx:id="Editor" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.grimlytwisted.programs.screens.editor.Editor">
   <bottom>
      <HBox id="bottomContainer" stylesheets="@styles/coloring.css" BorderPane.alignment="CENTER">
         <children>
            <Label fx:id="nameOfImage" text="nameOfImage" />
            <Region HBox.hgrow="ALWAYS" />
            <Label fx:id="sizeOfImage" text="sizeOfImage" />
         </children>
         <opaqueInsets>
            <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
         </opaqueInsets>
         <padding>
            <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
         </padding>
      </HBox>
   </bottom>
   <top>
      <VBox BorderPane.alignment="CENTER">
         <children>
            <MenuBar>
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Delete" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
         </children>
      </VBox>
   </top>
   <left>
      <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE">
        <tabs>
          <Tab text="Untitled Tab 1">
            <content>
              <AnchorPane id="tabPane" fx:id="tabPane" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" stylesheets="@styles/coloring.css" />
            </content>
          </Tab>
          <Tab text="Untitled Tab 2">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
            </content>
          </Tab>
        </tabs>
      </TabPane>
   </left>
   <center>
      <ImageView id="imageDisplay" fx:id="imageDisplay" cache="true" cacheHint="SPEED" depthTest="ENABLE" nodeOrientation="INHERIT" pickOnBounds="true" preserveRatio="true" smooth="false">
         <viewport>
            <Rectangle2D />
         </viewport>
      </ImageView>
   </center>
</BorderPane>

所以它溢出到我的菜单和底部容器区域,似乎只限于宽度。它显然没有高度边界,我需要它只与宽度/高度一样大,没有“拉伸”


当前我希望保留像素比率,并在图像达到最大值之前尽可能地缩放图像。

图像视图
包装在
窗格
中,并将其
fitWidth
/
fitHeight
属性绑定到此
窗格
的大小。另外,将
图像视图
设为非托管状态,并为
窗格
设置首选大小:


private void setDefaultImage() {
    imageDisplay.setImage(new Image("images/dirt.jpg"));
}