【问题标题】:Location of the buttons go to a strange location (SpringLayout)按钮的位置转到一个奇怪的位置(SpringLayout)
【发布时间】:2016-12-11 05:54:00
【问题描述】:

我正在制作一个 java 应用程序,您可以在其中通过表单添加新帐户,我使用 SpringLayout 是因为它看起来很整洁,但是在添加带有文本的 JTextFields 后,按钮保持在左上方,而它不应该出现在那里,我正在使用 SpringUtilities (https://docs.oracle.com/javase/tutorial/uiswing/examples/layout/SpringGridProject/src/layout/SpringUtilities.java)

package dinges.Containers;

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

import dinges.Utilities.SpringUtilities;

@SuppressWarnings("serial")
public class Addnew extends JPanel {

    String[] options = {"User", "Accountant", "Administrator", "Developer"};

    /**
     *  > Add a text input for the following:
     *  > Id, Name, last name, current balance, and the state. But this has to be in order of the new Account.
     *  > we're just going to be using JTextFields, a JButton for saving and JLabels for writing what it is
     * 
     **/

    public Addnew() {
        // frame size is WIDTH =   280     ,      HEIGHT =     480
        SpringLayout layout = new SpringLayout();
        setLayout(layout);

        JButton save = new JButton("Save data");
        JTextField name = new JTextField(15);
        JTextField lastname = new JTextField(15);
        JComboBox<String> accounttype = new JComboBox<String>(options);
        JLabel label1 = new JLabel("First name: ", JLabel.TRAILING);
        JLabel label2 = new JLabel("Last name: ", JLabel.TRAILING);
        JLabel label3 = new JLabel("Account type: ", JLabel.TRAILING);
        JLabel label4 = new JLabel("Save data: ", JLabel.TRAILING);
        label1.setLabelFor(name);
        label2.setLabelFor(lastname);
        label3.setLabelFor(accounttype);




        add(label1);
        add(name);
        add(label2);
        add(lastname);
        add(label3);
        add(accounttype);
        add(save);
        add(label4);

        SpringUtilities.makeCompactGrid(this, 3, 2, 6, 6, 6, 6);
    }

}

这使它看起来像这样:

但是按钮应该在 JComboBox 下方,并且它的 JLabel 应该和其他按钮一样定位。

问题出在哪里?我已经换了一段时间了,但我真的找不到它。

【问题讨论】:

    标签: java swing layout-manager springlayout


    【解决方案1】:

    您似乎没有正确设置正确的行数

        // SpringUtilities.makeCompactGrid(p, rows, cols, initX, initY, xPad, yPad); 
        int numPairs = options.length; // 4
        SpringUtilities.makeCompactGrid(p, numPairs, 2, 6, 6, 6, 6); 
    

    【讨论】:

    • (1-),是的,这是一个小时前的建议。无需重复答案。此外,“选项”与行数无关。 “options”数组用于组合框。
    【解决方案2】:

    按钮停留在左上角

    它应该在哪里?

    我猜它应该在底部,但您应该在问题中说明这一点,因为我们不是读心者,也不知道您在想什么。

    我正在使用 SpringUtilities

    您是否告诉 SpringUtilities 您需要多少行/列?

    那是你修改了demo代码中的参数还是直接复制demo代码没有修改?

    【讨论】:

    • 我没有注意到这个,我习惯了自己编码东西,我只是按照朋友告诉我的spring实用程序,谢谢你提醒我
    猜你喜欢
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 2012-07-30
    • 1970-01-01
    相关资源
    最近更新 更多