2

JPaneを次のようにレイアウトしたい:

-------
|     |
|     |
|     |
-------
|     |
-------

このように、上部セクションは下部セクションよりも大きく/高くなっています (上部セクションは別の JPanel で構成され、Graphics オブジェクトを使用して画像を表示します。下部セクションも別の JPanel で構成されていますが、Graphics オブジェクトを使用していくつかの線を描画します)。およびテキスト)。

これを行う最善の方法は、GridBagLayout と GridBagConstraints を使用することだと聞いたことがあります。

GridBagConstraints の適切なプロパティを見つけようとしていますが、いくつか問題があります。これは私がこれまでに持っているものです...

上部には、次のものがあります。

gridx = 0
gridy = 0
weighty = 1.0; // expand downwards, because the bottom should never expand in the Y direction
fill = GridBagConstraints.BOTH

下部には、次のものがあります。

gridx = 0
gridy = 1
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.PAGE_END

残念ながら、最終的には大きな灰色の長方形が表示されるだけです(アプリケーションの背景は白です)-画像が読み込まれず、線/テキストが表示されません。

私は何をすべきか?何を調整すればよいですか?

私はいくつかのチュートリアルを読みましたが、本当に混乱しているように見えます.最初のアプリケーションではうまくいきましたが、今これをやろうとするとうまくいかないようです.

4

3 に答える 3

5

一般に、gridbag レイアウトの場合

  • コンポーネントのスケールが必要な場合は、そのスケール方向に重みを与える必要があり、その方向に設定したサイズ (幅/高さ) はレイアウト マネージャーによって無視されます。

  • コンポーネントのスケールが必要ない場合は、コンポーネントのサイズを定義する必要があります (必要に応じて、Java のドキュメントでこのトピックを掘り下げることができます)。ボトムパネルの場合、少なくとも好みの高さを指定する必要があります。

これはあなたの期待通りに働くことができます

pnlTop.setBackground(Color.WHITE);
pnlBottom.setBackground(Color.BLUE);

// Because you don't want the bottom panel scale, you need to give it a height.
// Because you want the bottom panel scale x, you can give it any width as the
// layout manager will ignore it.
pnlBottom.setPreferredSize(new Dimension(1, 20));


getContentPane().setLayout(new GridBagLayout());
GridBagConstraints cst = new GridBagConstraints();
cst.fill = GridBagConstraints.BOTH;
cst.gridx = 0;
cst.gridy = 0;
cst.weightx = 1.0; // --> You miss this for the top panel
cst.weighty = 1.0;
getContentPane().add(pnlTop, cst);

cst = new GridBagConstraints();
cst.fill = GridBagConstraints.HORIZONTAL;
cst.gridx = 0;
cst.gridy = 1;
cst.weightx = 1.0; // You miss this for the bottom panel
cst.weighty = 0.0;
getContentPane().add(pnlBottom, cst);

さらに、gridbag レイアウトを使用する場合は、painless-gridbag ライブラリhttp://code.google.com/p/painless-gridbag/を試すことをお勧めします(私はそのライブラリの作成者です)。この問題は解決しませんが (問題は gridbag レイアウトでコンポーネントのサイズを管理することに関係しているため)、多くの入力を節約し、コードの保守を容易にします。

pnlBottom.setPreferredSize(new Dimension(1, 20));

PainlessGridBag gbl = new PainlessGridBag(getContentPane(), false);
gbl.row().cell(pnlTop).fillXY();
gbl.row().cell(pnlBottom).fillX();
gbl.done();
于 2012-01-26T05:45:37.440 に答える
2

あなたの質問からはわかりませんが、トップが 70%、ボトムが 30% になるかもしれません。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class BorderPanels extends JFrame {

    private static final long serialVersionUID = 1L;

    public BorderPanels() {
        getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel panel1 = new JPanel();
        Border eBorder = BorderFactory.createEtchedBorder();
        panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
        gbc.gridx = gbc.gridy = 0;
        gbc.gridwidth = gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.weightx = gbc.weighty = 70;
        getContentPane().add(panel1, gbc);
        JPanel panel2 = new JPanel();
        panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
        gbc.gridy = 1;
        gbc.weightx = 30;
        gbc.weighty = 30;
        gbc.insets = new Insets(2, 2, 2, 2);
        getContentPane().add(panel2, gbc);
        pack();
    }

    public static void main(String[] args) {
        new BorderPanels().setVisible(true);
    }
}
于 2012-01-26T08:02:35.187 に答える
0

サードパーティのライブラリを使用する可能性がある場合は、FormLayout. 各行の高さとサイズ変更動作を指定できます。

私はこれをテストしませんでしたが、

FormLayout layout = new FormLayout( 
 "pref", //columns
 "50dlu:grow(0.2)","25dlu:grow(0.1)" //rows
);

トリックを行うかもしれません。構文の詳細については、JGoodies Forms pdfを参照してください。

于 2012-01-26T07:35:32.273 に答える