Home > Mobile >  how to display JLabels in separate lines? JavaX GUI
how to display JLabels in separate lines? JavaX GUI

Time:11-19

I would like to display my output in the GUI like the picture below

wanted display

But I end up having it like this when I use setText on JLabels

my program:( display

Here are the codes that display my texts:

class StudentPanel extends JPanel {
    JLabel jlName;
    JLabel jlTitle;
    JLabel jlImageFile;
    JLabel jlGroup;
    JLabel jlDemoWhat;

    private JLabel student1image_2;

    public StudentPanel() {

        //jlName = new JTextArea("<html><body><\n>");
        jlName = new JLabel();
        add(jlName);

        jlTitle = new JLabel();
        add(jlTitle);

        jlImageFile = new JLabel();
        add(jlImageFile);

        jlGroup = new JLabel();
        add(jlGroup);

        jlDemoWhat = new JLabel();
        add(jlDemoWhat);
    }

    public void setStudent(Student s) {
        jlName.setText("Hi Prof, I am "   s.getName());
        jlTitle.setText("I am a "   s.getTitle());
        ImageIcon student1image = new ImageIcon(s.getImageFile());
        Image student1image_1 = student1image.getImage();
        Image student1Resized = student1image_1.getScaledInstance(260, 180, UNDEFINED_CONDITION);
        student1image = new ImageIcon(student1Resized);
        student1image_2 = new JLabel(student1image);
        add(student1image_2);

        jlGroup.setText(s.getGroup());
        jlDemoWhat.setText(s.getDemoWhat());
    }
}

Does anyone know how to separate them?
Thank you so much.

I Googled but so far no hope is found.

CodePudding user response:

In your ctor, StudentPanel(), you can set a vertical layout with

setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); before you add your components.

CodePudding user response:

Oracle has a helpful tutorial, Example

A Swing application should always start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

You use Swing components. The only reason to extend a Swing component, or any Java class, is when you want to override one or more of the class methods.

I hard-coded the student values. You would need to add the references to your Student class.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class StudentGUI implements Runnable {

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

    JLabel jlName;
    JLabel jlTitle;
    JLabel jlImageFile;
    JLabel jlGroup;
    JLabel jlDemoWhat;

    @Override
    public void run() {
        JFrame frame = new JFrame("Student Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createMainPanel(), BorderLayout.CENTER);
        addValues();

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5));
        panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));

        jlName = new JLabel();
        panel.add(jlName);

        jlTitle = new JLabel();
        panel.add(jlTitle);

        jlImageFile = new JLabel();
        panel.add(jlImageFile);

        jlGroup = new JLabel();
        panel.add(jlGroup);

        jlDemoWhat = new JLabel();
        panel.add(jlDemoWhat);

        return panel;
    }
    
    private void addValues() {
        jlName.setText("Hi Prof, I am "   "Heng 2");
        jlTitle.setText("I am a "   "Full Time Student");
        jlGroup.setText("I am from group T05");
        jlDemoWhat.setText("I wish to demo Assignment 2");
    }

}
  • Related