我的问题是我的第二个 gridbagconstraints 变量的最后两列间隔太多。我使用了一个 gridbagconstraint 来管理第一列,第二列处理最后 3 列。
遗憾的是,由于是新用户,我无法提供屏幕截图:(
import java.awt.*;
import javax.swing.*;
public class Main_Window_Frame extends JFrame{
//The tabbed pane object is constructed
JTabbedPane TabGroup = new JTabbedPane();
//Jpanels to be added to TabGroup
JPanel panelAddEmployee = new JPanel();//This will create the first tab "Add Employee"
JPanel panelSearchEmployee = new JPanel();//This will create the second tab "Search for an Employee
JPanel panelRemoveEmployee = new JPanel();//This will create the third tab "Remove an Employee"
JPanel panelListAllEmployees = new JPanel();//This will create the fourth tab "List all Employees"
JPanel panelSysOptions = new JPanel();//This will create the fifth tab "System options"
//Construct GridBagConstraints for the objects within panelAddEmployee JPanel
GridBagConstraints c = new GridBagConstraints();
GridBagConstraints gbc = new GridBagConstraints();
//Add employee panel Label declarations
JLabel lblAddEmployee;
JLabel lblEmpID;
JLabel lblEmpIDout;
JLabel lblForename;
JLabel lblSurname;
JLabel lblGender;
JLabel lblDateBirth;
JLabel lblAddress;
JLabel lblPhoneNo;
JLabel lblEmail;
JLabel lblJobTitle;
JLabel lblSalary;
JLabel lblNatNo;
JLabel lblStartDate;
//Add employee panel TextField declarations
JTextField txtForename;
JTextField txtSurname;
JTextField txtAddress;
JTextField txtPhoneNo;
JTextField txtEmail;
JTextField txtJobTitle;
JTextField txtSalary;
JTextField txtNatNo;
//Add employee panel RadioButton declarations
JRadioButton radMale;
JRadioButton radFemale;
//Add employee panel ComboBox declarations
JComboBox cbDob1;
JComboBox cbDob2;
JComboBox cbDob3;
JComboBox cbDateStart1;
JComboBox cbDateStart2;
JComboBox cbDateStart3;
//Add employee panel label array
JLabel[] addEmployeeLabels = new JLabel[13];
//Add employee panel textField array
JTextField[] addEmployeeTextFields = new JTextField[8];
protected String[] DDBirthDate = {"01","02","03","04","05","06","07",
"08","09","10","11","12",
"13","14","15","16","17","18","19",
"20","21","22","23","24","25","26",
"27","28","29","30","31"};
protected String[] MMBirthDate = {"01","02","03","04","05","06","07",
"08","09","10","11","12"};
protected String[] YYYYBirthDate = {"1996","1995","1994","1993","1992",
"1991","1990","1989","1988","1987",
"1986","1985","1984","1983","1982",
"1981","1980 "," 1979 ","1978","1977",
"1976","1975","1974","1973","1972","1971",
"1970","1969","1968","1967 ","1966",
"1965","1964","1963","1962","1961","1960",
"1959","1958","1957","1956","1955",
"1954","1953","1952","1951","1950",
"1949","1948","1947","1946","1945",
"1944","1943","1942","1941"};
protected String[] YYYYStartDate = {"2012","2011","2010"};
public Main_Window_Frame() {
super("Employee Record System");
setSize(750,500); //set the size of the form
//This creates the template for the multiple tabs using JTabbedPane
getContentPane().add(TabGroup);
//Add the GridBagLayout manager to the panelAddEmployee JPanel
panelAddEmployee.setLayout(new GridBagLayout());
//Add the GridBagLayout manager to the panelAddEmployee JPanel
panelSearchEmployee.setLayout(new GridBagLayout());
generateEmployeeAttributeLabels();
addEmployeePanelObjects();
addListAllEmployeesPanelObjects();
addSystemOptionsPanelObjects();
//This adds the first, second, third and final tab to the tabbedPanel Object
TabGroup.addTab("Add Employee", panelAddEmployee);
TabGroup.addTab("Search for an Employee", panelSearchEmployee);
TabGroup.addTab("Remove an Employee", panelRemoveEmployee);
TabGroup.addTab("List all Employees", panelListAllEmployees);
TabGroup.addTab("System Options", panelSysOptions);
}
public void generateEmployeeAttributeLabels(){
//Labels are assigned to a label array
addEmployeeLabels[0] = lblAddEmployee = new JLabel(); lblAddEmployee.setText("Add Employee"); //Set the text to "Add Employee"
addEmployeeLabels[1] = lblEmpID = new JLabel(); lblEmpID.setText("Employee ID:");
addEmployeeLabels[2] = lblForename = new JLabel(); lblForename.setText("Forename:");
addEmployeeLabels[3] = lblSurname = new JLabel(); lblSurname.setText("Surname:");
addEmployeeLabels[4] = lblGender = new JLabel(); lblGender.setText("Gender:");
addEmployeeLabels[5] = lblDateBirth = new JLabel(); lblDateBirth.setText("Date of Birth:");
addEmployeeLabels[6] = lblAddress = new JLabel(); lblAddress.setText("Address:");
addEmployeeLabels[7] = lblPhoneNo = new JLabel(); lblPhoneNo.setText("Phone Number:");
addEmployeeLabels[8] = lblEmail = new JLabel(); lblEmail.setText("Email Address:");
addEmployeeLabels[9] = lblJobTitle = new JLabel(); lblJobTitle.setText("Job Title:");
addEmployeeLabels[10] = lblSalary = new JLabel(); lblSalary.setText("Salary:");
addEmployeeLabels[11] = lblNatNo = new JLabel(); lblNatNo.setText("National Insurance no:");
addEmployeeLabels[12] = lblStartDate = new JLabel(); lblStartDate.setText("Start Date:");
//GridBagConstraints for the title label "Add Employee"
c.fill = GridBagConstraints.HORIZONTAL;
int j = 0;
//This for loop adds all of the labels to the add employee panel.
//More efficient than multiple lines of code for each label.
for(int i = 0; i < 13; i++){
//If the element is the "Add Employee" label then we give it the appropriate sizes
if(i == 0){
c.insets = new Insets(0,0,0,0); //Insets are 0, assigned to upper-most top left
c.weightx = 0.5; //horizontal weight is 0.5
c.gridx = 0; //Place in column 0
c.gridy = 0; //Place in row 0
addEmployeeLabels[0].setFont(new Font("Arial",Font.BOLD,24));
}else{
//If the element is the "Start Date" Label then
if (i == 12){
c.weighty = 1.0; //vertical weight is 0.5
c.anchor = GridBagConstraints.PAGE_START; //Ensures last label is at the edge of its display area
c.gridx = 0; //Place in column 0
c.gridy = 12; //Place in column 12
addEmployeeLabels[12].setFont(new Font("Arial",Font.PLAIN,12));
//For every other label, a default size and relational location is assigned.
}else{
c.insets = new Insets(15,5,0,0);
c.weightx = 0.5;
c.gridx = 0; //Place in column 0
c.gridy = i; //Place in column i (relational positioning based on element index value)
addEmployeeLabels[i].setFont(new Font("Arial",Font.PLAIN,12));
}
}
//Adds each label in the array to the "panelAddEmployee" Panel
panelAddEmployee.add(addEmployeeLabels[i], c);
//Adds each label in the array to the "panelAddEmployee" Panel
//panelSearchEmployee.add(addEmployeeLabels[i], c);
}
}
public void addEmployeePanelObjects(){
gbc.gridwidth = 1;
//Construct lblEmpIDout label
lblEmpIDout = new JLabel();
lblEmpIDout.setText("0");
//Construct radMale radio button
radMale = new JRadioButton();
radMale.setText("Male?");
//Construct radFemale radio button
radFemale = new JRadioButton();
radFemale.setText("Female?");
cbDob1 = new JComboBox();
cbDateStart1 = new JComboBox();
for(int i = 0; i < DDBirthDate.length; i++){
cbDob1.addItem(DDBirthDate[i]);
cbDateStart1.addItem(DDBirthDate[i]);
}
cbDob2 = new JComboBox();
cbDateStart2 = new JComboBox();
for(int i = 0; i < MMBirthDate.length; i++){
cbDob2.addItem(MMBirthDate[i]);
cbDateStart2.addItem(MMBirthDate[i]);
}
cbDob3 = new JComboBox();
cbDateStart3 = new JComboBox();
for(int i = 0; i < YYYYBirthDate.length; i++){
cbDob3.addItem(YYYYBirthDate[i]);
cbDateStart3.addItem(YYYYBirthDate[i]);
}
//TextFields are assigned to a TextField array
addEmployeeTextFields[0] = txtForename = new JTextField();
addEmployeeTextFields[1] = txtSurname = new JTextField();
addEmployeeTextFields[2] = txtAddress = new JTextField();
addEmployeeTextFields[3] = txtPhoneNo = new JTextField();
addEmployeeTextFields[4] = txtEmail = new JTextField();
addEmployeeTextFields[5] = txtJobTitle = new JTextField();
addEmployeeTextFields[6] = txtSalary = new JTextField();
addEmployeeTextFields[7] = txtNatNo = new JTextField();
int j = 0;
//This for loop adds all of the labels to the add employee panel.
//More efficient than multiple lines of code for each label.
for(int i = 0; i < 13; i++){
gbc.insets = new Insets(15,5,0,0);
gbc.gridy = i;
gbc.gridx = 0;
gbc.weightx = 1;
gbc.weighty = 1;
if (i == 0 || i == 1 ||i == 4 || i == 5 || i == 12 ){
if(i==1){
panelAddEmployee.add(lblEmpIDout, gbc);
}
if(i==4){
panelAddEmployee.add(radMale,gbc);
gbc.gridx = 1;
panelAddEmployee.add(radFemale,gbc);
gbc.gridx = 0;
}
if(i==5){
panelAddEmployee.add(cbDob1,gbc);
gbc.gridx = 1;
panelAddEmployee.add(cbDob2,gbc);
gbc.gridx = 2;
panelAddEmployee.add(cbDob3,gbc);
gbc.gridx = 0;
}
if(i==12){
panelAddEmployee.add(cbDateStart1,gbc);
gbc.gridx = 1;
panelAddEmployee.add(cbDateStart2,gbc);
gbc.gridx = 2;
panelAddEmployee.add(cbDateStart3,gbc);
gbc.gridx = 0;
}
//Do not add a text box to title, ID
//gender, date of birth or start date
}else{
//Adds each textfield in the array to the "panelAddEmployee" Panel
panelAddEmployee.add(addEmployeeTextFields[j], gbc);
j++;
}
}
}
public void addSearchEmployeePanelObjects(){
}
public void addListAllEmployeesPanelObjects(){
}
public void addSystemOptionsPanelObjects(){
}
}