First of all, let me commend you on actually looking at that tutorial I linked you yesterday and putting effort into learning :)
Here's a couple things I see.
Here, you're trying to use a BorderLayout for a JLabel. Generally you want to use the LayoutManagers with containers, (JPanels, JFrames, JDialogs, JApplets). Get rid of the code below.
JLabel img = new JLabel(ic, JLabel.CENTER);
img.setLayout( new BorderLayout() );
Also here, With all your jp.add()
's.
jp.add(img, BorderLayout.CENTER);
jp.add(jl);
jp.add(tf);
jp.add(jl2);
....
I'm assuming you want to use a BorderLayout, but there was no BorderLayout specified for the jp
Panel. By default, if you don't specify a LayoutManager, JPanel will give you a FlowLayout
JPanel jp = new JPanel();
Keep in mind, when you assign a BorderLayout
to a container, you want to make sure every component you add to it, has a Layout position specified. No position should be used more than once. Also, a BorderLayout.CENTER
generally should always be used. (e.g. if you only have two components in the panel, use BorderLayout.SOUTH
, BorderLayout.CENTER
).
Also, you should learn to nest Layout with multiple Panels to get your desired result. Here's a simple example. Say you have two buttons and a Image Label
JLabel label = new JLabel(new ImageIcon(filePath));
JButton jbt1 = new JButton("jbt1");
JButton jbt2 = new JButton("jbt2");
I want the layout to look like this
+--------------------------------+
| |
| |
| Image Label |
| |
+----------------+---------------+
| jbt1 | jbt2 |
+----------------+---------------+
First thing I want to do is add the buttons to a separate JPanel. Then wrap the label in a JPanel (not necessary, but I like using panels);
JPanel buttonsPanel = new JPanel(); <-- FlowLayout (none specified)
buttonsPanel.add(jbt1);
buttonsPanel.add(jbt2);
JPanel labelPanel = new JPanle(); <-- FlowLayout
labelPabel.add(label);
Then I'm going to wrap the two panels with another panel with a BorderLayout
JPanel panel1 = new JPanel(new BorderLayout());
panel1.add(buttonPanel, BorderLayout.SOUTH);
panel1.add(labelPanel, BorderLayout.CENTER);
So my final panel would look like this
panel1
+--------------------------------+
| |
| | BorderLayout.CENTER
| Image Label |
| |
+----------------+---------------+
| jbt1 | jbt2 | BorderLayout.SOUTH
+----------------+---------------+
Let's say you came up with another panel you wanted to add. You want to add this new panel to the left of the panel we just created. We only now need to use the panel1
panel we created and wrap that panel and new panel in another panel
JPanel newPanel = new JPanel();
JPanel panel2 new JPanel(new BorderLayout());
panel2.add(newPanel, BorderLayout.WEST);
panel2.add(panel1, BorderLayout.CENTER);
panel2
+---------------+--------------------------------+
| | |
| | |
| | Image Label |
| newPanel | |
| +----------------+---------------+
| | jbt1 | jbt2 |
+---------------+----------------+---------------+
See how the layout and panel have been. This is great way to handle laying out components