<VBox prefHeight="600" prefWidth="800" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1">
<VBox alignment="TOP_CENTER">
<ToolBar minHeight="50.0" prefHeight="50.0" prefWidth="800.0" stylesheets="@style.css" GridPane.rowIndex="1">
<ImageView fitHeight="35.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
<Image url="@react-toolbar-logo.png" />
</ImageView>
</ToolBar>
<MenuBar fx:id="menuBar" prefHeight="0.0" prefWidth="0.0" />
</VBox>
</VBox>
public class Main extends Application {
private Image mainImage;
private final Group selectionGroup = new Group();
@Override
public void start(Stage primaryStage) throws Exception {
// Basic stage settings
primaryStage.setTitle("Picture Viewer");
primaryStage.setResizable(true);
// Set task bar primary icon
primaryStage.getIcons().add(new javafx.scene.image.Image("main/react-app-icon.png"));
// Declare UI variables
final BorderPane borderPane = new BorderPane();
final ScrollPane scrollPane = new ScrollPane();
final Scene scene = new Scene(borderPane, 800, 600);
ImageView mainImageView = new ImageView();
// Select main layout file
FXMLLoader loader = new FXMLLoader(getClass().getResource("/main/scene.fxml"));
loader.setController(new MainController(primaryStage, selectionGroup, mainImage, mainImageView));
Parent root = loader.load();
// Add custom stylesheet URL
scene.getStylesheets().add("main/style.css");
// Set UI element properties
selectionGroup.getChildren().add(mainImageView);
scrollPane.setContent(selectionGroup);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
// Show primary stage
// primaryStage.setScene(scene);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class MainController implements Initializable {
private Stage primaryStage;
private Group selectionGroup;
private Image mainImage;
private ImageView mainImageView;
MainController(Stage primaryStage, Group selectionGroup, Image mainImage, ImageView mainImageView) {
this.primaryStage = primaryStage;
this.selectionGroup = selectionGroup;
this.mainImage = mainImage;
this.mainImageView = mainImageView;
}
private boolean isAreaSelected = false;
private final AreaSelection areaSelection = new AreaSelection();
@FXML
private
MenuBar menuBar;
@Override
public void initialize(URL location, ResourceBundle resources) {
// Declare menus
final Menu menu1 = new Menu("File");
final Menu menu2 = new Menu("Options");
// Define menu 1 items
final MenuItem open = new MenuItem("Open");
final MenuItem clear = new MenuItem("Clear");
final MenuItem exit = new MenuItem("Exit");
// Define menu 2 items
// final MenuItem select = new MenuItem("Select Area");
final MenuItem crop = new MenuItem("Crop & Upload");
final MenuItem clearSelectionItem = new MenuItem("Clear Selection");
// Set menu items
menu1.getItems().addAll(open, clear, exit);
menu2.getItems().addAll(crop, clearSelectionItem);
// Set menu click events
setMenu1ClickEvents(primaryStage, open, clear, exit);
setMenu2ClickEvents(crop, clearSelectionItem);
// Instantiate menus
menuBar.getMenus().addAll(menu1, menu2);
}
private void setMenu1ClickEvents(Stage primaryStage, MenuItem open, MenuItem clear, MenuItem exit) {
// Open file system to select image
open.setOnAction(event -> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Image File");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg"));
File selectedFile = fileChooser.showOpenDialog(primaryStage);
if (selectedFile != null) {
clearSelection(selectionGroup);
this.mainImage = convertFileToImage(selectedFile);
System.out.println("selectedFile: " + selectedFile);
mainImageView.setImage(mainImage);
System.out.println("mainImage.getWidth(): " + mainImage.getWidth());
changeStageSizeImageDimensions(primaryStage, mainImage);
}
});
// Clear the current image
clear.setOnAction(event -> {
clearSelection(selectionGroup);
mainImageView.setImage(null);
System.gc();
});
// Exit the application
exit.setOnAction(event -> {
Platform.exit();
System.exit(0);
});
}
private void setMenu2ClickEvents(MenuItem crop, MenuItem clearSelectionItem) {
// Set menu 2 click events
crop.setOnAction(event -> {
if (isAreaSelected()) {
cropImage(areaSelection.selectArea(selectionGroup).getBoundsInParent(), mainImageView);
}
});
clearSelectionItem.setOnAction(event -> clearSelection(selectionGroup));
}
}
private Image convertFileToImage(File imageFile) {
Image image = null;
try (FileInputStream fileInputStream = new FileInputStream(imageFile)) {
image = new Image(fileInputStream);
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
您从不显示ImageView
,或者您在Java代码中创建的任何控件。
根据注释,您希望FXML的内容位于边框窗格的顶部,而滚动窗格包含组,图像视图位于中心。
所以你应该做:
public class Main extends Application {
private Image mainImage;
private final Group selectionGroup = new Group();
@Override
public void start(Stage primaryStage) throws Exception {
// Basic stage settings
primaryStage.setTitle("Picture Viewer");
primaryStage.setResizable(true);
// Set task bar primary icon
primaryStage.getIcons().add(new javafx.scene.image.Image("main/react-app-icon.png"));
// Declare UI variables
final BorderPane borderPane = new BorderPane();
final ScrollPane scrollPane = new ScrollPane();
final Scene scene = new Scene(borderPane, 800, 600);
ImageView mainImageView = new ImageView();
// Select main layout file
FXMLLoader loader = new FXMLLoader(getClass().getResource("/main/scene.fxml"));
loader.setController(new MainController(primaryStage, selectionGroup, mainImage, mainImageView));
Parent root = loader.load();
// Add custom stylesheet URL
scene.getStylesheets().add("main/style.css");
// Set UI element properties
selectionGroup.getChildren().add(mainImageView);
scrollPane.setContent(selectionGroup);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
borderPane.setCenter(scrollPane);
borderPane.setTop(root);
// Show primary stage
primaryStage.setScene(scene);
// primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
null 那个代码有什么问题?我不明白为什么不能将ImageRi发送到ImageView。我还验证了我正在传递给setImageURI函数的Uri值是正确的。
E/AndroidRuntime:致命异常:main java.lang.OutOfMemoryError位于Android.Graphics.BitmapFactor.NativeDecodeStream(原生方法)位于Android.Graphics.BitmapFactor.DecodeStream(BitmapFactor.JAVA:623)位于Android.Graphics.Bitma
我是android编程的新手。我想在显示时在一个xml中动态设置图像。具体来说,我将项目相关的图像存储在drawable文件夹中。此外,我将图像名称存储在字符串变量中,并尝试动态地将这些图像设置为imageview。但图像没有显示。 我的代码:
问题内容: 我在xml布局文件中设置了一个onClick方法,此方法可触发手机振动100毫秒,这时我将ImageView可见性设置为可见,因此可以看到它。我希望在振动停止后再次将ImageView设置回去。我该怎么办? 问题答案: 您可以同时启动此方法:
我尝试将PNG图像表单drawable加载到ImageView中,并使用下面的代码设置此ImageView的着色颜色⇒ 它正在工作: 我想使用Glide将SVG图像加载到ImageView中,并为其设置着色颜色。但在成功地将SVG图像加载到imageView后,setColorFilter无法工作。 我可以使用Glide将SVG图像加载到另一个ImageView中,代码如下: 现在,我想更改mag
主要内容:本节引言:,1.src属性和background属性的区别:,2.adjustViewBounds设置缩放是否保存原图长宽比,3.scaleType设置缩放类型,4.最简单的绘制圆形的ImageView,本节小结:本节引言: 本节介绍的UI基础控件是:ImageView(图像视图),见名知意,就是用来显示图像的一个View或者说控件! 官方API:ImageView;本节讲解的内容如下: ImageView的src属性和blackground的区别; adjustViewBounds设