0

I am new to working with GUI's in Java and I am having a problem moving my text and buttons around. No matter what coordinates I give my button or any of the other JLabel it doesn't move, I was wondering how I could fix it this in such a way that I can place my components where ever I want on the JPanel

public class IntroPage  extends JFrame {

public static void main(String[] args) {

        IntroPage main = new IntroPage();
        main.setVisible(true);
    }

private JPanel contentPane;

    public IntroPage (){

        //make sure the program exits when the frame closes
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Welcome");
        contentPane = new JPanel();
        setSize(400,700);

        //This will center the JFrame in the middle of the screen
        setLocationRelativeTo(null);

        //Welcome Page stuff :D 
        JLabel ApplauseLabel = new JLabel("Welcome to U.X.Dot.X");
        ApplauseLabel.setFont(new Font("Gill Sans MT", Font.PLAIN, 30));
        ApplauseLabel.setLocation(100, 50);
        contentPane.add(ApplauseLabel);

        JLabel slogan = new JLabel("Register below");
        slogan.setFont(new Font("Gill Sans MT", Font.PLAIN, 15));
        slogan.setLocation(100, 400);  
        contentPane.add(slogan);      

        //FacebookSignUp.
        JButton FBbutton = new JButton("Login With FaceBook");
        FBbutton.setBackground(Color.BLUE);
        FBbutton.setSize(50,50);
        FBbutton.setLocation(20, 40);
        FBbutton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //Add JPanel to go to FB API. Much later
            }
        });
        contentPane.add(FBbutton);

        add(contentPane);

        //make sure the JFrame is visible
        setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
cuber
  • 371
  • 5
  • 20
  • 1) 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. 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 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 .. – Andrew Thompson Apr 09 '17 at 15:39
  • .. 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). 4) `new Font("Gill Sans MT", Font.PLAIN, 15)` This is a frgaile way to set a `Font`. Most systems won't have it installed, so best to check for the existence of it first, then default to one of the 'generic' fonts such as `Font.SERIF` or `Font.SANS_SERIF` if needed. The generic fonts will always be available (though might change from OS to OS - another reason not to use exact positioning). – Andrew Thompson Apr 09 '17 at 15:39
  • Then what should I use instead to position my stuff? @AndrewThompson. I was using `GridBag` but placing them like 1,0 or 4,0 would make it not move. – cuber Apr 09 '17 at 15:45
  • *"Then what should I use instead to position my stuff?"* Laying out GUIs can take a little time (to get used to how layouts work). Why not follow the advice in my first comment and give a drawing of what the GUI should look like? – Andrew Thompson Apr 09 '17 at 15:46
  • It isn't really anything special in particular, what I want to do is just place my text and buttons on middle lower on the screen. @AndrewThompson . Ideally wherever I want on the Panel. – cuber Apr 09 '17 at 15:55

1 Answers1

3

You're ignoring the layout managers of your contentPane JPanel. Understand that it uses FlowLayout by default, and will ignore your setLocation and setBounds statements. Ror the JPanel to accept absolute positioning, you would have to give it a null layout via contentPane.setLayout(null).

Having said that, I do not advise you to do this! While null layouts, setLocation(...) and setBounds(...) might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

For example the following GUI

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;

import javax.swing.*;

public class IntroPage2 extends JPanel {
    public static final String TITLE = "Welcome to U.X.Dot.X";
    private JLabel welcomeLabel = new JLabel(TITLE, SwingConstants.CENTER);
    private JButton fbButton = new JButton("Login With Facebook");

    public IntroPage2() {
        fbButton.setBackground(Color.BLUE);
        fbButton.setForeground(Color.CYAN);
        welcomeLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 30));
        int wlGap = 20;
        welcomeLabel.setBorder(BorderFactory.createEmptyBorder(wlGap, wlGap, wlGap, wlGap));

        JLabel registerBelowLabel = new JLabel("Register Below");
        registerBelowLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 15));
        JPanel centralPanel = new JPanel(new GridBagLayout());
        centralPanel.setPreferredSize(new Dimension(300, 600));
        centralPanel.add(registerBelowLabel);

        JPanel topPanel = new JPanel(new BorderLayout());
        topPanel.add(fbButton, BorderLayout.LINE_START);
        topPanel.add(welcomeLabel, BorderLayout.CENTER);

        setLayout(new BorderLayout());
        int ebGap = 8;
        setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
        add(topPanel, BorderLayout.PAGE_START);
        add(centralPanel, BorderLayout.CENTER);
    }

    private static void createAndShowGui() {
        IntroPage2 mainPanel = new IntroPage2();

        JFrame frame = new JFrame("Welcome");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

would create something like:

enter image description here

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373