3

これを実現するために GridBagLayout レイアウト マネージャーを使用しようとしています。

ここに画像の説明を入力

しかし、私が現在得ているのはこれです:

ここに画像の説明を入力

問題は、オレンジ色と茶色/灰色のパネルが 2 番目の列を占有するはずですが、コードの実行に関しては 3 番目の列のみを占有したいように見えることです。

レイアウトに使用しているコード:

 Container contentPane = form.getContentPane();
    contentPane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    JPanel pnlGame = new JPanel();
    pnlGame.setBackground(Color.green); //temp
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 2;
    c.gridheight = 2;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.85;
    c.weighty = 0.65;
    contentPane.add(pnlGame, c);

    JPanel pnlBuy = new JPanel();
    c.gridx = 2;
    pnlBuy.setBackground(Color.blue); //temp
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.15;
    c.weighty = 0.46;
    contentPane.add(pnlBuy, c);

    JPanel pnlUpgrade = new JPanel();
    pnlUpgrade.setBackground(Color.yellow); //temp
    c.gridx = 2;
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.15;
    c.weighty = 0.19;
    contentPane.add(pnlUpgrade, c);

    JPanel pnlStats = new JPanel();
    pnlStats.setBackground(Color.red); //temp
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 1;
    c.gridheight = 2;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.61;
    c.weighty = 0.35;
    contentPane.add(pnlStats, c);

    JPanel pnlSpeed = new JPanel();
    pnlSpeed.setBackground(Color.orange); //temp
    c.gridx = 1;
    c.gridy = 2;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.38;
    c.weighty = 0.04;
    contentPane.add(pnlSpeed, c);

    JPanel pnlRounds = new JPanel();
    pnlRounds.setBackground(Color.gray); //temp
    c.gridx = 2;
    c.gridy = 3;
    c.gridwidth = 2;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.38;
    c.weighty = 0.31;
    contentPane.add(pnlRounds, c);

それで、私は何を間違っていますか?私の英語が少し下手だったらすみません、そして/または私が犯している間違いが目に見えないほど明らかでしたら...それは朝の20時から5時です、そして私は長い一日を過ごしました. おそらく干し草を打つはずです、かなりすぐに。

アップデート:

茶色/灰色のパネルのグリッド幅を変更すると、すべてが適切に整列しているように見えますが、レイアウトに厄介なギャップが生じます。ここ:

i.imgur.com/6JUx2.png

パネルのコード (Kevin S によって提案された修正を含む):

JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);

それで、私が間違っていることはありますか、それともこれは、私が一緒に暮らす必要のある GridBagLayout の奇妙な動作ですか?

残念ながら、私の編集のおかげで、Bala R が親切にそこに入れてくれたすべての埋め込みが失われました。というわけで、画像のリンクに戻ってしまいました。そして今、私は 2 つ以上のハイパーリンクを投稿できないようです.リンクは最後のもので殺されました.コピーして貼り付ける必要があります.

ありがとう、サム

4

4 に答える 4

1

中央の列のすべてのコンポーネントは、少なくとも 1 つの他の列にも含まれています。したがって、GridBagLayout は、中央の列の優先幅を 0 として計算します。これが、表示されている効果です。

中央の列の幅を広げたい場合は、この列にのみあるコンポーネントをそこに配置します。

于 2011-03-27T14:30:00.533 に答える
0

レイアウトを機能させたいだけですか?

