1

2 つのパネルで GridBagLayout をフレームの 40% と 60% にしようとしていますが、それらの内部にコンポーネントを含めることができますが、面倒です。

ボタンをパネル内に配置しないと、思いどおりに機能します。

何が間違っているのかよくわからず、ボタンの作成を GridBagLayout のパネルが作成された場所に移動しようとしましたが、それでも機能しませんでした。

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

public class Test{

public void display(){
    JFrame frame = new JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(900,650);
    frame.setVisible(true);

    JPanel test = new JPanel();
    test.setLayout(new GridBagLayout());
    GridBagConstraints c= new GridBagConstraints();

    JPanel left= new JPanel();
    JPanel right= new JPanel();

    c.fill = GridBagConstraints.VERTICAL - GridBagConstraints.HORIZONTAL;
    c.weightx = 0.4;
    c.gridx = 1;
    c.weighty = 1;
    test.add(left,c);
    c.weightx = .6;
    c.gridx = 2;
    test.add(right,c);

    JButton button= new JButton("A button");
    left.add(button,c);//If I do not add this, then it shows how I want it to be

    frame.add(test);
   }
}
4

2 に答える 2

1

重みの重要な点は、余分なスペースをどうするかを説明することです。コンポーネントには、レイアウト マネージャーがレイアウトを計算するときに使用する、推奨される最小および最大の寸法があります。GridBagLayout は、これらの重みを使用して余分なスペースを分割します。あなたの場合、分割されたスペースは900-button.getPreferredSize().widthに等しいと思います。おそらく 800 ピクセルを 320 と 480 に分割しています。

于 2012-01-15T18:32:59.220 に答える
0

以下は GridBagLayout で作成されたパネルの例です: (swing factory については気にせず、代わりにコンポーネントを作成してください)

private void buildSourcePanel() {

  JPanel pnlSource = new JPanel();

  GridBagLayout gbl_pnlSource = new GridBagLayout();
  gbl_pnlSource.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0};
  gbl_pnlSource.columnWidths = new int[]{0, 0, 100, 100, 25};
  pnlSource.setLayout(gbl_pnlSource);

  final JLabel lblFolderMask = swingFactory.createLabel(" SOURCE DIRECTORY ", null, null, SwingConstants.LEFT, SwingConstants.CENTER, true);
  pnlSource.add(lblFolderMask, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 5, 5, 0), 0, 0));

  txtSource = swingFactory.createTextField(null, "txtSource", null, SystemColor.textHighlight, SwingConstants.LEFT, false, true, "Source Directory");
  pnlSource.add(txtSource, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0));

  final JButton btnBrowse = new JButton("Browse...");
  btnBrowse.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 0, false), new EmptyBorder(5, 5, 5, 5)));
  btnBrowse.setFont(new Font("Verdana", Font.BOLD, 14));
  pnlSource.add(btnBrowse, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0));

  final JButton btnClear = new JButton("Clear...");
  btnClear.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 0, false), new EmptyBorder(5, 5, 5, 5)));
  btnClear.setFont(new Font("Verdana", Font.BOLD, 14));
  pnlSource.add(btnClear, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0));

  lblStatus = swingFactory.createLabel(null, null, null, SwingConstants.CENTER, SwingConstants.CENTER, false);
  pnlSource.add(lblStatus, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 5, 5, 5), 0, 0));
}
于 2012-01-15T11:31:33.500 に答える