2013-12-21 24 views
0

有人能告訴我最簡單的方法來實現下面的圖像在Java中的佈局嗎?JSwing簡單按鈕/ JFXPanel佈局

JFXPanel應該佔用窗口大小調整後應保持相同大小的按鈕以外的所有屏幕空間。

enter image description here

更一般地,有沒有佈局管理在Java中,讓我堆棧組件了另一種簡單的方式?

我試過的一切都會讓按鈕的方式太大。也許JFXPanel與大小混亂,我不知道。

謝謝你,這讓我很生氣。

回答

3
  • 使用嵌套的JPanels,這是關鍵,每個都使用自己的佈局。
  • BorderLayout爲整個事情,在主JPanel。
  • 一個FlowLayout中的JPanel舉行的JButton和地方的JPanel主的JPanel BorderLayout.PAGE_START
  • 將JFXPanel主的JPanel BorderLayout.CENTER
  • 閱讀佈局管理教程,因爲這是所有解釋,並表示有。

enter image description here

enter image description here

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); 
    } 
}