Reputation: 274
My issue is, rather than the elements being on top of each other neatly, there are huge blocks of space unnecessarily in between each element of the gui that I use in the program.
I'm trying to have it like this:
label
textfield, button
textarea
But instead it's coming out like
label
(blotch of space)
textfield, button
(blotch of space)
textarea
when I run it. Any help is appreciated, I've really been trying to figure it out by myself. package guiprojj;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
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");
JButton enter = new JButton("Enter");
final JTextArea movieinfo = new JTextArea(5,20);
final JTextField movietext = new JTextField(16);
final JScrollPane scrolll = new JScrollPane(movieinfo);
final JLabel titlee = new JLabel("Enter movie name here:");
JPanel pangui = new JPanel();
JPanel pangui2 = new JPanel();
maingui.setLayout(new GridLayout(3, 0));
maingui.add(titlee);
pangui.add(movietext);
pangui.add(enter);
pangui2.add(scrolll);
//scrolll.add(movieinfo);
//pangui.add(movieinfo);
maingui.setResizable(false);
maingui.setVisible(true);
movieinfo.setLineWrap(true);
movieinfo.setWrapStyleWord(true);
movieinfo.setEditable(false);
maingui.add(pangui);
maingui.add(pangui2);
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);
}
});
}
}
Upvotes: 0
Views: 148
Reputation: 1876
if u need to put them separately or with variable space , just use GridBag Layout or use absolute layout i.e. no layout, with manual positioning
it would be easy using for loop for aligning in absolute layout (using setBounds(x,y,width,height)
Upvotes: 0
Reputation:
You need to use a layout manager that works the way you expect it to. There are several managers included with Java and I recommend you start here:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
This will give you a good visual introduction to the various layout managers and should give you the foundation you need to pick the right one.
Upvotes: 1
Reputation: 285405
This is how GridLayout works: all grid cells are the same size, probably the largest preferredSize of its components. Better to do what I suggested in my last answer: nesting BorderLayout using JPanels.
Upvotes: 3