问题
I can not find the error to save my life. The error is "constraint must be a string (or null)" I dont know why it is giving me this error, I have to be missing something simple. I tried adding
for example: dataPane = new JPanel(new GridBagLayout()); to all my panels and nothing.
I am trying to add a panel (2) to the extended panel. here is my code:
public class SearchFlight extends JPanel {
//giving names to the components\\\
private JRadioButton oneWay;
private JRadioButton roundTrip;
private ButtonGroup buttonGroup;
private JLabel fromDestdLabel;
private JComboBox fromDestCb;
private JLabel toDestLbl;
private JComboBox toDestCb;
private JLabel departLbl;
private JComboBox departMonth;
private JComboBox departDay;
private JTextField departYear;
private JLabel arriveLbl;
private JComboBox arriveMonth;
private JComboBox arriveDay;
private JTextField arriveYear;
private JLabel adultLbl;
private JComboBox adultCb;
private JLabel childLbl;
private JComboBox childCb;
private JLabel infantLbl;
private JComboBox infantCb;
private JButton searchBtn;
private JButton canxBtn;
private JPanel buttonPane;
private JPanel dataPane;
public SearchFlight(){
initcomp();
}
public void initcomp(){
//initilizing all the componets\\
oneWay = new JRadioButton("One Way");
roundTrip = new JRadioButton("Round Trip");
buttonGroup = new ButtonGroup();
fromDestdLabel = new JLabel("From");
fromDestCb = new JComboBox();
toDestLbl = new JLabel("To");
toDestCb = new JComboBox();
departLbl = new JLabel("Depart");
departMonth = new JComboBox();
departDay = new JComboBox();
departYear = new JTextField();
arriveLbl = new JLabel("Arrive");
arriveMonth = new JComboBox();
arriveDay = new JComboBox();
arriveYear = new JTextField();
adultLbl = new JLabel("Adult");
adultCb = new JComboBox();
childLbl = new JLabel("Child");
childCb = new JComboBox();
infantLbl = new JLabel("infant");
infantCb = new JComboBox();
searchBtn = new JButton("Search");
canxBtn = new JButton("Cancel");
buttonPane = new JPanel();
dataPane = new JPanel(new GridBagLayout());
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
BorderLayout borderLayout = new BorderLayout();
c.fill = GridBagConstraints.HORIZONTAL;
dataPane.setLayout(borderLayout);
c.gridx = 1;
c.gridy = 0;
dataPane.add(oneWay, c);
c.gridx = 2;
c.gridy = 0;
dataPane.add(roundTrip, c);
c.gridx = 0;
c.gridy = 1;
dataPane.add(fromDestdLabel ,c);
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 2;
dataPane.add(fromDestCb, c);
c.gridx = 0;
c.gridy = 2;
dataPane.add(toDestLbl,c);
c.gridx = 1;
c.gridy = 2;
dataPane.add(toDestCb, c);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 3;
dataPane.add(departLbl ,c);
c.gridx = 1;
c.gridy = 3;
dataPane.add(departMonth,c);
c.gridx = 2;
c.gridy = 3;
dataPane.add(departDay, c);
c.gridx = 3;
c.gridy = 3;
dataPane.add(departYear, c);
c.gridx = 0;
c.gridy = 4;
dataPane.add(arriveLbl, c);
c.gridx = 1;
c.gridy = 4;
dataPane.add(arriveMonth, c);
c.gridx = 2;
c.gridy = 4;
dataPane.add(arriveDay,c);
c.gridx = 3;
c.gridy = 4;
dataPane.add(arriveYear,c);
c.gridx = 0;
c.gridy = 5;
dataPane.add(adultLbl,c);
c.gridx = 1;
c.gridy = 5;
dataPane.add(adultCb,c);
c.gridx = 0;
c.gridy = 6;
dataPane.add(childLbl,c);
c.gridx = 1;
c.gridy = 6;
dataPane.add(childCb,c);
c.gridx = 0;
c.gridy = 7;
dataPane.add(infantLbl,c);
c.gridx = 1;
c.gridy = 7;
dataPane.add(infantCb,c);
buttonPane.add(searchBtn);
buttonPane.add(canxBtn);
add(buttonPane, BorderLayout.SOUTH);
add(dataPane);
}
}
回答1:
You are giving dataPane a BorderLayout, but then trying to use GridBagConstraints when adding components to it --- not allowed, and even if allowed, just doesn't make sense.
Instead you have one of two options:
- Keep the container's layout as BorderLayout but use BorderLayout constants such as BorderLayout.EAST when adding components to this container, or
- Change the dataPane's layout manager to GridBagLayout, and then sure, go ahead and continue using GridBagConstraints when adding components.
Edit
You state in comment:
so I use dataPane = new JPanel(new GridBagLayout()); then add it by add(dataPane);
Yes, it is fine to use a GridBagLayout, but I'm not sure what you mean by your second point, the one re add(dataPane)
as that appears unrelated to your original problem.
来源:https://stackoverflow.com/questions/22446078/constraint-must-be-a-string-or-null