3

簡単に指定できるダイアログを作成しようとしています。いくつかのラベルとその反対側にいくつかのテキスト領域があり、[OK]と[キャンセル]ボタンがあります。真ん中には幅の広いコンポーネントがあります(たとえば、ラベルを使用しました)

 |label|  [combo box]
 |label|  [txtfield]
 |label|  [txtfield]
 |  long label here |


 [btn1]      [btn2]

GridBagLayoutを使用しようとしていますが、希望どおりに機能していません。その理由がわかりません。基本的に、ボタンをダイアログの下部に固定する必要があります。

以下のコードは、私がそれをどのように使用しているかを示しています。

package gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class GridBagLayoutExample2 extends JFrame {

    private static final long serialVersionUID = -1972347726217162551L;
    final private JLabel lbl1 = new JLabel("LABEL1: ");
    final private JLabel lbl2 = new JLabel("LABEL2: ");
    final private JLabel lbl3 = new JLabel("LABEL3: ");
    final private JTextArea txt1 = new JTextArea(" ");
    final private JComboBox cmb1 = new JComboBox();
    final private JTextArea txt2 = new JTextArea("");
    final private JLabel lblLine = new JLabel("a compenent on all the line");
    final private JButton btnOK = new JButton("OK");
    final private JButton btnCancel = new JButton("Cancel");

    public GridBagLayoutExample2() {

        GridBagLayout bl = new GridBagLayout();
        Container pane = getContentPane();
        pane.setLayout(bl);
        GridBagConstraints c = new GridBagConstraints();
        int r = 0;

        placeComponentInGridBagLayout(lbl1, pane, bl, c, 0, r, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, null, null, null, null);
        placeComponentInGridBagLayout(cmb1, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
        placeComponentInGridBagLayout(lbl2, pane, bl, c, 0, r, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, null, null, null, null);
        placeComponentInGridBagLayout(txt2, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
        placeComponentInGridBagLayout(lbl3, pane, bl, c, 0, r, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
        placeComponentInGridBagLayout(txt1, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null);
        placeComponentInGridBagLayout(lblLine, pane, bl, c, 0, r++, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 2, null, null, null);
        placeComponentInGridBagLayout(btnOK, pane, bl, c, 0, null, null, null, GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL, null, null, null, null);
        placeComponentInGridBagLayout(btnCancel, pane, bl, c, 1, null, null, null, GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL, null, null, null, null);

        setSize(new Dimension(850, 350));
        pack();
        setVisible(true);
    }

    public static void placeComponentInGridBagLayout(Component component, Container container, GridBagLayout bagLayout, GridBagConstraints c, Integer gridX, Integer gridY, Double weightX, Double weightY, Integer anchor, Integer fill, Integer ipadx, Integer ipady,
            Integer gridWidth, Integer gridHeight) {
        if (c == null) {
            c = new GridBagConstraints();
        }
        if (gridX != null) {
            c.gridx = gridX;
        }
        if (gridY != null) {
            c.gridy = gridY;
        }
        if (weightX != null) {
            c.weightx = weightX;
        }
        if (weightY != null) {
            c.weighty = weightY;
        }
        if (fill != null) {
            c.fill = fill;
        }
        if (anchor != null) {
            c.anchor = anchor;
        }
        if (ipadx != null) {
            c.ipadx = ipadx;
        }
        if (ipady != null) {
            c.ipady = ipady;
        }
        if (gridWidth != null) {
            c.gridwidth = gridWidth;
        }
        if (gridHeight != null) {
            c.gridheight = gridHeight;
        }
        bagLayout.setConstraints(component, c);

        container.add(component);
    }

}

私が間違っていることについて何か考えはありますか?また、Swingで同じことを達成するためのより現代的な方法はありますか?

ありがとう

4

4 に答える 4

2

私が見ることができる1つの問題は、長いラベルが[OK]ボタンと同じグリッドセルに入っていることです。ダイアログ全体の下部にボタンを固定する場合は、を使用してみてくださいBorderLayout。ボタン用に別のパネルを作成し、それをに入れますBorderLayout.SOUTH。次に、メインのものをを使用して別のパネルに配置しGridBagLayoutBorderLayout.CENTER.それを配置します。ボタンはテキストコンポーネントと完全に整列しませんが、それで問題ないと思います。

于 2011-12-13T17:13:13.730 に答える
2

単純なフォームを作成する場合、SpringLayoutは便利なレイアウトマネージャーです。また、コンポーネントを1行にパックするには、BoxLayoutを使用すると便利です。

簡単な例を次に示します。

ここに画像の説明を入力してください

そしてここにコードがあります:

public class LayoutDemo extends JFrame {

    public LayoutDemo() {

        JLabel label1 = new JLabel("label 1");
        JLabel label2 = new JLabel("label 2");
        JLabel label3 = new JLabel("label 3");
        JComboBox<String> combobox = new JComboBox<>();
        JTextField field1 = new JTextField();
        JTextField field2 = new JTextField();

        JPanel formPanel = new JPanel(new SpringLayout());
        formPanel.add(label1);
        formPanel.add(combobox);
        formPanel.add(label2);
        formPanel.add(field1);
        formPanel.add(label3);
        formPanel.add(field2);

        SpringUtilities.makeCompactGrid(formPanel, 3, 2, 2, 2, 3, 3);

        JLabel longLabel = new JLabel("This is a longer label");
        longLabel.setAlignmentX(CENTER_ALIGNMENT);

        JButton button1 = new JButton("Button 1");
        JButton button2 = new JButton("Button 2");

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
        buttonPanel.add(button1);
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(button2);

        JPanel basePanel = new JPanel();
        basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.PAGE_AXIS));
        basePanel.add(formPanel);
        basePanel.add(longLabel);

        add(basePanel, BorderLayout.NORTH);
        add(buttonPanel, BorderLayout.SOUTH);
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new LayoutDemo();
    }
}

BorderLayoutを使用formPanelして、ウィンドウのサイズを変更するときに、ストレッチせずに上部を維持することができます。しかし、フォームにSpringLayoutを使用する方法についていくつかのアイデアを提供したことを願っています。

于 2011-12-13T18:48:07.907 に答える
2

以下に示すように、borderlayoutとgridlayoutを組み合わせて使用​​できます。

ここに画像の説明を入力してください

package sof;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CustomLayoutFrame{
    public static void main(String args[]) {
        JFrame frame = new JFrame("Custom Layout Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        JPanel centerPnl = new JPanel();
        centerPnl.setLayout(new BorderLayout());
        JPanel gridLayoutPane = new JPanel(); 
        GridLayout gridLayout = new GridLayout(3,2);
        gridLayoutPane.setLayout(gridLayout);
        gridLayoutPane.add(new JLabel("Label1"));
        gridLayoutPane.add(new JComboBox());
        gridLayoutPane.add(new JLabel("Label2"));
        gridLayoutPane.add(new JTextField());
        gridLayoutPane.add(new JLabel("Label3"));
        gridLayoutPane.add(new JTextField());
        centerPnl.add(gridLayoutPane, BorderLayout.CENTER);
        centerPnl.add(new JLabel("Long label should be going here"), BorderLayout.SOUTH);

        frame.add(centerPnl, BorderLayout.CENTER);

        JPanel bottomPanel = new JPanel();
        bottomPanel.add(new JButton("Button1"));
        bottomPanel.add(new JButton("Button2"));

        frame.add(bottomPanel, BorderLayout.SOUTH);

        frame.setSize(300, 150);
        frame.setVisible(true);
      }
}
于 2011-12-13T19:15:14.407 に答える
2

LayoutManagerこれらの目的に最適なのは、 FormLayoutJGoodiesのです。

于 2011-12-13T21:32:08.870 に答える