为什么每当我输入文本区域时,我的所有GUI组件都会移动到JFrame的左上角?

时间:2014-11-05 22:15:39

标签: java swing layout layout-manager null-layout-manager

我是Java和Stack Overflow的新手,我正在制作一个注册表单,用于收集用户数据并将其存储在结果窗口中的JLabel中。每个窗口都是在一个单独的类中制作的。我在原始窗口中有一个提交按钮,其代码如下所示,调用生成的窗口,将从文本字段和区域收集的用户数据放入JLabel。

我的问题是:每当我输入我的一个文本区域时,我的所有组件都会移动到窗口的左上角?

以前习惯工作正常。直到我将布局从null更改为spring,然后我停止从JFrame扩展并改为生成JFrame对象。到目前为止,我的另一个班级很好,只是这个给我带来麻烦。

这是用户输入文本的原始窗口的代码:

public class RegistrationForm1{
private JFrame frame;
private JButton submit;
private JLabel fnLabel, lnLabel, dobLabel, phoneLabel, cellLabel, 
ecLabel, ecnLabel, employerLabel, workLabel, alLabel, 
rLabel, ptlLabel, pcpLabel, pcpPhoneLabel;
private JTextArea alBox, rBox; 
private JTextField fnBox, lnBox, dobBox, phoneBox, cellBox, 
ecBox, ecnBox, employerBox, workBox, ptlBox, pcpBox, 
pcpPhoneBox;

public RegistrationForm1(){

    frame = new JFrame("RegistrationForm");
    frame.setLayout(new SpringLayout());
    frame.setSize(500, 570);
    frame.setResizable(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

public void CloseFrame(){
    frame.dispose();
}

public void form1(){

    fnLabel = new JLabel("First Name: ");
    fnLabel.setBounds(114, 0, 100, 50);
    frame.add(fnLabel);
    fnBox = new JTextField(20);
    fnBox.setBounds(185, 15, 100, 20);
    fnBox.setEditable(true);
    frame.add(fnBox);

    lnLabel = new JLabel("Last Name: ");
    lnLabel.setBounds(115, 30, 100, 50);
    frame.add(lnLabel);
    lnBox = new JTextField(20);
    lnBox.setBounds(185, 47, 100, 20);
    lnBox.setEditable(true);
    frame.add(lnBox);

    dobLabel = new JLabel("Date of Birth: ");
    dobLabel.setBounds(101, 60, 100, 50);
    frame.add(dobLabel);
    dobBox = new JTextField(10);
    dobBox.setBounds(185, 77, 100, 20);
    dobBox.setEditable(true);
    frame.add(dobBox);

    phoneLabel = new JLabel("Phone#: ");
    phoneLabel.setBounds(134, 90, 100, 50);
    frame.add(phoneLabel);
    phoneBox = new JTextField(10);
    phoneBox.setBounds(185, 105, 130, 20);
    phoneBox.setEditable(true);
    frame.add(phoneBox);

    cellLabel = new JLabel("Cell#: ");
    cellLabel.setBounds(148, 120, 100, 50);
    frame.add(cellLabel);
    cellBox = new JTextField(10);
    cellBox.setBounds(185, 135, 130, 20);
    cellBox.setEditable(true);
    frame.add(cellBox);

    ecLabel = new JLabel("Emergency Contact: ");
    ecLabel.setBounds(59, 150, 200, 50);
    frame.add(ecLabel);
    ecBox = new JTextField(20);
    ecBox.setBounds(185, 165, 200, 20);
    ecBox.setEditable(true);
    frame.add(ecBox);

    ecnLabel = new JLabel("Emergency Contact Number: ");
    ecnLabel.setBounds(5, 180, 300, 50);
    frame.add(ecnLabel);
    ecnBox = new JTextField(20);
    ecnBox.setBounds(185, 197, 150, 20);
    ecnBox.setEditable(true);
    frame.add(ecnBox);

    employerLabel = new JLabel("Employer: ");
    employerLabel.setBounds(121, 210, 100, 50);
    frame.add(employerLabel);
    employerBox = new JTextField(20);
    employerBox.setBounds(185, 225, 200, 20);
    employerBox.setEditable(true);
    frame.add(employerBox);

    workLabel = new JLabel("Work#: ");
    workLabel.setBounds(139, 240, 100, 50);
    frame.add(workLabel);
    workBox = new JTextField(10);
    workBox.setBounds(185, 255, 150, 20);
    workBox.setEditable(true);
    frame.add(workBox);

    alLabel = new JLabel("Allergies: ");
    alLabel.setBounds(124, 270, 100, 50);
    frame.add(alLabel);
    alBox = new JTextArea();
    alBox.setBounds(185, 285, 200, 50);
    alBox.setEditable(true);
    frame.add(alBox);

    rLabel = new JLabel("Reason for visit: ");
    rLabel.setBounds(81, 330, 120, 50);
    frame.add(rLabel);
    rBox = new JTextArea();
    rBox.setBounds(185, 350, 200, 50);
    rBox.setEditable(true);
    frame.add(rBox);

    ptlLabel = new JLabel("Pain Threshold Level: ");
    ptlLabel.setBounds(50, 394, 150, 50);
    frame.add(ptlLabel);
    ptlBox = new JTextField();
    ptlBox.setBounds(185, 410, 30, 20);
    ptlBox.setEditable(true);
    frame.add(ptlBox);

    pcpLabel = new JLabel("Primary Care Physician: ");
    pcpLabel.setBounds(35, 420, 150, 50);
    frame.add(pcpLabel);
    pcpBox = new JTextField(20);
    pcpBox.setBounds(185, 440, 200, 20);
    pcpBox.setEditable(true);
    frame.add(pcpBox);

    pcpPhoneLabel = new JLabel("PCP Phone#: ");
    pcpPhoneLabel.setBounds(103, 450, 100, 50);
    frame.add(pcpPhoneLabel);
    pcpPhoneBox = new JTextField(10);
    pcpPhoneBox.setBounds(185, 470, 150, 20);
    pcpPhoneBox.setEditable(true);
    frame.add(pcpPhoneBox);

    submit = new JButton("Submit");
    submit.setBounds(200, 500, 70, 40);
    frame.add(submit);
    submit.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    RegistrationForm2 apollo = new RegistrationForm2();
                    apollo.form2(fnBox, lnBox, dobBox, phoneBox, cellBox, 
                            ecBox, ecnBox, employerBox, workBox, alBox, rBox, 
                            ptlBox, pcpBox, pcpPhoneBox);
                    CloseFrame();
                }
            }
            );
}
}

0 个答案:

没有答案