约束必须是一个字符串(或null)

时间:2014-03-17 02:45:07

标签: java swing layout-manager

我找不到拯救生命的错误。错误是"约束必须是字符串(或null)"我不知道为什么它会给我这个错误,我不得不错过一些简单的东西。 我尝试添加

例如:dataPane = new JPanel(new GridBagLayout());我的所有面板都没有。

我正在尝试将面板(2)添加到扩展面板。 这是我的代码:

    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 个答案:

答案 0 :(得分:4)

你给dataPane一个BorderLayout,但是在向它添加组件时尝试使用GridBagConstraints--不允许,即使允许,也没有意义。

相反,您有两种选择之一:

  • 将容器的布局保持为BorderLayout,但在向此容器添加组件时使用BorderLayout常量(如BorderLayout.EAST)或
  • 将dataPane的布局管理器更改为GridBagLayout,然后确保在添加组件时继续使用GridBagConstraints。

修改
您在评论中说明:

  

所以我使用dataPane = new JPanel(new GridBagLayout());然后通过add(dataPane)添加它;

是的,可以使用GridBagLayout,但我不确定你的第二点是什么意思,而add(dataPane)似乎与你原来的问题无关。