约束必须是一个字符串(或null) [英] constraint must be a string (or null)

查看:113
本文介绍了约束必须是一个字符串(或null)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

for example: dataPane = new JPanel(new GridBagLayout()); to all my panels and nothing.

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

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);
 }               

}


推荐答案

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

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:


  • 将容器的布局保持为BorderLayout但在向此容器添加组件时使用BorderLayout常量(例如BorderLayout.EAST),或

  • 将dataPane的布局管理器更改为GridBagLayout,然后确保在添加组件时继续使用GridBagConstraints 。

编辑

你在评论中说:

Edit
You state in comment:


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

so I use dataPane = new JPanel(new GridBagLayout()); then add it by add(dataPane);

是的,可以使用GridBagLayout,但是我不确定你的第二点是什么意思添加(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.

这篇关于约束必须是一个字符串(或null)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