0

ユーザー名、パスワードなどの入力値を持つフォームを作成する必要があります。これがどのように見えるかの画像へのリンクです。http://img196.imageshack.us/img196/1727/empdesign.png

これらの属性はすべて画面の端に配置され、中央に配置されることはありません。メソッド JComponenet.setHorizo​​ntalAlignment(SwingConstants.LEFT) を使用してみましたが、これも何もしません。JLabels と JPanels でその方法を試しましたが、どちらも効果がありません。

これが私のコードです:

public class tada extends GUIDesign{

  //making all the jlabels to be placed

JLabel usernameLabel = new JLabel("username");
JLabel passwordLabel = new JLabel("Name");
JLabel empIDLabel = new JLabel("empID");
JLabel SalaryLabel = new JLabel("Salary");
JLabel EmployerLabel = new JLabel(" Salary");
JLabel hoursLabel = new JLabel("Gender");

JLabel departmentLabel = new JLabel("Department");



 //making all the textfields, combo boxes, and etc. 
JTextField usernameField = new JTextField(20);
JTextField passwordField = new JTextField(20);
JTextField empIDField = new JTextField(20);


JTextField SalaryField = new JTextField(20);
JTextField EmployerField = new JTextField(20);
String[] hourss = {"Fall", "Spring", "Summer"};
JComboBox hoursBox = new JComboBox(hourss);

JCheckBox[] departmentCheckBoxesBoxs = {new JCheckBox("Department 1"), 
                                        new JCheckBox("Department 2"), 
                                        new JCheckBox("Department 3"), 
                                        new JCheckBox("Department 4"), 
                                        new JCheckBox("Department 5")};






String[] Salary = {"section", "combo", "box"};
JComboBox sectionBox = new JComboBox(Salary);


//making all the panels to be placed inside the main panel. 

JPanel usernamePanel = new JPanel();
JPanel passwordPanel = new JPanel();
JPanel empIDPanel = new JPanel();
JPanel SalaryPanel = new JPanel();
JPanel EmployerPanel = new JPanel();
JPanel hoursPanel = new JPanel();
JPanel departmentsPanel = new JPanel();

JPanel top, bottom;


JButton submitButton = new JButton("Submit");

public tada(){

//これにより、パネルがスーパークラスから初期化されます。スーパークラスから継承された本当に重要なものは何もありません。super("雇用主のデザイン", 10);

    setPreferredSize(new Dimension(400,600));

//すべてのラベル、テキスト フィールドなどをサブパネルに追加し、サブパネルをメイン パネルの下部に追加します。setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS)); トップ = 新しい JPanel(); top.setSize(getWidth(), 30); 下 = 新しい JPanel(); bottom.setLayout(新しい BoxLayout(bottom,BoxLayout.Y_AXIS));

    top.setBackground(Color.red);
    title.setFont(new Font("Helvetica", 1,20));
    top.add(title);
    bottom.setBackground(Color.yellow);

    usernamePanel.add(usernameLabel);
    usernamePanel.add(usernameField);
    bottom.add(usernamePanel);

    passwordPanel.add(passwordLabel);
    passwordPanel.add(passwordField);
    bottom.add(passwordPanel);

    empIDPanel.add(empIDLabel);
    empIDPanel.add(empIDField);
    bottom.add(empIDPanel);

    SalaryPanel.add(SalaryLabel);
    SalaryPanel.add(SalaryField);
    bottom.add(SalaryPanel);

    EmployerPanel.add(EmployerLabel);
    EmployerPanel.add(EmployerField);
    bottom.add(EmployerPanel);

    hoursPanel.add(hoursLabel);
    hoursPanel.add(hoursBox);
    bottom.add(hoursPanel);

    departmentsPanel.add(departmentLabel);
    for(JCheckBox jbc: departmentCheckBoxesBoxs)
    {
        departmentsPanel.add(jbc);
    }
    bottom.add(departmentsPanel);




    add(top);
    add(bottom);


}

// また、私は boxLayout() を使用しており、y_axis に配置されているため、y 軸の配置を自動的に行うと思っていましたが、そうではありません。

4

2 に答える 2

2

コードを考えると、私が見ることができる最も簡単な方法は、メインのコンテナー レイアウト マネージャーを変更することです。ここでは、必要に応じて各コンポーネントの個々の要件を変更できるように、グリッド バッグ レイアウトを使用しました。

ここに画像の説明を入力

public class BadLayout05 {

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

