JFrame adding Text not working. Two GUI appear

I am trying to create a simple JFrame menu where there is a nice background, which works, along with some text. The text is a bit buggy. Here is how it looks (http://imgur.com/nRpzA30)

As you can see, only one line of the text appears not the second line.

Along with that. Two GUI's appear when I run the program. One is completley empty, and one looks like the picture above.

Finally I can't use the method logo.setHorizontalAlignment(JLabel.NORTH); without getting an error, same with a few others. The two I tested and worked were only .CENTER, and .LEFT.

Any help would be great!

Oh and almost forgot, here's my code :)

    package character;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;

/**
 * Created by Niknea on 6/28/14.
 */
public class characterSelector extends JFrame{


    JPanel cselectorText;

    JFrame  cselectorButtons;

    JLabel logo, characterName, label;

    JButton previous, next;


    public characterSelector(String title){
        super(title);

        this.createCharacterSelector();

        this.setSize(1920, 1033);
        this.setResizable(true);
        this.setVisible(true);
    }


    public void createCharacterSelector(){

      try {
            label = new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/resources/Grass_Background.jpg"))));


            cselectorButtons = new JFrame();

            logo = new JLabel("SoccerKidz [REPLACE W/ COOL LOGO]");

            characterName = new JLabel("<Character Name>");

            logo.setPreferredSize(new Dimension(50, 50));



            logo.setFont(new Font(logo.getFont().getName(), Font.HANGING_BASELINE, 50));

            characterName.setFont(new Font(characterName.getFont().getName(), Font.HANGING_BASELINE, 50));

            cselectorButtons.add(logo);

            cselectorButtons.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            cselectorButtons.setContentPane(label);
            cselectorButtons.setLayout(new BorderLayout());
            characterName.setForeground(Color.CYAN);
            characterName.setHorizontalAlignment(JLabel.CENTER);
            cselectorButtons.add(characterName);
          logo.setForeground(Color.CYAN);
          logo.setHorizontalAlignment(JLabel.LEFT);
            cselectorButtons.add(logo);
            cselectorButtons.pack();
            cselectorButtons.setLocationRelativeTo(null);
            cselectorButtons.setVisible(true);
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }
}

Thanks again! :)

Answers


As you can see, only one line of the text appears not the second line.

You set the layout of cselectorButtons to BorderLayout. Then you add JLabel("<Character Name>");, which is fine. But then you add JLabel("SoccerKidz [REPLACE W/ COOL LOGO]");. Here's what's happening. With BorderLayout, when you just add(component), generally you want to specify a BorderLayout.[POSITION] as the second argument to the add. If you don't, every component that you add, without the position specified, will be added the the BorderLayout.CENTER implicitly by default. The problem with this is each position can only have one component. So in your case, the first label gets kicked out the center, only showing the second one you added.

Two GUI's appear when I run the program. One is completley empty, and one looks like the picture above.

When look at your code. Your class is a JFrame in which you add nothing to, and this.setVisible(true). And also you a JFrame cselectorButtons; in which you do add components and also cselectorButtons.setVisible(true);. Can you guess which one is the one you're seeing that's not empty. Don't extends JFrame. Just use the instance one you currently are using.

Finally I can't use the method logo.setHorizontalAlignment(JLabel.NORTH); without getting an error.

It doesn't take much to take a look at the API for JLabel. That being said, what makes you think aligning something horizontally, should include an option to position to the north. Doesn't make sense

  • public void setHorizontalAlignment(int alignment)

    Sets the alignment of the label's contents along the X axis.

    Parameters: alignment - One of the following constants defined in SwingConstants: LEFT, CENTER (the default for image-only labels), RIGHT, LEADING (the default for text-only labels) or TRAILING.


I suggest you take a look at Layout out Components Within a Container and A Visual Guide to Layout Managers for other possibly layout managers you can use, if BorderLayout doesn't suit you. Also keep in mind you can nest different panels with different layout managers to get your desired result. But first you need to learn how they work.


UPDATE

I made some changes, which works. I'm also looking at your pastebin code, and can't really see any difference. You may want to investigate my code to try and look for differences

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 * Created by Niknea on 6/28/14.
 */
public class CharacterSelector {

    JPanel cselectorText;
    JFrame cselectorButtons;
    JLabel logo, characterName, label;
    JButton previous, next;

    public CharacterSelector() {
        createCharacterSelector();
    }

    public void createCharacterSelector() {

        try {
            label = new JLabel(new ImageIcon(ImageIO.read(getClass()
                    .getResource("/resources/Grass_Background.jpg"))));
            cselectorButtons = new JFrame();
            logo = new JLabel("SoccerKidz [REPLACE W/ COOL LOGO]");
            characterName = new JLabel("<Character Name>");
            logo.setPreferredSize(new Dimension(50, 50));
            logo.setFont(new Font(logo.getFont().getName(),
                    Font.HANGING_BASELINE, 50));
            characterName.setFont(new Font(characterName.getFont().getName(),
                    Font.HANGING_BASELINE, 50));
            cselectorButtons.add(logo);
            cselectorButtons.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            cselectorButtons.setContentPane(label);
            cselectorButtons.setLayout(new BorderLayout());
            characterName.setForeground(Color.CYAN);
            characterName.setHorizontalAlignment(JLabel.CENTER);
            cselectorButtons.add(characterName);
            logo.setForeground(Color.CYAN);
            logo.setHorizontalAlignment(JLabel.LEFT);
            cselectorButtons.add(logo, BorderLayout.NORTH);
            cselectorButtons.pack();
            cselectorButtons.setLocationRelativeTo(null);
            cselectorButtons.setVisible(true);
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new CharacterSelector();
            }
        });
    }
}

Need Your Help

Using iOS filename Device Modifiers (~ipad, ~iphone) in Documents or Caches folder

objective-c ios uiimage imagenamed

Is there a way to take advantage of the device modifiers ~ipad, ~iphone, @2x, for images stored in the cache or documents. As far as I can tell it only works if the files are stored in the main bun...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.