[英]JavaFX how to change the size of the scene via button.setOnAction
我是這里的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 );
}
}
所以基本上我試圖通過點擊一個按鈕來增加當前舞台/場景的大小,但不知何故出了問題我得到了運行時錯誤,它說“java.lang.IllegalArgumentException:Grid hgap = 0.0,vgap = 10.0 ,alignment = CENTER已經設置為另一個場景的根“,對不起,我沒有分享完整的編碼,這里很長時間分享它,有人介意建議嗎? 抱歉我的英語不好
編輯:為了避免任何混淆,我會在課堂上分享完整的編碼,對不便之處抱歉:(
例外情況清楚地表明,正在嘗試將一個場景的根節點設置為另一個場景的根節點。
但是,改變場景大小的方法是錯誤的。
因此,不要創建新場景,而是更改舞台的大小。
換句話說,而不是
Scene scene = new Scene(grid, 600, 500);
setScene(scene);
做就是了
setWidth(600);
setHeight(500);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.