Placing a JLabel with a specific x, y coordinate on a JPanel

I am trying to place a series of JLabels at specific X and Y coordinates on a JPanel (and also set its height and width). No matter what I do, each shortcut immediately ends to the right of the previous shortcut and is the same size as the rest.

Right now, my Jpanel is in a grid layout. I tried Absolute Layout (illegal results of excluding arguments), Free Design (without shortcuts), Flow Layout (everything just shrinks to the center) and several others.

Not sure what I need to do to make this work. Can anyone help? Thanks!

JLabel lbl1 = new JLabel("label 1"); JLabel lbl2 = new JLabel("label 2"); JLabel lbl3 = new JLabel("label 3"); JLabel lbl4 = new JLabel("label 4"); JLabel lbl5 = new JLabel("label 5"); myPanel.add(lbl1); myPanel.add(lbl2); myPanel.add(lbl3); myPanel.add(lbl4); myPanel.add(lbl5); lbl1.setLocation(27, 20); lbl2.setLocation(123, 20); lbl3.setLocation(273, 20); lbl4.setLocation(363, 20); lbl5.setLocation(453, 20); lbl1.setSize(86, 14); lbl2.setSize(140, 14); lbl3.setSize(80, 14); lbl4.setSize(80, 14); lbl5.setSize(130, 14); 
+8
java layout swing jcomponent
source share
6 answers

You must set your container layout to null:

 myPanel.setLayout(null); 

However, be advised to also take a look at the Matisse Layout Manager, I think it is now called GroupLayout. The main problem with absolute positioning is what happens when a window changes its size.

+16
source share
  • Set the container layout manager to null by calling setLayout(null) .

  • Call the method of the setbounds class of the Component class for each of the children of the container.

  • Call the redraw method of the Component class.

Note:

Creating containers with absolutely positioned containers can cause problems if the window containing the container is resized.

Follow this link: http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

+4
source share

Layout managers are used to automatically determine the layout of components in a container. If you want to place components in specific coordinates, you should not use the layout manager at all.

 myPanel = new JPanel(null); 

or

 myPanel.setLayout(null); 
+3
source share

I can recommend using an IDE, for example NetBeans with its graphical editor. To check the code and because there are many ways:

Setting the layout manager or for absolute positioning that does myPanel.setLayout (null) has several effects.

In general, if you make your calls in the JFrame constructor, you can call pack() to start linking.

Each layout manager then uses its own implementation of add(Component) or add(Component, Constraint) . BorderLayout is used with the addition (label, BorderLayout.CENTER), etc.

+2
source share
  // Best solution!! import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Main { public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = (JPanel) frame.getContentPane(); panel.setLayout(null); JLabel label = new JLabel("aaa"); panel.add(label); Dimension size = label.getPreferredSize(); label.setBounds(100, 100, size.width, size.height); frame.setSize(300, 200); frame.setVisible(true); } } 
0
source share

You can use your own method that calls setSize, setLocation values ​​for direct ....! `Also I will show you how to use the JProgress panel

 import java.awt.*; import java.awt.event.*; import javax.swing.*; class installComp{ void install(Component comp, int w, int h, int x, int y){ comp.setSize(w,h); comp.setLocation(x,y); } } class MyFrame extends JFrame{ int cur_val = 0; JButton btn = new JButton("Mouse Over"); JProgressBar progress = new JProgressBar(0,100); MyFrame (){ installComp comp=new installComp(); comp.install(btn,150,30,175,20); comp.install(progress,400,20,50,70); btn.addMouseListener(new MouseAdapter(){ public void mouseEntered(MouseEvent evt){ cur_val+=2; progress.setValue(cur_val); progress.setStringPainted(true); progress.setString(null); } }); add(btn); add(progress); setLayout(null); setSize(500,150); setResizable(false); setDefaultCloseOperation(3); setLocationRelativeTo(null); setVisible(true); } } class Demo{ public static void main(String args[]){ MyFrame f1=new MyFrame(); } } 
-one
source share

All Articles