-3

I'm trying to get my labels to be in the center of the window when I press the corresponding button, but it instead just sits on top of the button instead of sitting in the middle and i'm not sure why

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class navigator extends JFrame
{
Container con;
public navigator(){
    super("JFrame");


    JFrame newFrame = new JFrame("Navigator");
    newFrame.setSize(400, 400);
    newFrame.setVisible(true);
    newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    con = getContentPane();
    BorderLayout newLayout = new BorderLayout();
    con.setLayout(newLayout);



    JButton newButton = new JButton("Up");
    newFrame.add(newButton, BorderLayout.NORTH);

    JButton newButton2 = new JButton("Left");
    newFrame.add(newButton2, BorderLayout.WEST);

    JButton newButton3 = new JButton("Down");
    newFrame.add(newButton3, BorderLayout.SOUTH);


    JButton newButton4 = new JButton("Right");
    newFrame.add(newButton4, BorderLayout.EAST);




    JLabel newLabel = new JLabel("Going up!");
    newFrame.add(newLabel, BorderLayout.CENTER);
    newLabel.setVisible(false);
    newButton.add(newLabel);


    JLabel newLabel2 = new JLabel("Going left!");
    newFrame.add(newLabel2, BorderLayout.CENTER);
    newLabel2.setVisible(false);
    newButton2.add(newLabel2);


    JLabel newLabel3 = new JLabel("Going down!");
    newFrame.add(newLabel3, BorderLayout.CENTER);
    newLabel3.setVisible(false);
    newButton3.add(newLabel3);


    JLabel newLabel4 = new JLabel("Going right!");
    newFrame.add(newLabel4, BorderLayout.CENTER);
    newLabel4.setVisible(false);
    newButton4.add(newLabel4);



    newButton.addActionListener(new ActionListener()  
    {
     public void actionPerformed(ActionEvent e)
    {
     newLabel.setVisible(true); 
     newLabel2.setVisible(false);         
     newLabel3.setVisible(false);
     newLabel4.setVisible(false);
     }
   });



   newButton2.addActionListener(new ActionListener()   
    {
     public void actionPerformed(ActionEvent e)
    {
     newLabel2.setVisible(true);
     newLabel.setVisible(false);
     newLabel3.setVisible(false);
     newLabel4.setVisible(false);
     }
   });



   newButton3.addActionListener(new ActionListener()      
    {
     public void actionPerformed(ActionEvent e)
    {
     newLabel3.setVisible(true);
     newLabel2.setVisible(false);
     newLabel.setVisible(false);
     newLabel4.setVisible(false);
     }
   });



   newButton4.addActionListener(new ActionListener()      
    {
     public void actionPerformed(ActionEvent e)
    {
     newLabel4.setVisible(true);
     newLabel2.setVisible(false);
     newLabel3.setVisible(false);
     newLabel.setVisible(false);
     }
   });


 }

  public static void main(String[] args){                  
    navigator myNavigator = new navigator();    
  }

  }
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    Welcome to Stack Overflow! [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) `BorderLayout.center` does not represent what you think it does. Did you read the [Javadoc](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html)? –  Feb 19 '17 at 00:56
  • @Jarrod Roberson It's running fine but I made a logic error, i'm just not sure what is wrong. This is the output i'm getting. https://gyazo.com/5438c9abf42965922fc99bd429288c6c – Jamie Beaumont Feb 19 '17 at 01:07

2 Answers2

1
JLabel newLabel = new JLabel("Going up!");
newFrame.add(newLabel, BorderLayout.CENTER);
newLabel.setVisible(false);
newButton.add(newLabel); // ???

A component can only have a single parent. So you can't add the label to the frame and the button. I'm not even sure why you would be attempting to add the label to the button.

In any case you can't add four labels to the center of the frame. The BorderLayout only allows one component in each area, so only the last component added will ever be visible. The BorderLayout will only set the size of the last button added. All the other buttons will have a size of (0, 0) so there is nothing to paint.

So just add a single label and then change the text using the setText(...) method in your ActionListener.

However, once you fix this you will still have a problem. By default a label is painted at the left of the space available to the label.

If you want the text displayed in the center then you need to use:

label.setHorizontalAlignment(JLabel.CENTER);

Also, all components should be added to the frame before making the frame visible.

Finally, class names should start with an upper case character. Look at the class names of the JDK API and follow the conventions used their.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • This worked, big thanks :D. I realised mid way that I had no reason to add the label to the button as well, dumb error. Thanks for the help! – Jamie Beaumont Feb 19 '17 at 12:00
0

When using a BorderLayout, you can only put one control in each section. So you can only put one button in CENTER, for instance.

If you want to put more things in one area, then you need to create a new JPanel, put it in CENTER, and then place the buttons on the newly created JPanel. (Of course following the same layout rules for it). You can recursively add as many jpanels as you like.

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • I made a panel and stuck the labels on it but now they're appearing at the top. I also tried creating a button, setting it's visibility to false and putting the labels on it, but then only 'Going right!' appears. Also, this is what I mean by appearing at the top https://gyazo.com/3f7de3a565d148acb57051820be3d422 – Jamie Beaumont Feb 19 '17 at 01:52