简体   繁体   English

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

[英]JavaFX how to change the size of the scene via button.setOnAction

I am a Java and JavaFx newbie here, and I would like to ask a question. 我是这里的Java和JavaFx新手,我想问一个问题。 To put it simple, I have this one stage that has a button and a scene with a specific size, I declared the size of the scene with such coding: 简单来说,我有一个阶段,有一个按钮和一个特定大小的场景,我用这样的编码声明了场景的大小:

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




    }

}

so basically I am trying to increase the size of the current stage/scene with a click of a button, but somehow something went wrong and I got runtime error, it says "java.lang.IllegalArgumentException: Grid hgap=0.0, vgap=10.0, alignment=CENTERis already set as root of another scene", sorry that I did not share the full coding, it is very long to share it here, anyone mind to advice? 所以基本上我试图通过点击一个按钮来增加当前舞台/场景的大小,但不知何故出了问题我得到了运行时错误,它说“java.lang.IllegalArgumentException:Grid hgap = 0.0,vgap = 10.0 ,alignment = CENTER已经设置为另一个场景的根“,对不起,我没有分享完整的编码,这里很长时间分享它,有人介意建议吗? sorry for my poor English 抱歉我的英语不好

edit: to avoid any confusion, I'll share the full coding in the class, sorry for the inconvenience :( 编辑:为了避免任何混淆,我会在课堂上分享完整的编码,对不便之处抱歉:(

The exception clearly says that the root node of one scene is being tried to set as root to another scene. 例外情况清楚地表明,正在尝试将一个场景的根节点设置为另一个场景的根节点。

However the approach for changing the scene size is wrong. 但是,改变场景大小的方法是错误的。
So instead of creating new scene, change the size of stage. 因此,不要创建新场景,而是更改舞台的大小。
In other words, instead of 换句话说,而不是

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

just do 做就是了

setWidth(600);
setHeight(500);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM