Ricardo
Ricardo

Reputation: 69

Java help: JLabel's

I created this program that displays 2 photos and some facts (JLabel) about myself. However, the facts about myself are to the right of the pages. I would like the facts (JLabels) to be right under the pictures stacked, like a title and bullet points under the title, any help?

Ignore the import for sound. Eventually, I want the program to play a tune.

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

public class AudioandImage extends JFrame {
    public ImageIcon image1;
    public JLabel label1;
    public ImageIcon image2;
    public JLabel label2;
    public JLabel name;
    public JLabel facts;
    public JLabel born;
    public JLabel es;
    public JLabel sport;
    public JLabel lastly;

    AudioandImage() {
        setLayout (new FlowLayout());
        image1 = new ImageIcon(getClass().getResource("losangeles.jpg"));

        label1 = new JLabel(image1);
        add(label1);

        image2 = new ImageIcon(getClass().getResource("elsalvador.jpg"));

        label2 = new JLabel(image2);
        add(label2);

        name = new JLabel("My name is Erik Landaverde");
        add(name);
        facts = new JLabel("Some facts about myself:");
        add(facts);
        born = new JLabel("I was born and raised in South Central Los Angeles");
        add(born);
        es =  new JLabel("Have a Salvadorean background");
        add(es);
        sport = new JLabel("My favorite sport is soccer");
        add(sport);
        lastly = new JLabel("Lastly... I am a programmer!");
        add(lastly);
    }

    public static void main(String[] args) {
        AudioandImage gui = new AudioandImage();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.pack();
        gui.setTitle("A Little About Myself");
    }
}

Upvotes: 0

Views: 97

Answers (3)

Dev. Joel
Dev. Joel

Reputation: 1137

The desired result can be accomplished by assigning a different layout to your JFrame to FlowLayout() BorderLayout for example,

setLayout (new BorderLayout()); // asign layout to JFrame

add(label1,BorderLayout.PAGE_START); //Add JLabel 1 to Jframe

add(label2,BorderLayout.CENTER); //Add JLabel 2 to Jframe

 name = new JLabel("<html><ul>My name is : Erik  Landaverde "
            + "<li/>Some facts about myself: </li> "
            + "<li/>I was born and raised in South Central Los Angeles</li>"
            + "<li/>Have a Salvadorean background</li>"
            + "<li/>My favorite sport is soccer</li>"
            + "<li/>Lastly... I am a programmer!</li></ul></html>", SwingConstants.CENTER);

add(name,BorderLayout.PAGE_END); //Add JLabel 3 to Jframe

for the label in March added it into one because only contained what I format strings with <html> </html> and line with <ul><li>content</li></ul>

Upvotes: 1

Jack
Jack

Reputation: 193

Use Below code for GridBagLayout

Test() {
    setLayout(new GridBagLayout());
    image1 = new ImageIcon(getClass().getClassLoader().getResource("image.jpg"));

    label1 = new JLabel(image1);
    add(label1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));

    name = new JLabel("My name is Erik Landaverde");
    add(name, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
    facts = new JLabel("Some facts about myself:");
    add(facts, new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
    born = new JLabel("I was born and raised in South Central Los Angeles");
    add(born, new GridBagConstraints(0, 4, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
    es = new JLabel("Have a Salvadorean background");
    add(es, new GridBagConstraints(0, 5, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
    sport = new JLabel("My favorite sport is soccer");
    add(sport, new GridBagConstraints(0, 6, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
    lastly = new JLabel("Lastly... I am a programmer!");
    add(lastly, new GridBagConstraints(0, 7, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
}

public static void main(String[] args) {
    Test gui = new Test();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setVisible(true);
    gui.pack();
    gui.setTitle("A Little About Myself");
}

Upvotes: 0

Ashwinee K Jha
Ashwinee K Jha

Reputation: 9317

Flow layout is not suited for the way you want to display the labels. See https://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html for tutorial on flow layout.

You can also find tutorial to BorderLayout from above link. You could use this layout here. Create a JPanel with your labels using GridLayout with 1 column. Then place it in CENTER or SOUTH of the JFrame using BroderLayout.

Another approach could be to write your text as a single HTML and set it in JTextPane etc.

Upvotes: 0

Related Questions