Layout GridPanel BorderPanel按钮手风琴

时间:2013-05-09 23:04:15

标签: java javafx-2

上下文:

  • Windows 7 64位
  • Public JDK 7“1.7.0_13”build 1.7.0_13-b20

这是SSCCE的两个快照,用于显示布局的奇怪行为,有或没有手风琴作为GridPane中的单元格,重现这些行为的代码,最后是我问的问题。

良好的渲染,正如预期的那样,BorderPane的“中心”内容是标签:

Good rendering, as expected

按钮布局不好,BorderPane的“中心”内容是Accordion:

Bad layout of the button

代码:

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

public class SSCCE extends Application {

   public static final boolean TOO_BIG_BUTTON = true;
//   public static final boolean TOO_BIG_BUTTON = false;

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

   @SuppressWarnings("nls")
   @Override
   public void start( Stage stage ) {
      TextField tf = new TextField( "textfield" );
      BorderPane bp = new BorderPane();
      Label lv = new Label( "BorderPane - Center - Center - Center - Center - Center" );
      lv.setPrefHeight( 96.0 );
      if( TOO_BIG_BUTTON ) {
         Accordion  acc = new Accordion();
         TitledPane tp  = new TitledPane( "Titled pane", lv );
         acc.getPanes().add( tp );
         acc.setExpandedPane( tp );
         stage.setTitle( "SSCCE - too large button" );
         bp.setCenter( acc );
      }
      else {
         stage.setTitle( "SSCCE - as expected" );
         bp.setCenter( lv );
      }
      GridPane gp = new GridPane();
      Label lbl = new Label( "Label: " );
      Button btn = new Button( "button" );
      btn.setMaxWidth( Double.MAX_VALUE );
      gp.add( lbl, 0, 1 );
      gp.add( tf , 1, 1 );
      gp.add( btn, 2, 1 );
      gp.add( bp , 0, 2, 3, 1 );
      GridPane.setHgrow( tf , Priority.ALWAYS );
      GridPane.setHgrow( btn, Priority.NEVER  );
      GridPane.setHalignment( btn, HPos.RIGHT );
      stage.setScene( new Scene( gp ));
      stage.centerOnScreen();
      stage.show();
   }
}

问题:

  • 为什么?
  • 如何帮助布局管理器计算合适的按钮大小?

编辑Bug entered into JavaFX bug tracking system Jira.

1 个答案:

答案 0 :(得分:1)

您的JavaFX版本是什么?我尝试使用最新版本(Java 8 EAP b88),结果如下:

enter image description here

相关问题