    public BadLayout05() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new tada());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class tada extends JPanel {

        //making all the jlabels to be placed
        JLabel usernameLabel = new JLabel("username");
        JLabel passwordLabel = new JLabel("Name");
        JLabel empIDLabel = new JLabel("empID");
        JLabel SalaryLabel = new JLabel("Salary");
        JLabel EmployerLabel = new JLabel(" Salary");
        JLabel hoursLabel = new JLabel("Gender");
        JLabel departmentLabel = new JLabel("Department");
        //making all the textfields, combo boxes, and etc.
        JTextField usernameField = new JTextField(20);
        JTextField passwordField = new JTextField(20);
        JTextField empIDField = new JTextField(20);
        JTextField SalaryField = new JTextField(20);
        JTextField EmployerField = new JTextField(20);
        String[] hourss = {"Fall", "Spring", "Summer"};
        JComboBox hoursBox = new JComboBox(hourss);
        JCheckBox[] departmentCheckBoxesBoxs = {new JCheckBox("Department 1"),
            new JCheckBox("Department 2"),
            new JCheckBox("Department 3"),
            new JCheckBox("Department 4"),
            new JCheckBox("Department 5")};
        String[] Salary = {"section", "combo", "box"};
        JComboBox sectionBox = new JComboBox(Salary);
//making all the panels to be placed inside the main panel.
        JPanel usernamePanel = new JPanel();
        JPanel passwordPanel = new JPanel();
        JPanel empIDPanel = new JPanel();
        JPanel SalaryPanel = new JPanel();
        JPanel EmployerPanel = new JPanel();
        JPanel hoursPanel = new JPanel();
        JPanel departmentsPanel = new JPanel();
        JPanel top = new JPanel();
        JPanel bottom = new JPanel();
        JButton submitButton = new JButton("Submit");

        public tada() {

//this initializes the panel from superclass. nothing really important inherited from superclass. super("employer design", 10);

        // This is bad idea...
//            setPreferredSize(new Dimension(400, 600));

//adding all the labels, text fields, etc to the sub-panels, and adding subpanels to main pannel, bottom. setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS)); top = new JPanel(); top.setSize(getWidth(), 30); bottom = new JPanel(); bottom.setLayout(new BoxLayout(bottom,BoxLayout.Y_AXIS));

            top.setBackground(Color.red);
            JLabel title = new JLabel("Employer Design");
            // This is a bad idea - IHMO
            title.setFont(new Font("Helvetica", 1, 20));
            top.add(title);
            bottom.setBackground(Color.yellow);

            usernamePanel.add(usernameLabel);
            usernamePanel.add(usernameField);
            bottom.add(usernamePanel);

            passwordPanel.add(passwordLabel);
            passwordPanel.add(passwordField);
            bottom.add(passwordPanel);

            empIDPanel.add(empIDLabel);
            empIDPanel.add(empIDField);
            bottom.add(empIDPanel);

            SalaryPanel.add(SalaryLabel);
            SalaryPanel.add(SalaryField);
            bottom.add(SalaryPanel);

            EmployerPanel.add(EmployerLabel);
            EmployerPanel.add(EmployerField);
            bottom.add(EmployerPanel);

            hoursPanel.add(hoursLabel);
            hoursPanel.add(hoursBox);
            bottom.add(hoursPanel);

            departmentsPanel.add(departmentLabel);
            for (JCheckBox jbc : departmentCheckBoxesBoxs) {
                departmentsPanel.add(jbc);
            }

            GridBagConstraints gbc = new GridBagConstraints();
            setLayout(new GridBagLayout());
            gbc.gridx = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(top, gbc);
            gbc.weightx = 0;
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.WEST;
            add(usernamePanel, gbc);
            add(passwordPanel, gbc);
            add(empIDPanel, gbc);
            add(SalaryPanel, gbc);
            add(EmployerPanel, gbc);
            add(hoursPanel, gbc);
            add(departmentsPanel, gbc);
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(bottom, gbc);

        }
    }
}

より良い解決策は、プライマリ フィールドを単一のパネルに追加し、GridBagLayout代わりに

ここに画像の説明を入力

public class BadLayout05 {

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

