JSwing simple button/JFXPanel layout

不打扰是莪最后的温柔 提交于 2019-12-25 01:44:58

问题


can someone please tell me the simplest way to achieve the layout in the image below in Java?

The JFXPanel should take all the screen space except for the button that should remain of the same size when the window is resized.

More generally, is there any LayoutManager in Java that lets me stack components one over the other in a simple way?

Everything I've tried makes the button way too large. Maybe the JFXPanel messes with the sizing, I don't know.

Thank you, this is driving me mad.


回答1:


  • Use nested JPanels, that's the key, each using their own layoututs.
  • BorderLayout for the whole thing, in the main JPanel.
  • A FlowLayout JPanel to hold the JButton, and place that JPanel in the main JPanel BorderLayout.PAGE_START
  • Place JFXPanel in the main JPanel BorderLayout.CENTER
  • Read the layout manager tutorial since this is all explained and shown there.

import java.awt.BorderLayout;
import java.awt.Dimension;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JavaFXSwingApplication1 extends JApplet {

    private static final int JFXPANEL_WIDTH_INT = 300;
    private static final int JFXPANEL_HEIGHT_INT = 250;
    private static JFXPanel fxContainer;
    private static JFXPanel fxContainerTwo;
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception e) {
                }
                JFrame frame = new JFrame("JavaFX embeded in Swing");
                frame.setLayout(new BorderLayout(5, 5));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JApplet applet = new JavaFXSwingApplication1();
                applet.init();
                frame.setContentPane(applet.getContentPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                applet.start();
            }
        });
    }

    @Override
    public void init() {
        fxContainer = new JFXPanel();
        fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT / 5, JFXPANEL_HEIGHT_INT / 5));
        add(fxContainer, BorderLayout.NORTH);
        fxContainerTwo = new JFXPanel();
        fxContainerTwo.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
        add(fxContainerTwo, BorderLayout.CENTER);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                createScene();
                createScene2();
            }
        });
    }

    private void createScene() {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        Scene scene = new Scene(root, Color.BLUEVIOLET);
        fxContainer.setScene(scene);
    }

    private void createScene2() {
        Button btn = new Button();
        btn.setText("Say 'Hello World' Two");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        Scene scene = new Scene(root, Color.ALICEBLUE);
        fxContainerTwo.setScene(scene);
    }
}


来源:https://stackoverflow.com/questions/20723289/jswing-simple-button-jfxpanel-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!