JavaFX如何通过button.setOnAction更改场景的大小

时间:2014-03-18 10:02:44

标签: java javafx

我是这里的Java和JavaFx新手,我想问一个问题。简单来说,我有一个阶段,有一个按钮和一个特定大小的场景,我用这样的编码声明了场景的大小:

public class ClientBaseDialog
extends Stage
{

GridPane grid;
ScrollPane scroll;
Boolean counter = false;

public ClientBaseDialog( String Label1, String Label2 )
{

    setResizable( false );
    final Label label = new Label( Label1 );
    final Label label2 = new Label( Label2 );

    label2.setWrapText( true );


    BorderPane borderPane = new BorderPane();

    final VBox myView = new VBox();
    label.setWrapText( true );
    myView.getChildren().addAll( label2 );

    Button closeButton = new Button( "Close" );
    closeButton.setOnAction( new EventHandler<ActionEvent>()
    {
        @Override
        public void handle( ActionEvent t )
        {
            ClientBaseDialog.this.close();

        }
    } );

    Button detailButton = new Button( "Detail" );
    detailButton.setOnAction( new EventHandler<ActionEvent>()
    {
        @Override
        public void handle( ActionEvent t )
        {
            if ( counter == false )
            {
                grid.add( scroll, 1, 5 );
                counter = true;

                   Scene scene = new Scene( grid, 600, 500 );
                   setScene( scene );
            }

            else
            {

                grid.getChildren().remove( scroll );
                counter = false;
            }

        }
    } );

    label.setWrapText( true );
    label.setPrefSize( 500, 250 );
    label2.setWrapText( true );

    HBox hbox = new HBox();
    HBox hbox2 = new HBox();

    hbox.setAlignment( Pos.CENTER );
    hbox2.setAlignment( Pos.CENTER );

    hbox.getChildren().add( closeButton );
    hbox2.getChildren().add( detailButton );
    hbox.setSpacing( 10 );

    scroll = new ScrollPane();
    scroll.setContent( myView );
    scroll.setPrefHeight(300);
    scroll.setHbarPolicy( ScrollPane.ScrollBarPolicy.NEVER );
    borderPane.setBottom( scroll );
    scroll.setFitToWidth( true );

    grid = new GridPane();
    ColumnConstraints col1 = new ColumnConstraints();
    col1.setPercentWidth( 5 );
    ColumnConstraints col2 = new ColumnConstraints();
    col2.setPercentWidth( 90 );
    ColumnConstraints col3 = new ColumnConstraints();
    col3.setPercentWidth( 5 );
    grid.getColumnConstraints().addAll( col1, col2, col3 );

    RowConstraints row1 = new RowConstraints();
    row1.setPercentHeight( 10 );
    RowConstraints row2 = new RowConstraints();
    row2.setPercentHeight( 2);
    RowConstraints row3 = new RowConstraints();
    row3.setPercentHeight( 2 );
    grid.getRowConstraints().addAll( row1, row2, row3 );

    grid.setHgap( 0 );
    grid.setVgap( 10 );
    grid.setPadding( new Insets( 0, 10, 0, 10 ) );

    grid.add( label, 1, 0 );
    grid.add( hbox, 1, 3 );
    grid.add( hbox2, 1, 4 );
    grid.setAlignment( Pos.CENTER );
    Scene scene = new Scene( grid, 600, 250 );
    setScene( scene );




    }

}

所以基本上我试图通过点击一个按钮来增加当前舞台/场景的大小,但不知何故出了问题我得到了运行时错误,它说&#34; java.lang.IllegalArgumentException:Grid hgap = 0.0,vgap = 10.0,alignment = CENTER已经设置为另一个场景的根&#34;,对不起,我没有分享完整的编码,这里分享它很长,有人介意建议吗?抱歉我的英语不好

编辑:为了避免任何混淆,我会在课堂上分享完整的编码,抱歉给您带来不便:(

1 个答案:

答案 0 :(得分:2)

该例外明确表示正在尝试将一个场景的根节点设置为另一个场景的根。

然而,改变场景大小的方法是错误的 因此,不要创建新场景,而是改变舞台的大小 换句话说,而不是

Scene scene = new Scene(grid, 600, 500);
setScene(scene);

只是做

setWidth(600);
setHeight(500);