我使用javafx來構建一個GUI,包括帶有MenuBar和另一個GridPane的GridPane。我希望第二個GridPane是可滾動的,所以我把它放到ScrollPane「新的ScrollPane(grid2)」中,並將它添加到第一個GridPane中。不幸的是,如果窗口大小發生變化,ScrollPane的內容不再水平拉伸,需要什麼。ScrollBar中的GridPane,拉伸空白空間
這裏是一個事先知情同意,這應該clearify問題: http://img5.fotos-hochladen.net/uploads/scrollpaneemptwjpv2084xm.png (不能使用SOF-工具,因爲信譽< 10)
我想有TitledPages的一個大數字,在前面的按鈕他們,哪些可以滾動(不像菜單,應該保持在頂部)。
如果我只是將第二個GridPane添加到第一個,它以正確的方式拉伸,所以我想知道爲什麼ScrollPane或其元素不能被拉伸,以及如何。
我向您發佈我的代碼,感謝您閱讀本文,並感謝您的努力!
package main;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MainGUI6 extends JFrame {
private static final long serialVersionUID = -8824577253071530315L;
JFXPanel panel;
Scene scene;
boolean wait = true;
public MainGUI6() {
panel = new JFXPanel();
Platform.runLater(new Runnable() {
@Override
public void run() {
GridPane grid = new GridPane();
scene = new Scene(grid, 1000, 1000);
grid.setVgap(4);
grid.setPadding(new Insets(5, 5, 5, 5));
//Menu
final Menu menu1 = new Menu("Menu1");
final Menu menu2 = new Menu("Menu2");
final Menu menu3 = new Menu("Menu3");
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(menu1, menu2, menu3);
grid.add(menuBar, 0, 0);
grid.setHgrow(menuBar, Priority.ALWAYS);
//Second grid
GridPane grid2 = new GridPane();
//Buttons
Button b1 = new Button("ButtonI1");
Button b2 = new Button("ButtonI2");
Button b3 = new Button("ButtonI3");
grid2.add(b1, 0, 0);
grid2.add(b2, 0, 1);
grid2.add(b3, 0, 2);
//TitledPanes
TitledPane tp1 = new TitledPane("TitledPane1", new Button("Button1"));
TitledPane tp2 = new TitledPane("TitledPane2", new Button("Button2"));
TitledPane tp3 = new TitledPane("TitledPane3", new Button("Button3"));
grid2.add(tp1, 1, 0);
grid2.add(tp2, 1, 1);
grid2.add(tp3, 1, 2);
grid2.setHgrow(tp1, Priority.ALWAYS);
grid2.setHgrow(tp2, Priority.ALWAYS);
grid2.setHgrow(tp3, Priority.ALWAYS);
ScrollPane sp = new ScrollPane(grid2);
grid.add(sp, 0, 1);
grid.setHgrow(sp, Priority.ALWAYS);
panel.setScene(scene);
wait = false;
}
});
this.getContentPane().add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainGUI6();
}
});
}
}
雖然我讀過每一個ScrollPane方法,但顯然錯過了那一個。非常感謝! – user51705