.setLocation and .setBounds not working on JLabel! (and JButton)

So I have looked through ton of topics about this, but none seem to fix my problem! When I try to use setBounds() or setLocation() on JLabel or JButton it just doesn't work! It seems to place both of them just randomly. Here is my code:

public static void main(String[] args) {
    final JFrame frame = new JFrame(TITLE);
    final JLabel fpsLabel = new JLabel("FPS: ERROR");
    final JLabel fpsDone = new JLabel("FPS done: ERROR");
    final JPanel contentPanel = new JPanel();

    frame.setSize(WIDTH, HEIGHT);
            frame.setContentPane(contentPanel);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    fpsLabel.setLocation(WIDTH / 2, HEIGHT / 6);
    fpsDone.setLocation(200, HEIGHT / 6);
    frame.setJMenuBar(menubar);
    frame.setResizable(false);
    frame.add(fpsLabel);
    frame.add(fpsDone);
    frame.setVisible(true);
}

If needed I can add picture.

SSCCE:

public static void main(String[] args) {
            final JFrame frame = new JFrame("Example SSCCE");
            final JLabel fpsLabel = new JLabel("FPS: ERROR");
            final JLabel fpsDone = new JLabel("FPS done: ERROR");
            final JPanel contentPanel = new JPanel();
            final int HEIGHT = 400 / 16 * 9;

            frame.setSize(400, HEIGHT);
            frame.setContentPane(contentPanel);
            fpsDone.setLocation(200, HEIGHT / 2);
            fpsLabel.setLocation(200, HEIGHT / 2 + 50);
            contentPanel.add(fpsDone);
            contentPanel.add(fpsLabel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
    }

What I would want it to look like (ASCII art?):

__________________________________
|                                |
|                                |
|                                |
|                                |
|                                |
|           FPS: ERROR           |
|         FPS done: ERROR        |
|                                |
|                                |
|                                |
|                                |
|                                |
__________________________________

Answers


Try adjusting this example to need:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class SingleColumnFrame {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                // the values of '20' are for gaps between components
                JPanel gui = new JPanel(new GridLayout(0,1,20,20));
                // adjust numbers as required
                gui.setBorder(new EmptyBorder(40, 200, 40, 200));

                gui.add(new JLabel("FPS: ERROR",SwingConstants.CENTER));
                gui.add(new JLabel("FPS done: ERROR",SwingConstants.CENTER));

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                f.setResizable(false);
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

Need Your Help

How to create bundle timeline card

google-glass google-mirror-api

I want to create bundle timeline card. What I did so far.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.