5

BorderLayoutを使用する中規模のUIがあります。中央は、さまざまなレイアウトなどのさまざまなパネルを含むタブ付きペインです。

この境界線レイアウトの中央にあるパネルをウィンドウのサイズに応じてサイズ変更したいのですが、パネル内のコンポーネントを拡大したくありません。ラベル、コンボボックス、テキストフィールド、ボタン-好みのサイズのままにして、それらを含むパネルを引き伸ばせるようにします。パネルのスペースが小さすぎる場合に備えて、スクロールペインに配置します。

カラフルな語彙を備えたさまざまなポスターは、コンポーネントでsetXXXsize()メソッドを使用することの危険性を警告しています。それが今私がしていることであり、それを回避する方法を学びたいと思います。

GridBagLayoutは、一部のパネルには適していません。本来、行と列を中心に配置されており、すべてが行と列に収まるわけではありません。もちろん、すべてに合うように人工的な行と列を作成することもできますが、Swingにそれよりも多くのレイアウトオプションがあることを本当に望んでいます。

垂直接着剤もそれを行いません。私はそれをHFOEの最愛のSSCEに含めました:

    package example;

    import java.awt.BorderLayout;

    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

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

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

            JLabel northLabel = new JLabel("Nawth");
            borderPanel.add(northLabel, BorderLayout.NORTH);

            String[] southComboChoices = { "one", "two", "three" };
            JComboBox southCombo = new JComboBox(southComboChoices);
            borderPanel.add(southCombo, BorderLayout.SOUTH);

            JPanel centerPanel = new JPanel();
            centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_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);
            centerPanel.add(firstCombo);
            centerPanel.add(secondCombo);
            centerPanel.add(thirdCombo);
            centerPanel.add(Box.createVerticalGlue());  // first attempt; does NOT
            // take up available vertical space, instead it appears to create a space
            // that is shared equally among the (now) four components of this space.
            borderPanel.add(centerPanel, BorderLayout.CENTER);

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

    }

ウィンドウを拡大すると、中央のコンボボックスが拡大します。書かれているように、それらの下の垂直接着剤も拡大しますが、利用可能なすべてのスペースを占めるわけではありません。それぞれと同じくらいのスペースが与えられているようです。

では、これに取り組む良い方法は何ですか?

4

2 に答える 2

11

BorderAndBox-中央揃え

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

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

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

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

    JLabel northLabel = new JLabel("Nawth");
    borderPanel.add(northLabel, BorderLayout.NORTH);

    String[] southComboChoices = { "one", "two", "three" };
    JComboBox southCombo = new JComboBox(southComboChoices);
    borderPanel.add(southCombo, BorderLayout.SOUTH);

    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_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);
    centerPanel.add(firstCombo);
    centerPanel.add(secondCombo);
    centerPanel.add(thirdCombo);
    centerPanel.add(Box.createVerticalGlue());  // first attempt; does NOT
    // take up available vertical space, instead it appears to create a space
    // that is shared equally among the (now) four components of this space.
    JPanel centerPanelConstrain = new JPanel(new GridBagLayout());
    centerPanelConstrain.add(centerPanel);
    borderPanel.add(centerPanelConstrain, BorderLayout.CENTER);

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

}

この回答も参照してください。これを解決する方法は複数あります。

于 2012-03-02T16:53:31.173 に答える
-2

JPanelと一緒に使用することをお勧めしますjavax.swing.GroupLayout。ただし、このレイアウトをコーディングして使用するのは簡単ではありません。Netbeans Matisse Builderなどのコードジェネレータを使用し、IDEを使用したくない場合は、コピーして好きな場所に貼り付けます。

于 2012-03-02T17:16:58.770 に答える