向JFrame添加JPanel的问题:java.lang.IllegalArgumentException:无法添加到布局:约束必须是字符串(或null)

时间:2013-05-03 23:13:53

标签: java swing jframe jpanel

当我点击运行时我的编译器出现问题它给了我:

Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
    at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:426)
    at java.awt.Container.addImpl(Container.java:1120)
    at java.awt.Container.add(Container.java:998)
    at javax.swing.JFrame.addImpl(JFrame.java:562)
    at java.awt.Container.add(Container.java:966)
    at DealerWindow.<init>(DealerWindow.java:36)
    at DealerWindow.main(DealerWindow.java:98)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

这似乎在告诉我,在第35行它无法添加到布局,因为约束必须是字符串或null

这是我的代码(是的每一点都是):

import java.awt.*;
import javax.swing.*;

public class DealerWindow extends JFrame{
    private final int WINDOW_WIDTH  = 450;
    private final int WINDOW_HEIGHT = 338;

    public DealerWindow(){

        //Sets the title
        setTitle("Welcome to X");

        //Set the window size
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        // Specify an action for the close button
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Set BorderLayout
        setLayout(new BorderLayout());

        // creates a Panel that will contain a Label that will contain the users image
        JPanel innerPanelCenter = new JPanel();
        Icon icon = new ImageIcon();
        JLabel Label = new JLabel(icon);

        //Sets Layout and adds the label and innerPanel
        innerPanelCenter.setLayout(new FlowLayout(FlowLayout.CENTER));
        innerPanelCenter.add(Label);
        add(innerPanelCenter,new FlowLayout(FlowLayout.CENTER));

        // creates innerPanelRight to go on the West side of the form
        JPanel innerPanelRight = new JPanel();
        innerPanelRight.setLayout(new FlowLayout());

        //Buttons for innerPanelRight
        JButton openButton = new JButton("Open");

        //Labels for innerPanelRight
        JLabel makeLabel = new JLabel("Make: ");
        JLabel modelLabel = new JLabel("Model: ");
        JLabel yearLabel = new JLabel("Year: ");
        JLabel commentsLabel = new JLabel("Comments: ");

        //TextFields for innerPanelRight
        JTextField makeTextField = new JTextField();
        JTextField modelTextField = new JTextField();

        //ComboBoxes for innerPanelRight and fills the ComboBox
        JComboBox yearComboBox = new JComboBox();
        for (int i = 1900; i >2013; i++){
           yearComboBox.addItem(i);
        }

        //TextAreas for innerPanelRight
        JTextArea commentsTextArea = new JTextArea();
        commentsTextArea.setLineWrap(true);

        //Creates JPanels for each user field and sets Layouts
        JPanel makePanel = new JPanel();
        makePanel.setLayout(new BorderLayout());
        JPanel modelPanel = new JPanel();
        modelPanel.setLayout(new BorderLayout());
        JPanel yearPanel = new JPanel();
        yearPanel.setLayout(new BorderLayout());
        JPanel commentsPanel = new JPanel();
        commentsPanel.setLayout(new BorderLayout());

        //adds components to the respective Panels
        yearPanel.add(yearLabel,BorderLayout.EAST);
        yearPanel.add(yearComboBox,BorderLayout.CENTER);

        makePanel.add(makeLabel,BorderLayout.EAST);
        makePanel.add(makeTextField,BorderLayout.CENTER);

        modelPanel.add(modelLabel,BorderLayout.EAST);
        modelPanel.add(modelTextField,BorderLayout.CENTER);

        commentsPanel.add(commentsLabel,BorderLayout.EAST);
        commentsPanel.add(commentsTextArea,BorderLayout.CENTER);

        //add the button and 4 component panels to the innerPanelRight
        innerPanelRight.add(openButton);
        innerPanelRight.add(yearPanel);
        innerPanelRight.add(makePanel);
        innerPanelRight.add(modelPanel);

        //add innerPanelRight to the west side of the frame
        add(innerPanelRight,BorderLayout.WEST);
    }
    public static void main(String [] args){
        new DealerWindow();
    }
}

错误说明问题出在第35行,即:

add(innerPanelCenter,new FlowLayout(FlowLayout.CENTER));

1 个答案:

答案 0 :(得分:3)

使用有效的布局约束。取代

add(innerPanelCenter,new FlowLayout(FlowLayout.CENTER));

add(innerPanelCenter, BorderLayout.CENTER);