gpt4 book ai didi

java - 约束必须是字符串(或 null)

转载 作者:行者123 更新时间:2023-12-01 18:34:53 25 4
gpt4 key购买 nike

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

例如: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);
}

}

最佳答案

您为 dataPane 提供了 BorderLayout,但随后在向其添加组件时尝试使用 GridBagConstraints --- 不允许,即使允许,也没有意义。

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

  • 将容器的布局保留为 BorderLayout,但在向该容器添加组件时使用 BorderLayout 常量,例如 BorderLayout.EAST,或者
  • 将 dataPane 的布局管理器更改为 GridBagLayout,然后在添加组件时继续使用 GridBagConstraints。
<小时/>

编辑
您在评论中指出:

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

是的,使用 GridBagLayout 很好,但我不确定你的第二点是什么意思,即 re add(dataPane) ,因为这似乎与你原来的问题无关。

关于java - 约束必须是字符串(或 null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22446078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com