Java Swingnode不可见图像保存错误

时间:2016-10-11 17:06:27

标签: java swing javafx

在尝试拍摄包含SwingNode和JavaFX元素的BorderLayout的快照时,在生成的图像中,Swingnode似乎位于右下角。我没有看到BorderLayout时得到这些结果。我在下面的测试程序中重现了这个问题。单击导出按钮时,可见和不可见选项卡应保存为具有相同图片的两个不同文件,但“invisible_file.png”图片不应位于右下角。

tab_hide_document.fxml

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.embed.swing.SwingNode?>


 <VBox xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TabController" prefHeight="500">
    <Button text="Export" fx:id="export" onAction="#handleExport"/>
    <TabPane fx:id="tabPane" minWidth="500">
    <tabs>
        <Tab text="visible tab" fx:id="visibleTab">
            <BorderPane fx:id="visibleBP" minHeight="300" minWidth="300">
            <center>
                <SwingNode fx:id="visibleNode"></SwingNode>
            </center>
            <bottom>
                <Slider></Slider>
            </bottom>
            </BorderPane>
        </Tab>   
         <Tab text="invisible tab" fx:id="invisibleTab">
            <BorderPane fx:id="invisibleBP" minHeight="300" minWidth="300">
            <center>
                <SwingNode fx:id="invisibleNode"></SwingNode>
            </center>
            <bottom>
                <Slider></Slider>
            </bottom>
            </BorderPane>
        </Tab>
    </tabs>
    </TabPane>
 </VBox>

TabController.java

import java.awt.BorderLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import javafx.embed.swing.SwingFXUtils;
import javafx.embed.swing.SwingNode;
import javafx.fxml.FXML;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;

public class TabController {

    @FXML
    private TabPane tabPane;

    @FXML
    private Tab visibleTab, invisibleTab;

    @FXML
    private SwingNode visibleNode, invisibleNode;

    @FXML
    private Button export;

    @FXML
    private BorderPane invisibleBP, visibleBP;

    private JPanel visiblePanel, invisiblePanel;

    public void initialize()
    {
        this.visiblePanel = new JPanel(new BorderLayout());
        this.invisiblePanel = new JPanel(new BorderLayout());       

        URL url = null;
        Image image = null;
        try{
            url = new URL("http://devstickers.com/assets/img/pro/d1i3.png");
            image = ImageIO.read(url);
        } catch (IOException e){ e.printStackTrace(); }

        ImageIcon img1 = new ImageIcon(image);
        this.visiblePanel.add(new JLabel(img1), BorderLayout.CENTER);       
        this.visibleNode.setContent(this.visiblePanel); 

        ImageIcon img2 = new ImageIcon(image);
        this.invisiblePanel.add(new JLabel(img2), BorderLayout.CENTER);         
        this.invisibleNode.setContent(this.invisiblePanel);

    }

    @FXML
    private void handleExport()
    {
        File visibleFile = new File("visible_file.png");
        File invisibleFile = new File("invisible_file.png");
        System.out.println("Export these pictures");
        try {
            ImageIO.write(SwingFXUtils.fromFXImage(this.invisibleBP.snapshot(new SnapshotParameters(), null), null), "png", invisibleFile);
            ImageIO.write(SwingFXUtils.fromFXImage(this.visibleBP.snapshot(new SnapshotParameters(), null), null), "png", visibleFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

TabExportTester.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class TabExportTester extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(TabController.class.getResource("tab_hide_document.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setHeight(1000);
            primaryStage.setWidth(1500);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

0 个答案:

没有答案