Java GUI窗口没有任何输出

时间:2016-07-03 09:12:36

标签: java user-interface window frame pane

问候环的团契,

以下任务设置在你身上。

你的主程序不会在窗口上输出我想要的结果。

框架弹出但面板不起作用,因此没有任何按钮和控件出现。

这是你的使命, 你应该选择接受它吗?

import javax.swing.*;
import java.awt.*;        
import java.awt.Container.*;  

public class newAccount
{    

static JFrame bankFrame;
JButton buttonCreate;

// Panel Variable

static JPanel panelObject;

// Label Variables

JLabel labelName;
JLabel labelDOB;
JLabel labelGender;
JLabel labelAge;
JLabel labelPartner;
JLabel labelCountry;
JLabel labelCity;
JLabel labelAddress;
JLabel labelZipPostal;

// Data Entry Control Variables

JTextField textName;
JTextField textDOB;
JComboBox comboGender;
JTextField textAge;
JTextField textPartner;
JComboBox comboCountry;
JTextField textCity;
JTextField textAddress;
JTextField textZipPostal;

// Layout Variables

GridBagLayout gbObject;
GridBagConstraints gbc;

public void newAccount()
{
    // Initializing Laoyout Variables

    gbObject = new GridBagLayout();
    gbc = new GridBagConstraints();
    panelObject = new JPanel();
    panelObject.setLayout(gbObject);


    // Initializing Label Controls

    labelName = new JLabel ("Name");
    labelDOB = new JLabel ("Date of Birth");
    labelGender = new JLabel ("Gender");
    labelAge = new JLabel ("Age");
    labelPartner = new JLabel ("Partner");
    labelCountry = new JLabel ("Country");
    labelCity = new JLabel ("City");
    labelAddress = new JLabel ("Address");
    labelZipPostal = new JLabel ("Zip/Postal Code");

    // Data Entry Controls

    textName = new JTextField(50);
    textDOB = new JTextField(9);

    String packages[] = {"Male", "Female", "Other"};
    comboGender = new JComboBox (packages);

    textAge = new JTextField(3);
    textPartner = new JTextField(50);

    String packages1[] = {"Oman", "India", "United Arab Emirates", "United States of America", "United Kingdon"};
    comboCountry = new JComboBox (packages1);

    textCity = new JTextField(50);
    textAddress = new JTextField(100);
    textZipPostal = new JTextField(10);

    // Controls for Name

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 10;
    gbObject.setConstraints (labelName, gbc);
    panelObject.add (labelName);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 10;
    gbObject.setConstraints (textName, gbc);
    panelObject.add (textName);

    // Controls for Date of Birth

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 30;
    gbObject.setConstraints (labelDOB, gbc);
    panelObject.add (labelDOB);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 30;
    gbObject.setConstraints (textDOB, gbc);
    panelObject.add (textDOB);

    // Controls for Gender

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 50;
    gbObject.setConstraints (labelGender, gbc);
    panelObject.add (labelGender);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 50;
    gbObject.setConstraints (comboGender, gbc);
    panelObject.add (comboGender);

    // Controls for Age

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 70;
    gbObject.setConstraints (labelAge, gbc);
    panelObject.add (labelAge);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 70;
    gbObject.setConstraints (textAge, gbc);
    panelObject.add (textAge);

    // Controls for Partner

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 90;
    gbObject.setConstraints (labelPartner, gbc);
    panelObject.add (labelPartner);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 90;
    gbObject.setConstraints (textPartner, gbc);
    panelObject.add (textPartner);

    // Controls for Country

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 110;
    gbObject.setConstraints (labelCountry, gbc);
    panelObject.add (labelCountry);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 110;
    gbObject.setConstraints (comboCountry, gbc);
    panelObject.add (comboCountry);

    // Controls for City

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 130;
    gbObject.setConstraints (labelCity, gbc);
    panelObject.add (labelCity);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 130;
    gbObject.setConstraints (textCity, gbc);
    panelObject.add (textCity);

    // Controls for Address

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 150;
    gbObject.setConstraints (labelAddress, gbc);
    panelObject.add (labelAddress);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 150;
    gbObject.setConstraints (textAddress, gbc);
    panelObject.add (textAddress);

    // Controls for Zip or Postal Code

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 1;
    gbc.gridy = 170;
    gbObject.setConstraints (labelZipPostal, gbc);
    panelObject.add (labelZipPostal);

    gbc.anchor = GridBagConstraints.NORTHWEST;
    gbc.gridx = 4;
    gbc.gridy = 170;
    gbObject.setConstraints (textZipPostal, gbc);
    panelObject.add(textZipPostal);
}

public static void main (String args[])
{

    newAccount customerObj;
    customerObj = new newAccount();
    bankFrame = new JFrame("Bank account");

    bankFrame.setSize(300, 300);
    bankFrame.setVisible(true);

    bankFrame.getContentPane().add(panelObject);

    customerObj.newAccount();
}
}

你的主问题似乎在这里:

public static void main (String args[])
{

newAccount customerObj;
customerObj = new newAccount();
bankFrame = new JFrame("Bank account");

bankFrame.setSize(300, 300);
bankFrame.setVisible(true);

bankFrame.getContentPane().add(panelObject); <--- Here

customerObj.newAccount();
}
}

让你的主骄傲

1 个答案:

答案 0 :(得分:0)

我的主,

您似乎无意中交换了两行代码。

而不是

bankFrame.getContentPane().add(panelObject);
customerObj.newAccount();

尝试按照与此相反的顺序排列它们:

customerObj.newAccount();
bankFrame.getContentPane().add(panelObject);

异常的原因是panelObject变量是静态的,并且在您调用customerObj.newAccount()之前不会分配 - 这意味着 在原始代码中 - 因为panelObject为null - 您一直在尝试不添加contentPane,这就是失败的原因。当我们第一次调用customerObj.newAccount时,我们正在初始化panelObject并且swing不会抱怨。

另请注意,您不应该与主线程中的swing组件进行交互,而只能从事件派发线程进行交互。为了做到这一点,只需用以下内容替换原始main方法:

    public static void main (String args[]) throws InvocationTargetException, InterruptedException {

    SwingUtilities.invokeAndWait(new Runnable() {
        public void run() {
            newAccount customerObj;
            customerObj = new newAccount();
            bankFrame = new JFrame("Bank account");

            bankFrame.setSize(300, 300);
            bankFrame.setVisible(true);

            customerObj.newAccount();
            bankFrame.getContentPane().add(panelObject);
        }
    });

}

invokeAndWait会将runnable传递给事件派发线程,并等到swing在返回之前执行该代码。

使用替换的main方法运行原始代码 - 在我的机器上提供:

screenshoot