1

他のそれぞれのフィールドに従って、チェックボックス(赤い強調表示された領域)を整列させたい。

ここに画像の説明を入力

これは、これを生成するために使用している主な方法です

public class DialogTesting extends JFrame{

public static void main(String args[])
{

    JFrame frame = new JFrame();
    frame.setSize(320,250);
    frame.setLocation(400,400);

    JTextField txtUserName,txtHostName,txtPortNo,txtSID;
    JPasswordField txtPassword;
    JPanel mainPanel;
    JCheckBox chkBoxSaveConnection;

    mainPanel = new JPanel();
    mainPanel.setBorder(BorderFactory.createEmptyBorder());
    mainPanel.setPreferredSize(new Dimension(300, 210));
    mainPanel.setLayout(new FlowLayout());


    JLabel l_label = null;
    txtUserName = new JTextField("K_USERNAME", 15);
    txtUserName.putClientProperty("maxlength", 200);
    l_label = new JLabel("User Name");
    l_label.setPreferredSize(new Dimension(100, 30));
    mainPanel.add(l_label);
    mainPanel.add(txtUserName);

    txtPassword = new JPasswordField("K_PASSWORD", 15);
    txtUserName.putClientProperty("maxlength", 200);
    l_label = new JLabel("Password");
    l_label.setPreferredSize(new Dimension(100, 30));
    mainPanel.add(l_label);
    mainPanel.add(txtPassword);

    txtHostName = new JTextField("K_HOSTNAME", 15);
    txtHostName.putClientProperty("maxlength", 200);
    l_label = new JLabel("Host Name");
    l_label.setPreferredSize(new Dimension(100, 30));
    mainPanel.add(l_label);
    mainPanel.add(txtHostName);

    txtPortNo = new JTextField("K_PORTNO", 15);

    l_label = new JLabel("Port Number");
    l_label.setPreferredSize(new Dimension(100, 30));
    txtPortNo.putClientProperty("maxlength", 200);
    mainPanel.add(l_label);
    mainPanel.add(txtPortNo);


    txtSID = new JTextField("K_SID", 15);
    l_label = new JLabel("SID number");
    l_label.setPreferredSize(new Dimension(100, 30));
    txtPortNo.putClientProperty("maxlength", 200);
    mainPanel.add(l_label);
    mainPanel.add(txtSID);

    chkBoxSaveConnection = new JCheckBox();
    l_label = new JLabel("chkBoxSaveConnection");
    l_label.setPreferredSize(new Dimension(150, 30));
    mainPanel.add(l_label);
    mainPanel.add(chkBoxSaveConnection);

    mainPanel.setVisible(true);

    frame.add(mainPanel);
    frame.setVisible(true);

}
}

ここで、チェックボックス(赤い強調表示された領域)を他のフィールドに従って整列させたい

このソリューションを試して、正しく整列させました

mainPanel.setLayout(new GridLayout()); 
GridBagConstraints l_bag_constraints = new GridBagConstraints();
l_bag_constraints.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(jlabel,FieldMapperHelper.getGridBagCompPosition(l_bag_constraints,0,0,10,1,0,10)
            );
    mainPanel.add(txtUserName
            ,FieldMapperHelper.getGridBagCompPosition(l_bag_constraints,0,1,10,1,0,10)
            );

しかし、この場合、非常に小さなテキスト ボックスが表示されます。

それ以外に欲しいものがあれば教えてください。

4

1 に答える 1

2

さて、あなたの問題は、適切な LayoutManager を使用しておらず、優先サイズを設定することを余儀なくされていることです (決してそれを行うべきではありません)。

これはあなたのコードの更新版で、GridBagLayoutうまく動作するはずです:

結果イメージ

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class DialogTesting {

    protected void initUI() {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextField txtUserName, txtHostName, txtPortNo, txtSID;
        JPasswordField txtPassword;
        JPanel mainPanel;
        JCheckBox chkBoxSaveConnection;

        GridBagConstraints firstCol = new GridBagConstraints();
        firstCol.weightx = 1.0;
        firstCol.anchor = GridBagConstraints.WEST;
        firstCol.insets = new Insets(5, 20, 5, 5);

        GridBagConstraints lastCol = new GridBagConstraints();
        lastCol.gridwidth = GridBagConstraints.REMAINDER;
        lastCol.weightx = 1.0;
        lastCol.fill = GridBagConstraints.HORIZONTAL;
        lastCol.insets = new Insets(5, 5, 5, 20);

        mainPanel = new JPanel(new GridBagLayout());

        JLabel l_label = null;
        txtUserName = new JTextField("K_USERNAME", 15);
        txtUserName.putClientProperty("maxlength", 200);
        l_label = new JLabel("User Name");
        mainPanel.add(l_label, firstCol);
        mainPanel.add(txtUserName, lastCol);

        txtPassword = new JPasswordField("K_PASSWORD", 15);
        txtUserName.putClientProperty("maxlength", 200);
        l_label = new JLabel("Password");
        mainPanel.add(l_label, firstCol);
        mainPanel.add(txtPassword, lastCol);

        txtHostName = new JTextField("K_HOSTNAME", 15);
        txtHostName.putClientProperty("maxlength", 200);
        l_label = new JLabel("Host Name");
        mainPanel.add(l_label, firstCol);
        mainPanel.add(txtHostName, lastCol);

        txtPortNo = new JTextField("K_PORTNO", 15);

        l_label = new JLabel("Port Number");
        txtPortNo.putClientProperty("maxlength", 200);
        mainPanel.add(l_label, firstCol);
        mainPanel.add(txtPortNo, lastCol);

        txtSID = new JTextField("K_SID", 15);
        l_label = new JLabel("SID number");
        txtPortNo.putClientProperty("maxlength", 200);
        mainPanel.add(l_label, firstCol);
        mainPanel.add(txtSID, lastCol);

        chkBoxSaveConnection = new JCheckBox();
        l_label = new JLabel("chkBoxSaveConnection");
        mainPanel.add(l_label, firstCol);
        mainPanel.add(chkBoxSaveConnection, lastCol);

        frame.add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DialogTesting().initUI();
            }
        });
    }
}
于 2012-12-11T13:11:18.180 に答える