1

2 つのボタンを重ねてパネルの上部に固定しようとしていますが、2 番目のボタンはパネルの中央にとどまります。

このコードでは、分割ペインを作成し、そこに 2 つのパネルを追加します。1 つのパネルにはボタン (関心のあるパネル) が含まれ、もう 1 つのパネルにはテキスト フィールドが含まれています。ボタンを使用すると、テキスト フィールドにテキストが表示されます。

GridBagLayout パラメーターの正しい組み合わせによって、2 つのボタンを (間に隙間なく) 重ねて配置し、それらをパネルの上部に固定できるようにする方法を見つけたいと思います。weighting と gridy パラメータを使用していくつかの異なることを試しましたが、役に立ちませんでした。

お時間をいただき、ご回答いただきありがとうございます。

package gridbagtest;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class GridBagTest
{
    public static void main(String[] args)
    {
        JFrame gridTest = new JFrame("Grid Bag Layout Test");

        JSplitPane splitPaneHorizontal = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        final JTextField blah = new JTextField(30);
        JPanel textPane = new JPanel();
        textPane.add(blah);

        JPanel buttonPane = new JPanel();
        buttonPane.setBackground(Color.WHITE);
        buttonPane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.gridx = 0;
//      c.gridy = GridBagConstraints.RELATIVE;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 1;

        JButton flightPlanButton = new JButton("First Button");

        flightPlanButton.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {   blah.setText("First Button pushed");    }
         });

        buttonPane.add(flightPlanButton, c);

        JButton powerButton = new JButton("Second Button");
        powerButton.addActionListener(new ActionListener()                            
        {
            public void actionPerformed(ActionEvent e) 
            {   blah.setText("Second Button pushed");   }
        });

        c.gridy = 1;
        buttonPane.add(powerButton, c);

        splitPaneHorizontal.setTopComponent(new JScrollPane(buttonPane));
        splitPaneHorizontal.setBottomComponent(textPane);   

        gridTest.add(splitPaneHorizontal);
        gridTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gridTest.pack();
        gridTest.setSize(600,800);
        gridTest.setVisible(true);

    }
}
4

2 に答える 2

2

1 つの問題は、空のスペースを埋めるためにボタンの下に何も追加していないことだと思います。これを解決するには多くの方法があります。1 つは、GridBagLayout を完全に回避し、JButton を BoxLayout を使用する JPanel に配置し、その JPanel を BorderLayout.PAGE_START (または NORTH) 位置で BorderLayout を使用する別の JPanel に配置することです。もう 1 つの例では、ボタンの重みを 0 に設定してから、空の JLabel などの別のコンポーネントを追加するか、ここではボックス クラスの接着剤を使用して、適切な重み、適切な高さを与えます。 、何が起こるか見てみましょう:

  JPanel buttonPane = new JPanel();
  buttonPane.setBackground(Color.WHITE);
  buttonPane.setLayout(new GridBagLayout());
  GridBagConstraints c = new GridBagConstraints();
  c.fill = GridBagConstraints.HORIZONTAL;
  c.anchor = GridBagConstraints.FIRST_LINE_START;
  c.gridx = 0;
  c.gridy = 0;
  c.weightx = 1;
  c.weighty = 0;

  JButton flightPlanButton = new JButton("First Button");

  flightPlanButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
        blah.setText("First Button pushed");
     }
  });

  buttonPane.add(flightPlanButton, c);

  JButton powerButton = new JButton("Second Button");

  c.gridy = 1;
  buttonPane.add(powerButton, c);

  c.gridy = 2;
  c.fill = GridBagConstraints.BOTH;
  c.anchor = GridBagConstraints.LINE_END;
  c.weighty = 100.0;
  c.gridheight = GridBagConstraints.REMAINDER;
  buttonPane.add(Box.createGlue(), c);

また、Devon_C_Miller が述べていること、フィールドを変更する場合は新しい GridBagConstraint を作成する必要があることは真実ではないと信じています。実際、Oracle は独自の GridBagLayout API でこれを否定しています。

Devon_C_Miller にもう 1 つ: 確かに、GridBagLayout ソース コードを確認したところ、クラスがこの情報をcomptableHashTable<Component, GridBagConstraints>という変数に格納し、情報がこのテーブルに配置されるメソッドで、GridBagConstraints オブジェクトが最初に複製されることがわかりました。テーブルに追加される前に、新しい GridBagConstraints オブジェクトを作成する必要がないことを完全に証明します (多くの場合、そうするのが良いプログラミング手法ですが、他の理由もあります)。setConstraints(...)

于 2012-07-13T23:56:04.137 に答える
0

新しい GridBagConstraints オブジェクトを追加し、それを 2 番目のボタンの追加に使用すると、コードを修正できます。

        GridBagConstraints d = 新しい GridBagConstraints();
        d.fill = GridBagConstraints.HORIZONTAL;
        d.gridx = 0;
        d.gridy = 0;
        d.weightx = 0;
        d.重み = 0;

次に、追加で使用します。

        buttonPane.add(flightPlanButton, d);

このコードを実行したところ、必要に応じてボタンが積み上げられました。

于 2012-07-14T02:39:49.817 に答える