Failed to set JLabel location to JPanel

I set the location (0,0) for JLabel relative to JPanel. But it is close to the center and top. What mistake am I making?

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Main extends JFrame { private JPanel panel; private JLabel label1; public Main() { panel = new JPanel(); panel.setBackground(Color.YELLOW); ImageIcon icon1 = new ImageIcon("3.png"); label1 = new JLabel(icon1); label1.setLocation(0,0); panel.add(label1); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.getContentPane().add(panel); this.setSize(500,500); this.setVisible(true); } public static void main (String[] args) { new Main(); } } 
+6
source share
6 answers

Setting the layout manager to null did not help me. Try the following:

 // setLocation(0,0); //remove line. panel.setLayout(new FlowLayout(FlowLayout.LEFT)); // change from 'centered' 

Voila! :) I recommend you study the new RelativeLayout manager.

+8
source

Use layouts!

Screenshot of left aligned image icon

 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.net.URL; public class Main extends JFrame { private JPanel panel; private JLabel label1; public Main() throws Exception { // Do use layouts (with padding & borders where needed)! panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); panel.setBackground(Color.YELLOW); URL url = new URL("http://pscode.org/media/starzoom-thumb.gif"); ImageIcon icon1 = new ImageIcon(url); label1 = new JLabel(icon1); // Don't use null layouts, setLocation or setBounds! //label1.setLocation(0,0); panel.add(label1); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(panel); setSize(400,200); setLocationByPlatform(true); setVisible(true); } public static void main (String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { new Main(); } catch(Exception e) { e.printStackTrace(); } } }); } } 

Update

There seems to be a lot of confusion among the answers on this topic, so to clarify one question:

  • A JPanel has a FlowLayout by default.
  • It receives FlowLayout , comes from the constructor of 'no args', for example. new FlowLayout() 1 .
  • The no-args constructor for a FlowLayout creates ..

    (1) .. new FlowLayout with centered alignment and horizontal and vertical clearance of 5 units by default.

+8
source

You cannot do this because the default layout manager takes care of rendering items in the panel.

If you really want to set the item in position, you can choose panel = new JPanel(null);

But wait a bit! Do you really want to do this? I believe not. We (java programmers) usually use the service provided by layout managers to render elements on the screen. You have to spend an hour to learn and understand the concept, and then I promise you that you will use this technique and forget about manually rendering the elements on the screen as an insult to a very bad dream.

+3
source

You cannot set the absolute location of Component , and Container - LayoutManager ; you must call setLayout(null) on the JPanel before this works.

+2
source

By writing

panel = new JPanel ();

you force the panel to use FlowLayout, which adds inline elements. you can use

panel = new JPanel (null);

if you want to get better performance by disabling the FlowLayout dispatcher, but at the same time you have to take care of unpleasant things, such as the location of components, sizes, etc. I think it's better to use layout managers

+1
source
 setLayout(null); //set your frame layout to null for abs. positioning yourPanel = new JPanel(); // create your JPanel yourPanel.setLayout(null); // set the layout null for this JPanel ! text1 = new JLabel("SB"); // create some stuff text2 = new JLabel("BB"); yourPanel.add(text1); // add the stuff to the JPanel yourPanel.add(text2); yourPanel.getComponent(0).setBounds(0,0, 20, 20); // set your position of your elements inside your JPanel yourPanel.setBorder(BorderFactory.createLineBorder(Color.black)); // set a testing border to help you position the elements better yourPanel.setBounds(30, 200, 750, 220); // set the location of the JPanel 

// leave me an answer

+1
source

Source: https://habr.com/ru/post/909074/


All Articles