A、GridBagLayoutのレイアウトマネージャとそのレイアウトのGridBagConstraintsパラメータ説明
主に以下の4つのパラメータのGridBagLayout:
列幅:列の数;例えば:gridBagLayout.columnWidths =新しいINT [] { 0}; 表すのみ
rowHeights:行数;例えば:gridBagLayout.rowHeights =新しいINT []は、{ 0、0}は、 2行の合計を表します。
columnWeights:各列の幅の割合を設定するステップと、gridBagLayout.columnWeights =新しいダブル[] { 1.0}; 列幅は、容器の幅、すなわち、容器支持満杯であることを特徴とする
rowWeights:各行シェアの高さの比率を設定するステップと、gridBagLayout.rowWeights =新しい{0.2,0.8}] [二重;;コンテナの最初の行の高さのみ2点の高さ、8部を表す第2の容器の行の高さを表します
次のようにGridBagContraintsは、パラメータ設定をシェル:
GridBagLayoutのレイアウトを使用する前に、次のパラメータを知っている必要があります。
次のコード例:
パブリック クラス ClientPanel 延びJPanelのは、{ / ** *パネルを作成する。 * / パブリックClientPanel(){ GridBagLayoutのGridBagLayoutの = 新しい新規のGridBagLayout(); gridBagLayout.columnWidths = 新しい新しい INT [] {0 }; //が設けられている のGridBagLayoutを。 rowHeightsがいる = 新しい新しい INT [] {0、0 }、二列のが設けられている// gridBagLayout.columnWeightsを = 新しい新しい ダブル [] {1.0 }; //コンテナの幅に列幅設定 gridBagLayout.rowWeights = 新しい新規の ダブル [] {0.2,0.8 }; 2つの部品容器を占め、最初の行の//高さは、8つの容器の第2行の高さを占め setLayoutの(GridBagLayoutの); JPanelのパネル = 新しい新しいJPanelの(); panel.setBackground (Color.PINK) のGridBagConstraints gbc_panel = 新しい新規のGridBagConstraints(); gbc_panel.insets = 新しい新しいインセット(0,0 ,. 5、0 ); gbc_panel.fill = GridBagConstraints.BOTHに; gbc_panel.gridx = 0 ; gbc_panel.gridy = 0 。 追加(パネル、gbc_panelは); JPanelのPanel_1は = 新しい新JPanelの(); panel_1.setBackground(Color.ORANGE)。 GridBagConstraints gbc_panel_1 = 新しいのGridBagConstraints(); gbc_panel_1.fill = GridBagConstraints.BOTHに。 gbc_panel_1.gridx = 0 ; gbc_panel_1.gridy = 1 。 (panel_1、gbc_panel_1)を追加します。 } }
結果は以下の通りであります:
次のコード:
public class ClientPanel extends JPanel { /** * Create the panel. */ public ClientPanel() { GridBagLayout gridBagLayout = new GridBagLayout(); gridBagLayout.columnWidths = new int[]{0, 0, 0,0}; //设置了4列 gridBagLayout.rowHeights = new int[]{0, 0}; //设置了2行 gridBagLayout.columnWeights = new double[]{0.25,0.25,0.25,0.25}; gridBagLayout.rowWeights = new double[]{0.2,0.8}; setLayout(gridBagLayout); JPanel panel = new JPanel(); panel.setBackground(Color.PINK); GridBagConstraints gbc_panel = new GridBagConstraints(); gbc_panel.insets = new Insets(0, 0, 5, 0); gbc_panel.fill = GridBagConstraints.BOTH; gbc_panel.gridx = 3; gbc_panel.gridy = 0; add(panel, gbc_panel); JPanel panel_1 = new JPanel(); panel_1.setBackground(Color.ORANGE); GridBagConstraints gbc_panel_1 = new GridBagConstraints(); gbc_panel_1.insets = new Insets(0, 0, 0, 5); gbc_panel_1.fill = GridBagConstraints.BOTH; gbc_panel_1.gridx = 0; gbc_panel_1.gridy = 1; add(panel_1, gbc_panel_1); } }
运行结果为: