1
我遇到了一個編譯器問題,當我運行時,它給了我這個:JPanel添加到JFrame的問題:java.lang.IllegalArgumentException:無法添加到佈局:約束必須是一個字符串(或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));
1+最後一天對這個答案用完了贊成票!在Blaine:你會想要閱讀佈局管理器教程。這裏都有解釋,比在牆上扔隨機碼並等待看到什麼更好。 – 2013-05-03 23:21:13