Java GUI window 没有任何输出
Java GUI window does not have any output
问候护戒使者,
以下任务是交给你的。
你主的程序没有按照我的意愿在 window 上输出结果。
框架弹出,但面板不工作,因此 none 的按钮和控件出现。
这是你的使命,
你是否应该选择接受它:
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();
}
}
让你的主骄傲
大人,
您似乎无意中交换了两行代码。
而不是
bankFrame.getContentPane().add(panelObject);
customerObj.newAccount();
尝试像这样将它们倒序排列:
customerObj.newAccount();
bankFrame.getContentPane().add(panelObject);
异常的原因是 panelObject
变量是静态的,直到您调用 customerObj.newAccount()
才分配 - 这意味着
在原始代码中——因为 panelObject 为 null——你一直试图不添加任何 contentPane,这就是失败的原因。当我们第一次调用 customerObj.newAccount 时,我们正在初始化 panelObject 并且 swing 不会报错。
另请注意,您不应与主线程中的 swing 组件进行交互,而只能从事件调度线程中进行交互。为此,只需将原来的主要方法替换为以下内容:
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 方法的原始代码 - 在我的机器上给出了这个:
问候护戒使者,
以下任务是交给你的。
你主的程序没有按照我的意愿在 window 上输出结果。
框架弹出,但面板不工作,因此 none 的按钮和控件出现。
这是你的使命, 你是否应该选择接受它:
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();
}
}
让你的主骄傲
大人,
您似乎无意中交换了两行代码。
而不是
bankFrame.getContentPane().add(panelObject);
customerObj.newAccount();
尝试像这样将它们倒序排列:
customerObj.newAccount();
bankFrame.getContentPane().add(panelObject);
异常的原因是 panelObject
变量是静态的,直到您调用 customerObj.newAccount()
才分配 - 这意味着
在原始代码中——因为 panelObject 为 null——你一直试图不添加任何 contentPane,这就是失败的原因。当我们第一次调用 customerObj.newAccount 时,我们正在初始化 panelObject 并且 swing 不会报错。
另请注意,您不应与主线程中的 swing 组件进行交互,而只能从事件调度线程中进行交互。为此,只需将原来的主要方法替换为以下内容:
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 方法的原始代码 - 在我的机器上给出了这个: