views:

163

answers:

4

Hi, I have problem while setting the Jlabel location.
I set the content pane to some JPanel, I created and tried to add my JLabel.

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

I want that my JPanel will be on the top left corner on my application and what I am getting is "SomeApp" on the top center.(and not top left).

btw I tried to add JButton the and the I can`t change the width,height,x,y of the JButton.

A: 
Bragboy
Setting layout manager to null is a bad idea.
OscarRyz
A: 

Where a particular widget ends up in its container depends on the layout manager that it's using. The layout manager determines how to resize and arrange the widgets to make them fit appropriately. Obviously, the default layout for the content pane decided that the top center was the best place to put the JLabel.

If you want to get to not use a layout manager and just place everything yourself (which generally isn't the best way to lay things out btw), then add:

getContentPane().setLayout(null);
Jonathan M Davis
Thanks a lot , I am getting another problem - I want to set the buttons look and feel customized to specific platform for example for Window I will get the window style buttons and for mac.This is what I tried - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Yosy
+2  A: 

Swing uses Layout Managers to place the components.

You have to understand how they work to use them effectively. You can set the layout manager to null, and do the layout your self, but is not recommendable because you'll have to keep track of new components each time, and perform layout computation your self when the window moves shrink etc.

Layout managers are a bit hard to grasp at first.

Your windows could be like this:

as simple as this

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 );

    }
}
OscarRyz
A: 

Using layouts is usually a better idea since they allow for dynamic resizing of components. Here's how you'd do it with a 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 could create an additionnal panel with it's 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 usefull 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

ZeBlob
When I am trying your option, with this code - final JLabel mainTitle = new JLabel("IronApp - Choose and Play"); mainTitle.setFont(new Font("Consolas",1 , 28)); mainTitle.setBounds(100, 100, windowWidth, 50); mainTitle.setForeground(Color.WHITE); getContentPane().add(mainTitle,BorderLayout.NORTH);I can`t control my title postion move it left,down or right.
Yosy
@Yosy Use a custom border for that: http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/components/border.html
OscarRyz
Usually when I use layouts I try to never specify hard coded sizes except for h and v gaps. Instead I use BorderLayouts to push things around the screen so they end up where I want. If you look at the images for BorderLayouts in the first link, you'll see that anything dropped in the NORTH location will take up the entire space length-wise and only what's needed height-wise. So let's say I want to push the label to the right, I just create a new panel and add the label to the EAST region, effectively pushing it to the right. To push it to the left, use WEST.
ZeBlob