간단한 자바 FX 웹보기 자바 스윙 응용 프로그램은 내용을 표시하지

Piovezan :

다음 코드는 자바 스윙을 생성 JFrame자바 FX를 여는 버튼으로 WebView웹보기 대신 내용 표시의 비어있는 열 그러나 때, 대화 상자 내부합니다 (URL의 내용 중 하나를 또는 "자바 FX를 오신 것을 환영합니다!"). 무엇이 잘못 될 수 있을까?

(참고 : 코드에 기반 ).

OpenUrlInJFrameAction.java :

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import java.util.Objects;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.web.WebView;

public class OpenUrlInJFrameAction implements ActionListener {

    private final JFrame parent;
    private final URI uri;

    public OpenUrlInJFrameAction(JFrame parent, URI uri) {
        this.parent = Objects.requireNonNull(parent);
        this.uri = Objects.requireNonNull(uri);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        SwingUtilities.invokeLater(() -> {
            // You should execute this part on the Event Dispatch Thread
            // because it modifies a Swing component
            JDialog jDialog = new JDialog(parent, true);
            JFXPanel jfxPanel = new JFXPanel();
            jDialog.add(jfxPanel);
            jDialog.setSize(800, 600);
            jDialog.setLocationRelativeTo(null);
            jDialog.setVisible(true);
            // Creation of scene and future interactions with JFXPanel
            // should take place on the JavaFX Application Thread
            Platform.runLater(() -> {

                // Uncomment either the lines below Test 1 or below Test 2, 
                // both are apparently ignored by the web view.

                // Test 1
                Scene scene = createScene();
                jfxPanel.setScene(scene);                  

                // Test 2
                /*WebView webView = new WebView();
                jfxPanel.setScene(new Scene(webView));
                webView.getEngine().load(uri.toString());*/
            });
        });
    }

    private Scene createScene() {
        Group root = new Group();
        Scene scene = new Scene(root, Color.ALICEBLUE);
        Text text = new Text();

        text.setX(40);
        text.setY(100);
        text.setFont(new Font(25));
        text.setText("Welcome JavaFX!");

        root.getChildren().add(text);

        return (scene);
    }      
}

JFrameTest.java :

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class JFrameTest extends JFrame {

    public JFrameTest(String title) {
        super(Objects.requireNonNull(title));
    }


    public static void main(String [] args) {
        SwingUtilities.invokeLater(() -> {
            JFrameTest jFrameTest = new JFrameTest("Test");
            jFrameTest.setDefaultCloseOperation(EXIT_ON_CLOSE);
            JButton jButton = new JButton("Open dialog");
            try {
                jButton.addActionListener(new OpenUrlInJFrameAction(jFrameTest,
                        new URI("https://stackoverflow.com")));
            } catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }
            jFrameTest.add(jButton);
            jFrameTest.pack();
            jFrameTest.setVisible(true);
        });
    }
}
trashgod :

귀하의 예를 몇 가지 문제를 제기한다 :

  • 당신은 만드는 모달 대화 상자에 기본적으로 ModalityType.APPLICATION_MODAL추가 업데이트를 차단을. 대신, 생성 모덜리스를 다음과 같이 대화 상자를.

    dialog = new JDialog(parent, Dialog.ModalityType.MODELESS);
    
  • 가장 좋은 방법은 응용 프로그램의 설계에 따라 달라집니다; 하지만, 논의 된 바와 같이 여기 하는 모덜리스의 대화는 더 유연 입증 할 수있다; 피하기 중복, 호출 할 toFront()초기 대화 인스턴스, 아래 그림과 같이.

  • 대신 구현하는 ActionListener, 연장 고려 AbstractAction아래 그림; 어떻게주의 Action재사용 할 수 있습니다.

  • 버튼의 ActionListener상의 화재 이벤트 발송 쓰레드 ; 대화 상자 생성을 다시 대기 할 필요 또는 이점이 없습니다.

  • 재정의 getPreferredSize()논의, 여기 당신의 대화의 초기, 빈 크기를 설정합니다.

틀 대화

마찬가지로 테스트 :

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.SwingUtilities;

/**
 * @see https://stackoverflow.com/a/54958587/230513
 */
public class JFrameTest extends JFrame {

    public JFrameTest(String title) {
        super(Objects.requireNonNull(title));
    }

    private static class OpenDialogAction extends AbstractAction {

        private final JFrame parent;
        private final URI uri;
        private JDialog dialog;

        public OpenDialogAction(JFrame parent, URI uri) {
            super.putValue(NAME, "Open " + uri.getAuthority());
            this.parent = Objects.requireNonNull(parent);
            this.uri = Objects.requireNonNull(uri);
        }

        @Override
        public void actionPerformed(ActionEvent event) {
            if (dialog != null) {
                dialog.toFront();
                return;
            }
            dialog = new JDialog(parent, Dialog.ModalityType.MODELESS);
            dialog.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    dialog = null;
                }
            });
            dialog.setTitle(uri.getAuthority());
            JFXPanel fxPanel = new JFXPanel() {

                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(640, 480);
                }
            };
            dialog.add(fxPanel);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
            Platform.runLater(() -> {
                WebView webView = new WebView();
                webView.getEngine().load(uri.toString());
                Scene scene = new Scene(webView);
                fxPanel.setScene(scene);
            });
        }
    }

    public static void main(String[] args) throws URISyntaxException {
        URI uri1 = new URI("https://www.example.com");
        URI uri2 = new URI("https://www.example.net");
        URI uri3 = new URI("https://www.example.org");
        SwingUtilities.invokeLater(() -> {
            JFrameTest test = new JFrameTest("Test");
            test.setLayout(new GridLayout(0, 1));
            test.add(new JButton(new OpenDialogAction(test, uri1)));
            test.add(new JButton(new OpenDialogAction(test, uri2)));
            test.add(new JButton(new OpenDialogAction(test, uri3)));
            test.pack();
            test.setDefaultCloseOperation(EXIT_ON_CLOSE);
            test.setLocationByPlatform(true);
            test.setVisible(true);
        });
    }
}

추천

출처http://43.154.161.224:23101/article/api/json?id=281855&siteId=1