1

This code displays nothing, I have exhausted many avenues but it does not display anything on the GUI (I have a main class that calls this as well already). Please help. I am trying to put the two JButtons horizontally at the bottom of the page and the JTextField and JLabel at the center of the screen.

package test;

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

public class Gui extends JFrame {
    private JLabel label;
    private JButton clear;
    private JButton copy;
    private JTextField textfield;

    public Gui(){

        super("test");

        clear = new JButton("Clear");
        copy = new JButton("Copy");
        label = new JLabel("");
        textfield = new JTextField("enter text here");

        JPanel bottom = new JPanel(new BorderLayout());

        JPanel subBottom = new JPanel();
        subBottom.add(copy);
        subBottom.add(clear);


        JPanel centre = new JPanel (new BorderLayout());

        JPanel subCentre = new JPanel();
        subCentre.add(label);
        subCentre.add(textfield);





        bottom.add(subBottom, BorderLayout.PAGE_END);
        centre.add(subCentre, BorderLayout.CENTER);

        }


    }
Cameron2222
  • 41
  • 1
  • 7
  • 2
    Aren't you supposed to do something to those panels, like add them to your frame? – Qwerky May 22 '17 at 21:35
  • You should look into [GridLayouts](http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html). It might seem a bit confusing, so you may have to read it twice. But this is how I learned how to use GridLayouts, and it is very helpful for me now. –  May 22 '17 at 22:54
  • @Cameron2222 see my answer below. Is it what you wanted? –  May 22 '17 at 23:24
  • @theProgrammer101 yes it is a good solution thank you. I had set visible and such in the other class that made the instance of my GUI class. You got me understanding this a bit better. What does the pack(); do? – Cameron2222 May 23 '17 at 01:33
  • @theProgrammer101 +1'd – Cameron2222 May 23 '17 at 01:53

2 Answers2

3

Your code is a bit over-complicated. You only need two panels, centre, and buttons. There are two reasons your UI is not showing up:

  1. You never added the panels to the frame
  2. You never set visible to true(Achieve this by using setVisible(true)), unless you did this in the class you ran it in.

One simple way to achieve your desired UI is like so(I added a main method to show the window):

import javax.swing.*;
import java.awt.*;
public class test extends JFrame {
    public Test() {
        super("test");
        JPanel buttons = new JPanel();
        JPanel centre = new JPanel();
        add(buttons, BorderLayout.SOUTH); //these lines add the 
        add(centre, BorderLayout.CENTER); //panels to the frame
        JButton clear = new JButton("Clear");                     // No need
        JButton copy = new JButton("Copy");                       // to declare
        JLabel label = new JLabel("Label");                       // these
        JTextField textfield = new JTextField("enter text here"); // privately
        buttons.add(copy); 
        buttons.add(clear);
        centre.add(label);
        centre.add(textfield);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    } //end constructor

    //added main method to run the UI
    public static void main(String[] args) {
        new Experiments();
    } //end main
} //end class

And it shows the window:

Window

Community
  • 1
  • 1
  • Excellent thank you for simplifying that for me. I had it appearing, the GUI was constructed in a main that contained Gui go = new Gui(); go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); go.setSize(500,500); go.setVisible(true); – Cameron2222 May 23 '17 at 01:30
  • @Cameron2222 Ok, then your only problem was that you weren't adding your panels. Glad I could help! Please consider accepting this answer if you found it helpful. Thanks! –  May 23 '17 at 03:53
  • @Cameron2222 think of `pack();` as a vacuum seal bag. It simply closes in on the components tightly, without cutting any of them off. This is useful because you don't need to guess the size of the frame, and keep adjusting it until it fits, it automatically does this for you. take a look at [this Javadoc](http://docs.oracle.com/javase/8/docs/api/java/awt/Window.html#pack--) and [this post.](https://stackoverflow.com/questions/22982295/what-does-pack-do) –  May 23 '17 at 03:57
2

I got closer but its not pretty code, the JFrame is 500x500 so this works based on that... any better suggestions than what I have?

package lab6;

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

public class Gui extends JFrame {
    private JLabel label;
    private JButton clear;
    private JButton copy;
    private JTextField textfield;

    public Gui(){

        super("test");

        clear = new JButton("Clear");
        copy = new JButton("Copy");
        label = new JLabel("label");
        textfield = new JTextField("enter text here");

        JPanel masterPanel = new JPanel(new BorderLayout());

        JPanel top = new JPanel();
        top.setPreferredSize(new Dimension(100, 200));

        JPanel bottom = new JPanel();

        JPanel subBottom = new JPanel();
        subBottom.add(copy);
        subBottom.add(clear);


        JPanel centre = new JPanel ();

        JPanel subCentre = new JPanel();
        subCentre.add(label);
        subCentre.add(textfield);


        bottom.add(subBottom);
        centre.add(subCentre);

        masterPanel.add(bottom, BorderLayout.PAGE_END);
        masterPanel.add(top, BorderLayout.PAGE_START);
        masterPanel.add(centre, BorderLayout.CENTER);


        add(masterPanel);
    }


}
Cameron2222
  • 41
  • 1
  • 7
  • `I am trying to put the two JButtons horizontally at the bottom of the page` - Read the section from the Swing tutorial on [How to Use BorderLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html) for working examples. If you want the panel containing the buttons at the bottom then you need to specify a constraint when you add the panel to the frame. – camickr May 22 '17 at 21:53
  • Why are you using a "master panel"? Did you not read the tutorial? The working examples show you how to better create the GUI. Start with the working example and make changes. – camickr May 22 '17 at 22:16
  • The tutorial states "The components are laid out according to their preferred sizes and the constraints of the container's size. The North and South components may be stretched horizontally; the East and West components may be stretched vertically; the Center component may stretch both horizontally and vertically to fill any space left over." I cant see how it states how to apply the constraints? – Cameron2222 May 23 '17 at 01:40
  • 1
    The "PAGE_START", "CENTER" are the constraints for the BorderLayout. Constraints are specific to the layout manager and not all layout managers, like FlowLayout, use constraints. – camickr May 23 '17 at 04:00