簡體   English   中英

向JFrame添加JPanel的問題:java.lang.IllegalArgumentException:無法添加到布局:約束必須是字符串(或null)

[英]Issue with adding a JPanel to JFrame: java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)

當我點擊運行時我的編譯器出現問題它給了我:

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

使用有效的布局約束。 更換

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

add(innerPanelCenter, BorderLayout.CENTER);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM