4

これは、「 BorderLayoutの中心にコンポーネントの好ましいサイズを維持する」という質問の続きですが、ここからリンクする方法がわかりません。誰かがリンクを編集して、それがどのように行われたかを伝えるメッセージを私に送信したい場合は、それをいただければ幸いです。

その質問では、コンポーネントを適切なサイズに保つ方法と、BorderLayoutの中央にあるパネルをそのレイアウトの中央の左上隅に配置する方法(の代わりに)を尋ねて回答しました。真ん中に浮かびます。

私はそれを質問に合わせるために私の本当の問題を単純化しました、そして今私はそれを一般化するのに苦労しています。次のコードは、境界線レイアウトの中央にタブ付きペインを配置することを除いて、同様です。

私はいくつかのものを切り取りました:私のアプリでは、タブ付きペインは分割ペインの半分にあり、上にツールバー(別のボーダーレイアウトパネル)があり、下にナビゲーションボタンがあります。それなしで問題をデモできるので、ここからすべてを切り取りましたが、それは境界レイアウトを使用する理由があることを意味します。これは主に、これらすべての中央にあるパネルが余分なものを利用したいためです。ユーザーがそれらに与えなければならないスペース。

このコードでは、タブ付きのペインを取得していますが、ウィンドウから与えられた場合、その中の2つのパネルは余分なスペースの中央に浮かんでいます。1つのバージョン(コードの下部にある別の行を参照)では、それらのスクロールバーが表示されますが、他のバージョンでは表示されません。

タブ付きペイン内のコンポーネントを希望のサイズのままにしてほしい。このコードは、各ペインをgridbaglayoutパネルに配置することでこれを実行していますが、これには、私が望まない他の効果があります。

パネルが必要とするよりも大きい場合は、各タブ付きペインのコンテンツをスペースの左上隅に配置したいと思います。スペースが足りないときにスクロールバーを表示したいのですが。

それで、私がこれをどのように成し遂げるかについて他の意見がありますか?

package example;

import java.awt.BorderLayout;
import java.awt.GridBagLayout;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

public class BorderAndBox extends JFrame
{
    public static void main(String args[])
    {
        BorderAndBox bnb = new BorderAndBox();
        bnb.createUI();
        bnb.setLocationByPlatform(true);
        bnb.setVisible(true);
    }

    public void createUI()
    {
        JPanel borderPanel = new JPanel(new BorderLayout());

        JPanel innerPanelOne = new JPanel();
        innerPanelOne.setLayout(new BoxLayout(innerPanelOne, BoxLayout.LINE_AXIS));
        String[] firstChoices = { "first", "uno", "UN" };
        String[] secondChoices = { "second", "dos", "zwei" };
        String[] thirdChoices = { "third", "tres", "drei" };
        JComboBox firstCombo = new JComboBox(firstChoices);
        JComboBox secondCombo = new JComboBox(secondChoices);
        JComboBox thirdCombo = new JComboBox(thirdChoices);
        innerPanelOne.add(firstCombo);
        innerPanelOne.add(secondCombo);
        innerPanelOne.add(thirdCombo);
        JPanel innerPanelTwo = new JPanel();
        innerPanelTwo.setLayout(new BoxLayout(innerPanelTwo, BoxLayout.PAGE_AXIS));
        innerPanelTwo.add(new JLabel("additionalLabel"));

        JPanel panelA = new JPanel(new GridBagLayout());
        panelA.add(innerPanelOne);

        JPanel innerPanelThree = new JPanel();
        innerPanelThree.setLayout(new BoxLayout(innerPanelThree, BoxLayout.PAGE_AXIS));
        JLabel lblFirst = new JLabel("first in line");
        JLabel lblSecond = new JLabel("second to none");
        JLabel lblThird = new JLabel("third the bird");
        JLabel lblFourth = new JLabel("fourth");
        JLabel lblFifth = new JLabel("fifth");
        JLabel lblSixth = new JLabel("sixth");
        innerPanelThree.add(lblFirst);
        innerPanelThree.add(lblSecond);
        innerPanelThree.add(lblThird);
        innerPanelThree.add(lblFourth);
        innerPanelThree.add(lblFifth);
        innerPanelThree.add(lblSixth);
        JPanel panelB = new JPanel(new GridBagLayout());
        panelB.add(innerPanelThree);

        JScrollPane scrollPane1 = new JScrollPane(panelA);
        JScrollPane scrollPane2 = new JScrollPane(panelB);

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add("one", scrollPane1);
        tabbedPane.add("two", scrollPane2);


        JPanel westPanel = new JPanel(new BorderLayout());
        JPanel northPanel = new JPanel(new BorderLayout());
        northPanel.add(tabbedPane, BorderLayout.NORTH);
        westPanel.add(northPanel, BorderLayout.WEST);
        // the following lines are mutually exclusive; 
        // use only the first, you have no scroll bars,
        // use only the second, you do have them
        borderPanel.add(westPanel, BorderLayout.CENTER);
//          borderPanel.add(tabbedPane, BorderLayout.CENTER);

        getContentPane().add(tabbedPane);
        pack();
    }



}
4

1 に答える 1

4

あなたがやろうとしていることを見逃しているかもしれませんが、欠けているように見えるのは、GridBagLayout を使用するコンテナーにコンポーネントを追加するための GridBagConstraints を設定するcodeEです。たとえば、

  GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
        GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, 
        new Insets(0, 0, 0, 0), 0, 0);
  panelA.add(innerPanelOne, gbc);

  //...

  panelB.add(innerPanelThree, gbc);
于 2012-03-03T01:24:08.367 に答える