    public BadLayout05() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new tada());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class tada extends JPanel {

        //making all the jlabels to be placed
        JLabel usernameLabel = new JLabel("username");
        JLabel passwordLabel = new JLabel("Name");
        JLabel empIDLabel = new JLabel("empID");
        JLabel SalaryLabel = new JLabel("Salary");
        JLabel EmployerLabel = new JLabel(" Salary");
        JLabel hoursLabel = new JLabel("Gender");
        JLabel departmentLabel = new JLabel("Department");
        //making all the textfields, combo boxes, and etc.
        JTextField usernameField = new JTextField(20);
        JTextField passwordField = new JTextField(20);
        JTextField empIDField = new JTextField(20);
        JTextField SalaryField = new JTextField(20);
        JTextField EmployerField = new JTextField(20);
        String[] hourss = {"Fall", "Spring", "Summer"};
        JComboBox hoursBox = new JComboBox(hourss);
        JCheckBox[] departmentCheckBoxesBoxs = {new JCheckBox("Department 1"),
            new JCheckBox("Department 2"),
            new JCheckBox("Department 3"),
            new JCheckBox("Department 4"),
            new JCheckBox("Department 5")};
        String[] Salary = {"section", "combo", "box"};
        JComboBox sectionBox = new JComboBox(Salary);
        JPanel fields = new JPanel();
        JPanel departmentsPanel = new JPanel();
        JPanel top = new JPanel();
        JPanel bottom = new JPanel();
        JButton submitButton = new JButton("Submit");

        public tada() {

            top.setBackground(Color.red);
            JLabel title = new JLabel("Employer Design");
            title.setFont(new Font("Helvetica", 1, 20));
            top.add(title);
            bottom.setBackground(Color.yellow);

            fields.setLayout(new GridBagLayout());
            GridBagConstraints gbcLabels = new GridBagConstraints();
            GridBagConstraints gbcFields = new GridBagConstraints();

            gbcLabels.gridx = 0;
            gbcLabels.gridy = 0;
            gbcLabels.anchor = GridBagConstraints.WEST;
            gbcLabels.insets = new Insets(2, 2, 2, 2);

            gbcFields.gridx = 1;
            gbcFields.gridy = 0;
            gbcFields.anchor = GridBagConstraints.WEST;
            gbcFields.weightx = 1;
            gbcFields.insets = new Insets(2, 2, 2, 2);

            fields.add(usernameLabel, gbcLabels);
            fields.add(usernameField, gbcFields);

            gbcFields.gridy = ++gbcLabels.gridy;

            fields.add(passwordLabel, gbcLabels);
            fields.add(passwordField, gbcFields);

            gbcFields.gridy = ++gbcLabels.gridy;

            fields.add(empIDLabel, gbcLabels);
            fields.add(empIDField, gbcFields);

            gbcFields.gridy = ++gbcLabels.gridy;

            fields.add(SalaryLabel, gbcLabels);
            fields.add(SalaryField, gbcFields);

            gbcFields.gridy = ++gbcLabels.gridy;

            fields.add(EmployerLabel, gbcLabels);
            fields.add(EmployerField, gbcFields);

            gbcFields.gridy = ++gbcLabels.gridy;

            fields.add(hoursLabel, gbcLabels);
            fields.add(hoursBox, gbcFields);

            departmentsPanel.add(departmentLabel);
            for (JCheckBox jbc : departmentCheckBoxesBoxs) {
                departmentsPanel.add(jbc);
            }

            GridBagConstraints gbc = new GridBagConstraints();
            setLayout(new GridBagLayout());
            gbc.gridx = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(top, gbc);
            gbc.weightx = 0;
            gbc.fill = GridBagConstraints.NONE;
            gbc.anchor = GridBagConstraints.WEST;
            add(fields, gbc);
//            add(passwordPanel, gbc);
//            add(empIDPanel, gbc);
//            add(SalaryPanel, gbc);
//            add(EmployerPanel, gbc);
//            add(hoursPanel, gbc);
            add(departmentsPanel, gbc);
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(bottom, gbc);

        }
    }
}
于 2012-11-21T23:35:47.357 に答える
0

BoxLayout パネルを Y 軸に揃えますこれは、それらがすべて垂直に積み重ねられているために見られます。

コンポーネントを希望どおりに右側に配置するには、各サブパネルにレイアウト マネージャーを追加する必要があります (usernamePanel、passwordPanel など)。BoxLayoutこれを行う 1 つの方法は、X 軸上に整列する新しいfor each を使用することです。次に、必ずsetAlignment()それぞれJLabelの andを呼び出しJTextFieldて、それらが左側に並ぶようにします。

于 2012-11-21T23:10:59.760 に答える