本文介绍了JSwing简单按钮/ JFXPanel布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 有人可以告诉我在Java下面的图片中实现布局的最简单方法吗?can someone please tell me the simplest way to achieve the layout in the image below in Java? JFXPanel应该占用除按钮之外的所有屏幕空间调整窗口大小时应保持相同的大小。The JFXPanel should take all the screen space except for the button that should remain of the same size when the window is resized. 更一般地说,Java中是否有任何LayoutManager允许我以一种简单的方式将组件叠加到另一个上?More generally, is there any LayoutManager in Java that lets me stack components one over the other in a simple way?我尝试的所有东西都会让按钮变得太大。也许JFXPanel与尺寸混淆,我不知道。Everything I've tried makes the button way too large. Maybe the JFXPanel messes with the sizing, I don't know.谢谢,这让我很生气。推荐答案 使用嵌套的JPanels,这是关键,每个都使用自己的布局。 BorderLayout用于整个事情,主要是JPanel。 一个FlowLayout JPanel来保存JButton,并将JPanel放在主JPanel BorderLayout.PAGE_START 放置JFXPanel主JPanel BorderLayout.CENTER 阅读布局管理器教程,因为这一切都在那里解释和显示。 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_STARTPlace JFXPanel in the main JPanel BorderLayout.CENTERRead 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); }} 这篇关于JSwing简单按钮/ JFXPanel布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 05:51