Creating Gridbag Layout

2019-09-10 02:33发布

问题:

I'm trying to make a gridbaglayout, to simply have the label atop the textbox atop the textview in a vertical window, but I'm getting the exception

Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint
    at java.awt.GridBagLayout.addLayoutComponent(GridBagLayout.java:685)
    at java.awt.Container.addImpl(Container.java:1072)
    at java.awt.Container.add(Container.java:957)
    at javax.swing.JFrame.addImpl(JFrame.java:540)
    at java.awt.Container.add(Container.java:925)
    at guiprojj.gui.main(gui.java:31)

Can anyone assist me in fixing this exception, maybe I'm misunderstanding how gridbaglayout works, if so, if you could point me in the right direction that would be awesome. Appreciate the assistance.

package guiprojj;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.*;

import com.eclipsesource.json.JsonObject;
import com.google.gson.JsonParser;
import com.json.parsers.JSONParser;
import com.json.parsers.JsonParserFactory;


public class gui {
    public static void main(String[] args)
    {
        JFrame maingui = new JFrame("Gui");
        maingui.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        JButton enter = new JButton("Enter");
        final JTextArea movieinfo = new JTextArea(5,20);
        final JTextField movietext = new JTextField(16);
        c.gridx = 1;
        c.gridy = 0;
        maingui.add(movietext, maingui);
        final JScrollPane scrolll = new JScrollPane(movieinfo);
        final JLabel titlee = new JLabel("Enter movie name here:");
        maingui.setResizable(false);
        maingui.setVisible(true);
        movieinfo.setLineWrap(true);
        movieinfo.setWrapStyleWord(true);
        movieinfo.setEditable(false);

        scrolll.getPreferredSize();
        //pangui.setPreferredSize(new Dimension(300, 150));
        //pangui.add(scrolll, BorderLayout.CENTER);
        //movieinfo.add(scrolll);
        maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        maingui.pack();
        enter.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)  
            {
                System.out.println(Test.getMovieInfo(movietext.getText()));
                 JsonParserFactory factory=JsonParserFactory.getInstance();
                 JSONParser parser=factory.newJsonParser();
                 Map jsonData=parser.parseJson(Test.getMovieInfo(movietext.getText()));
                 String Title = (String)jsonData.get("Title");
                 String Year = (String)jsonData.get("Year");
                 String Plot = (String)jsonData.get("Plot");
                 movieinfo.setText("Title: "+Title+"\nYear: "+ Year +"\nPlot: "+Plot);
            }
            });

        }
}

回答1:

I think maingui.add(movietext, maingui); should be maingui.add(movietext, c);

That last parameter should be the layout-specific constraints object. You're passing it the parent object.



回答2:

maingui.add(movietext, maingui);

"maingui" is NOT a GridBagConstraint. "c" IS A GridBagConstraint.

I don't know if using the proper variable will correct the layout, but it should get rid of the Exception. Read the Swing tutorial on How to Use GridBagLayout for examples. Not only that you will find code to better structure your program. All GUI code should execute on the EDT. Read the tutorial section on Concurrency for more information on that.



回答3:

This is wrong:

maingui.add(movietext, maingui);

When you add to a container, you add the Component + its constraints. In your case:

maingui.add(movietext, c);