-1

I have tried setLocation(x,y) and setLocationRelativeTo(null) by setting Layout of JFrame as null but that didn't work out.While searching I found this questions being already asked by two or three people but they have done through setLocation() and setLocationRelativeTo(null).

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.FlowLayout;

public class StartMenu{
    JPanel startPanel;
    JLabel title;
    JButton startTest;
    JButton exit;
    JFrame menuFrame;

    public StartMenu(){
        menuFrame = new JFrame("Start Menu");
        menuFrame.setLayout(null);

        startPanel = new JPanel();

        title = new JLabel("Adaptive Test",JLabel.CENTER);
        title.setLocation(20,20);
        startPanel.add(title);

        startTest = new JButton("Start");
        startTest.setLocation(40,40);
        startPanel.add(startTest);

        exit = new JButton("Exit");
        exit.setLocation(100,100);
        startPanel.add(exit);
        menuFrame.setContentPane(startPanel);

        menuFrame.setVisible(true);
        menuFrame.setSize(500, 500);
        menuFrame.setResizable(false);
        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kamal Vidhani
  • 133
  • 1
  • 2
  • 12
  • 3
    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). – Andrew Thompson Sep 04 '15 at 19:05
  • 3
    I'm voting to close this question as off-topic because it is asking how to implement a hack, when the better strategy would be to use layouts, borders and padding. – Andrew Thompson Sep 04 '15 at 19:09
  • 1
    Your `startPanel` still has the default `FlowLayout` as layout. Change only the startPanel's layout to null (not the frame's), and use `setBounds()`... This only applies if you really want a null-layout, of course (for learning reasons or whatever). - If not necessary, use [layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). – Lukas Rotter Sep 04 '15 at 19:11
  • That really helped alot..! thanks to all. – Kamal Vidhani Sep 04 '15 at 19:31

2 Answers2

1

Your JFrame's layout is set to null, but the startPanel's one is not. So first of all, use:

startPanel.setLayout(null);

Now, instead of component.setLocation(x, y), use component.setBounds(x, y, width, height), so you also set a size for them.

But as has been said in the comments, it would be preferable to use layout managers instead of null layout.

eric.m
  • 1,577
  • 10
  • 20
-1

First you set to null the JFrame istead of the JPanel, so you have to use

startPanel.setLayout(null);

then you should use setBounds istead of setLocation becouse if you just set the location with a null layout manager you'll probably will see nothing on your panel, becouse all dimensions are initialized to 0 by default.

so, you can rewrite your software like this:

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class StartMenu{
    JPanel startPanel;
    JLabel title;
    JButton startTest;
    JButton exit;
    JFrame menuFrame;

    public StartMenu(){
        menuFrame = new JFrame("Start Menu");
        menuFrame.setLayout(null);

        startPanel = new JPanel();
        startPanel.setLayout(null);

        title = new JLabel("Adaptive Test",JLabel.CENTER);
        title.setBounds(20,20, 100, 30);
        startPanel.add(title);

        startTest = new JButton("Start");
        startTest.setBounds(50,50, 100, 30);
        startPanel.add(startTest);

        exit = new JButton("Exit");
        exit.setBounds(100,100, 100 ,30);
        startPanel.add(exit);
        menuFrame.setContentPane(startPanel);

        menuFrame.setVisible(true);
        menuFrame.setSize(500, 500);
        menuFrame.setResizable(false);
        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

Like this you get something working but it isn't a very good programming practice to set by hand all the location and sizes in your software becouse it will not work good on different systems (thus it will not be portable) and you will also find yourself in a debugging nightmare when your software will start growing with tens if not hundreds of graphics elements.

My suggestion is to use the gridBagLayout, it me seem obscure at first, but trust me, it's not so!