私はいくつかの変更を行いました。

 Container contentPane = form.getContentPane();
     contentPane.setLayout(new GridBagLayout());
     GridBagConstraints c = new GridBagConstraints();

     JPanel pnlGame = new JPanel();
     pnlGame.setBackground(Color.green); //temp
     c.gridx = 0;
     c.gridy = 0;
     c.gridwidth = 1; // one row
     c.gridheight = 1; // one column
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.60;
     c.weighty = 0.40;
     contentPane.add(pnlGame, c);

     // Added two new components
     // pnlgGame and pnlggGame

     // Covers up 2nd column and rows 0 and 1
     JPanel pnlgGame = new JPanel();
     pnlgGame.setBackground(Color.green); //temp
     c.gridx = 1;
     c.gridy = 0;
     c.gridwidth = 1; // one row
     c.gridheight = 2; // two column
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.20;
     c.weighty = 0.40;
     contentPane.add(pnlgGame, c);

     // Covers 2nd row and column 1 
     JPanel pnlggGame = new JPanel();
     pnlggGame.setBackground(Color.green); //temp
     c.gridx = 0;
     c.gridy = 1;
     c.gridwidth = 1;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.60;
     c.weighty = 0.20;
     contentPane.add(pnlggGame, c);


     JPanel pnlBuy = new JPanel();
     pnlBuy.setBackground(Color.blue); //temp
     c.gridx = 2;
     c.gridy = 0;
     c.gridwidth = 1;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.20;
     c.weighty = 0.40;
     contentPane.add(pnlBuy, c);

     JPanel pnlUpgrade = new JPanel();
     pnlUpgrade.setBackground(Color.yellow); //temp
     c.gridx = 2;
     c.gridy = 1;
     c.gridwidth = 1;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.20;
     c.weighty = 0.20;
     contentPane.add(pnlUpgrade, c);

     JPanel pnlSpeed = new JPanel();
     pnlSpeed.setBackground(Color.BLUE); //temp
     c.gridx = 1;
     c.gridy = 2;
     c.gridwidth = 2;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.40;
     c.weighty = 0.05;
     contentPane.add(pnlSpeed, c);

     JPanel pnlStats = new JPanel();
     pnlStats.setBackground(Color.red); //temp
     c.gridx = 0;
     c.gridy = 2;
     c.gridwidth = 1;
     c.gridheight = 2;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.60;
     c.weighty = 0.40;
     contentPane.add(pnlStats, c);


     JPanel pnlRounds = new JPanel();
     pnlRounds.setBackground(Color.gray); //temp
     c.gridx = 1;
     c.gridy = 3;
     c.gridwidth = 2;
     c.gridheight = 1;
     c.fill = GridBagConstraints.BOTH;
     c.weightx = 0.40;
     c.weighty = 0.35;
     contentPane.add(pnlRounds, c);

     form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     form.setVisible(true);
于 2011-03-27T15:04:45.947 に答える
0

したがって、pnlRounds JPanel を修正する必要があるようです。

JPanel pnlRounds = new JPanel();
pnlRounds.setBackground(Color.gray); //temp
c.gridx = 1; // NEEDS TO BE 1 (NOT 2)
c.gridy = 3;
c.gridwidth = 2;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.38;
c.weighty = 0.31;
contentPane.add(pnlRounds, c);

あなたの問題は、実際にそこにあるべきものが c.gridx = 2いつコードにあったかということです。c.gridx = 1

また、補足として、下部パネルの weightx は 1.00 に追加されず、.99 に追加されます。知っておくべきだと思っただけです。

于 2011-03-27T05:02:15.667 に答える
0

正確な解決策ではありませんが、一時的な回避策のようなものです。この新しいパネルを追加すると

    JPanel pnlTemp = new JPanel();
    pnlTemp.setBackground(Color.pink); //temp
    c.gridx = 1;
    c.gridy = 3;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 0.38;
    c.weighty = 0.31;
    contentPane.add(pnlTemp, c);

パネルを灰色にした後、レイアウトが修正されます。理由はわかりませんが、あなた(または他の誰か)が正しい解決策を見つけられるまで、これが役立つかもしれません. この新しいピンクのパネルは表示されませんが、レイアウトを修正するのに役立ちます。施工後のレイアウトはこんな感じ

ここに画像の説明を入力

于 2011-03-27T14:06:08.480 に答える