Reputation: 135
I'm making a simple weather app using IntelliJ and built in form designer. I have made and designed a form and edited the bound class file accordingly. When I actually run the code, the form I designed does now show up at all, in fact just the last line (maxtemp) is the only value that shows up on an empty white screen.
P.S I am using Gradle to build, I have also set the GUI designer to use Java source code instead of byte code (since Gradle does not support the byte code)
import org.json.JSONException;
import org.json.JSONObject;
import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame {
// Get the API
private API api = new API();
// Form elements
public JPanel mainPanel;
public JLabel text;
public JLabel category;
public JLabel mintemp;
public JLabel maxtemp;
public JLabel link;
public Frame() throws Exception, JSONException {
initFrame();
}
public void initFrame() throws Exception, JSONException {
// Get API response
JSONObject wjson = api.connection();
// Filter response and get data
String[] data = api.respFilter(wjson);
// Swing components
mainPanel = new JPanel(new BorderLayout());
text = new JLabel();
category = new JLabel();
mintemp = new JLabel();
maxtemp = new JLabel();
link = new JLabel();
// Add to the frame
add(mainPanel);
add(text);
add(category);
add(mintemp);
add(maxtemp);
text.setText(data[0]);
category.setText(data[1]);
mintemp.setText(data[3]);
maxtemp.setText(data[4]);
}
public static void main(String[] args) throws Exception, JSONException {
JFrame app = new Frame();
app.setTitle("Java-WeatherApp");;
app.setSize(900, 600);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setLocationRelativeTo(null);
app.setVisible(true);
}
}
I'm pretty new to Java and very new to the form builder on IntelliJ. Unfortunately I could not find any good tutorials on the form builder. Any help is appreciated!
Upvotes: 1
Views: 3184
Reputation: 307
A JFrame has BorderLayout as a default layout.
In your initFrame()
method when you call,
add(mainPanel);
add(text);
add(category);
add(mintemp);
add(maxtemp);
You are adding all the swing components to the JFrame
. Since, you are not specifying the position of a component and since the JFrame
has BorderLayout
, only one component is added to the JFrame
that is the last component you add to it, which the maxtemp Jlabel.
You should specify the position of a component when adding to the JFrame having BorderLayout as following,
add(minTemp, BorderLayout.NORTH);
I suggest you to learn more about BorderLayout and LayoutManagers.
OR,
You can use code similar to this,
// Swing components
mainPanel = new JPanel(new GridLayout(4, 1));
text = new JLabel();
category = new JLabel();
mintemp = new JLabel();
maxtemp = new JLabel();
link = new JLabel();
// Add to the mainPanel
mainPanel.add(text);
mainPanel.add(category);
mainPanel.add(mintemp);
mainPanel.add(maxtemp);
//Add mainPanel to Frame
add(mainPanel);
The above written code does the following,
mainPanel = new JPanel(new GridLayout(4, 1));
This line of code set GridLayout as the layout of mainPanel. GridLayout is a type of layout manager which divides the container(mainPanel) in to equal number of grids by dividing it into rows and columns.
In the given code I have divided the mainPanel into 4 rows and 1 columns, so total 4 grids.
mainPanel.add(text); mainPanel.add(category); mainPanel.add(mintemp); mainPanel.add(maxtemp);
This bunch of code add the swing components to the mainPanel. When the first line of code is executed, the text label is added to mainPanel in first grid. Similarly, second line add category to the second grid and similar for the rest.
add(mainPanel);
This line of code adds mainPanel to the Frame.
If you executed the above code, the output will be similar to,
Note : I have changed the text of the labels as shown in above image.
Upvotes: 2