大家好,所以我正在嘗試創建一個 gui,其中可以將組件的影像拖放到堆疊窗格中,然后將完成的布局/影像(多個上傳的影像)放入一個 excel 檔案中,我認為它會更好首先在用戶計算機上保存該快照節點的本地版本,然后將其發送到 excel 但是當我嘗試保存測驗 png 時它不起作用。并不是說它根本不起作用,但是一旦打開 .showSaveDialog 視窗,我選擇 .PNG 作為擴展名,然后給檔案一個名稱,例如“test”,然后點擊保存,沒有創建檔案。我不知道還有什么需要或者如果它是一些愚蠢的錯誤(當然我不知道我對 javafx 相當陌生,所以請耐心等待)。PS我試圖做一個最小的可重復的例子,如果它不完美,我很抱歉。我的控制器類:
public class Main {
@FXML private AnchorPane layoutPane;
@FXML private Canvas layoutCanvas;
@FXML private StackPane stackPane;
Window mainStage;
String filePath;
String imageFilePath;
// just to test saving a snapshot it just sets a single imageview as a child of the
// stack pane when button clicked otherwise this deals with drag and drop
public void dragAndDropImageView(ImageView image)
{
image.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
x = image.getLayoutX() - mouseEvent.getSceneX();
y = image.getLayoutY() - mouseEvent.getSceneY();
}
});
image.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
image.setLayoutX(mouseEvent.getSceneX() x);
image.setLayoutY(mouseEvent.getSceneY() y);
}
});
stackPane.getChildren().addAll(image);
}
// is supposed to handle the creation of new image views on each button click
public void handleImage() throws IOException
{
final double startingXPosition = 14;
final double startingYPosition = 146;
File chosenFile = fileChooser.showOpenDialog(mainStage);
String imageFilePath = chosenFile.getAbsolutePath();
Image componentImage = new Image(imageFilePath);
ImageView imageView = new ImageView();
imageView.setImage(componentImage);
imageView.setLayoutX(startingXPosition);
imageView.setLayoutY(startingYPosition);
imageView.setFitWidth(154);
imageView.setFitHeight(135);
layoutPane.getChildren().addAll(imageView);
imageView.setVisible(true);
dragAndDropImageView(imageView);
}
//handles saving image as png and sending to excel
public void exportImage() throws IOException
{
FileInputStream fis = new FileInputStream(filePath);
//String imageFilePath = imageFile.getAbsolutePath();
XSSFWorkbook workBook = new XSSFWorkbook(fis);
XSSFSheet sheet = workBook.getSheet("Tooling Layout");
WritableImage wi = new WritableImage(1080,790);
stackPane.snapshot(null, wi);
BufferedImage bImage = SwingFXUtils.fromFXImage(wi, null);
FileChooser saveFile = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PNG files", "*.PNG");
saveFile.getExtensionFilters().add(extFilter);
File imageFile = saveFile.showSaveDialog(mainStage);
ImageIO.write(bImage, ".png", imageFile);
InputStream is = new FileInputStream(imageFile);
byte[] bytes = IOUtils.toByteArray(is);
int pictureId = workBook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
is.close();
CreationHelper helper = workBook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(2);
Picture pict = drawing.createPicture(anchor, pictureId);
FileOutputStream fos = new FileOutputStream(filePath);
workBook.write(fos);
fis.close();
fos.close();
workBook.close();
}
}
我的 FXML
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Main">
<children>
<MenuBar prefHeight="67.0" prefWidth="640.0" style="-fx-background-color: white;" VBox.vgrow="NEVER" />
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" style="-fx-background-color: #00A5AD;" VBox.vgrow="ALWAYS">
<children>
<TabPane layoutY="-1.0" prefHeight="375.0" prefWidth="640.0" style="-fx-background-color: white;" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Tooling layout">
<content>
<AnchorPane fx:id="layoutPane" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Label layoutX="16.0" layoutY="115.0" text="Selected component Image:" />
<Button layoutX="4.0" layoutY="26.0" mnemonicParsing="false" onAction="#handleImage" style="-fx-background-color: black;" text="Upload Image of Component" textFill="WHITE" />
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="WHITE" height="319.0" layoutX="207.0" layoutY="17.0" scaleZ="0.0" stroke="BLACK" strokeType="INSIDE" width="426.0" />
<Button layoutX="3.0" layoutY="72.0" mnemonicParsing="false" onAction="#exportImage" style="-fx-background-color: black;" text="Export Layout to SpreadSheet" textFill="WHITE" />
<StackPane fx:id="stackPane" layoutX="217.0" layoutY="26.0" prefHeight="301.0" prefWidth="409.0" />
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
</children>
</VBox>
我的應用類:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application{
public void start(Stage primaryStage) throws Exception {
try{
FXMLLoader loader = new FXMLLoader(getClass().getResource("mainframe.fxml"));
Parent root = (Parent) loader.load();
Scene scene = new Scene(root);
primaryStage.setTitle("Excel Populator");
primaryStage.setScene(scene);
primaryStage.show();
}
catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
launch(args);
}
}
uj5u.com熱心網友回復:
所以我實際上能夠通過洗掉一些東西來讓我的代碼作業,并意識到我從來沒有真正創建一個 ImageIO 將寫入的檔案,本質上我能夠在一行中完成所有緩沖影像代碼和 FXutils 代碼并且每次保存快照時創建一個新檔案,以便 ImageIO 可以將我拍攝的快照寫入該 Png 檔案。
這些是我的錯誤,下面是我改變的:
Image finalImage = stackPane.snapshot(null, null);
FileChooser saveFile = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PNG files", "*.PNG");
saveFile.getExtensionFilters().add(extFilter);
File imageFile = saveFile.showSaveDialog(mainStage);
ImageIO.write(SwingFXUtils.fromFXImage(finalImage, null), "png", imageFile);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/497478.html
標籤:爪哇 用户界面 javafx apache-poi
上一篇:如何在顫動中創建這條曲線?