0

So I've got a JFrame which uses setLayout(null) so I can position my elements by hand.

However, when accessing the content pane and getting the size for the frame, it says its height is 1.0.

Does anyone know how I can fix this?

Here is the code:

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

public class Launcher extends JFrame
{
    public Launcher(String title) {
        super(title);

        setLayout(null);
        pack();

        setSize(new Dimension(LauncherUtil.LAUNCHER_WIDTH, LauncherUtil.LAUNCHER_HEIGHT));
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        displayComponents();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Launcher launch = new Launcher(LauncherUtil.LAUNCHER_TITLE);
                launch.setVisible(true);
            }
        });
    }

    private void displayComponents() {
        Dimension size = getContentPane().getSize();
        JButton launchButton = new JButton("Launch Game");
        System.out.println(size.getHeight());
        launchButton.setBounds(0, (int)size.getHeight() - 60, (int)size.getWidth(), 60);
        add(launchButton);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
madcrazydrumma
  • 1,847
  • 3
  • 20
  • 38
  • 2
    Either you use an appropriate layout manager and call the method `pack();` for your frame (when all components are added) or you must set the frame size by yourself (see the method `setSize(int, int)`). – Sergiy Medvynskyy Sep 05 '16 at 08:30
  • 2
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Sep 05 '16 at 08:56
  • `launchButton.setBounds(0, (int)size.getHeight() - 60, (int)size.getWidth(), 60);` A button stretched to the width of the bottom? `PAGE_END` of a `BorderLayout`. To increase the height, use `setMargin(Insets)` (or give it a large font or icon). – Andrew Thompson Sep 05 '16 at 08:59

2 Answers2

0

may be if you try to get the screen size by using Toolkit would be work like you want

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

it brings you the screen size where java program is running. Hope it helps.

-1

The content pane is a JPanel whose default size is 1x1px. Since you did not put any components into the content pane and have not set a preferred size for the panel, the content pane's size remains 1x1px.

One way to fix this is to call these three methods in the following order:

setLayout(null);
setPreferredSize(new Dimension(300, 400));
pack();

However, you should be using a layout manager instead of managing the size and position of your components by hand.

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77
  • Layout managers do not fulfill what I need to do and I feel positioning them absolutely is easier than messing with insets etc. – madcrazydrumma Sep 07 '16 at 11:26