Java Swing - JLabel Location

I have a problem setting my Jlabel location.
I installed the content panel in some kind of JPanel, I created and tried to add JLabel.

JLabel mainTitle = new JLabel("SomeApp"); mainTitle.setFont(new Font("Arial",2 , 28)); mainTitle.setBounds(0,0, 115, 130); getContentPane().add(mainTitle); 

I want my JPanel to be in the upper left corner of my application, and what I get is "SomeApp" in the top center (and not top left).

btw I tried adding JButton the and I can’t change the width, height, x, y of JButton.

+4
source share
3 answers

Swing uses Layout Managers to host components.

You must understand how they work in order to use them effectively. You can set the layout manager to zero and make the layout yourself, but it is not recommended because you have to keep track of new components each time and calculate the layout yourself when the window moves with a decrease, etc.

Layout managers are hard to understand at first.

Your windows may look like this:

as simple as this http://img827.imageshack.us/img827/7043/capturadepantalla201007z.png

Using this code:

 import javax.swing.*; import java.awt.Font; import java.awt.FlowLayout; class JLabelLocation { public static void main( String [] args ) { JLabel mainTitle = new JLabel("SomeApp"); mainTitle.setFont(new Font("Arial",2 , 28)); //mainTitle.setBounds(0,0, 115, 130); //let the layout do the work JFrame frame = new JFrame(); JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));// places at the left panel.add( mainTitle ); frame.add( panel );// no need to call getContentPane frame.pack(); frame.setVisible( true ); } } 
+4
source

If a particular widget ends in its container, it depends on the layout manager it uses. The layout manager determines how to resize and arrange widgets to fit their requirements. Obviously, the default layout for the content area decided that the top center was the best place to host JLabel.

If you want to not use the layout manager and just put everything on your own (which is usually not the best way to get things from under it), add:

 getContentPane().setLayout(null); 
+1
source

Using layouts is usually a better idea, as they allow you to dynamically resize components. Here's how you would do it with BorderLayout:

 this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add (new JLabel ("Main title"), BorderLayout.NORTH); 

If you want to add something to the right of the label, you can create an additional panel with your own layout:

 // Create a panel at the top for the title and anything else you might need JPanel titlePanel = new JPanel (new BorderLayout()); titlePanel.add(new JLabel ("Main title"), BorderLayout.WEST); // Add the title panel to the frame this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(titlePanel, BorderLayout.CENTER); 

Here are some useful links to get started with layouts:

http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/visual.html http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout /using.html

0
source

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


All Articles