在 Swing 中出现错误 java - IllegalArgumentException
Get error in Swing java - IllegalArgumentException
尝试在框架上安装一些秋千组件。
此代码在几天前有效。现在不行了,什么都没有。
也许有人可以告诉我哪里出了问题?
public static void main(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.setSize(500, 400); //Size of frame
mainFrame.setTitle("Cinema City"); //Set title
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel mainLabel = new JLabel("Welcome to Cinema City catalog!");
JLabel actorLabel = new JLabel("Actors: ");
JLabel laLabel = new JLabel("Last added: ");
JLabel searchLabel = new JLabel("How to search ?");
GridBagConstraints gbc = new GridBagConstraints();
mainFrame.add(mainLabel, new GridBagConstraints(4, 0, 1, 3, 1, 1,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
new Insets(20, 160, 0, 0), 0, 0));
mainFrame.add(actorLabel, new GridBagConstraints(0, 0, 1, 1, 1, 1,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(100, 0, 0, 0), 0, 0));
mainFrame.setVisible(true);
这是错误:
Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
at java.awt.BorderLayout.addLayoutComponent(Unknown Source)
at javax.swing.JRootPane.addLayoutComponent(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at GUI.main(GUI.java:40)
您实际上并未将布局设置为 GridBagLayout
,因此它仍然是默认设置(即 FlowLayout
)。
当然,只有GridBagLayout
才能真正处理GridBagConstraints
。
这可以通过将声明更改为 JFrame mainFrame = new JFrame(new GridBagLayout());
来解决
未提及特定 JFrame 的布局 - mainframe
在 JFrame 声明之后添加这一行
mainFrame.setLayout(new GridBagLayout());
应该可以正常工作。
您没有设置框架布局。
创建框架对象后编写此代码。
new GridBagLayout();
mainFrame.setLayout(gbl);
它的工作
尝试在框架上安装一些秋千组件。 此代码在几天前有效。现在不行了,什么都没有。 也许有人可以告诉我哪里出了问题?
public static void main(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.setSize(500, 400); //Size of frame
mainFrame.setTitle("Cinema City"); //Set title
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel mainLabel = new JLabel("Welcome to Cinema City catalog!");
JLabel actorLabel = new JLabel("Actors: ");
JLabel laLabel = new JLabel("Last added: ");
JLabel searchLabel = new JLabel("How to search ?");
GridBagConstraints gbc = new GridBagConstraints();
mainFrame.add(mainLabel, new GridBagConstraints(4, 0, 1, 3, 1, 1,
GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
new Insets(20, 160, 0, 0), 0, 0));
mainFrame.add(actorLabel, new GridBagConstraints(0, 0, 1, 1, 1, 1,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(100, 0, 0, 0), 0, 0));
mainFrame.setVisible(true);
这是错误:
Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
at java.awt.BorderLayout.addLayoutComponent(Unknown Source)
at javax.swing.JRootPane.addLayoutComponent(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at GUI.main(GUI.java:40)
您实际上并未将布局设置为 GridBagLayout
,因此它仍然是默认设置(即 FlowLayout
)。
当然,只有GridBagLayout
才能真正处理GridBagConstraints
。
这可以通过将声明更改为 JFrame mainFrame = new JFrame(new GridBagLayout());
未提及特定 JFrame 的布局 - mainframe
在 JFrame 声明之后添加这一行
mainFrame.setLayout(new GridBagLayout());
应该可以正常工作。
您没有设置框架布局。 创建框架对象后编写此代码。
new GridBagLayout();
mainFrame.setLayout(gbl);
它的工作