Include JLabel in JPanel

0

I am trying to show a JLabel in a JPanel, but it is not shown to me. I do not know why it will be

My code is as follows:

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

public class DialogoCancion extends JFrame {
    private JPanel panel;
    private JLabel titulo;
    private JLabel interprete;
    private JLabel duracion;
    private JButton añadir;
    private JButton cancelar;

    public DialogoCancion(){
        super("Añadir Cancion");
        setBounds(150, 80, 250, 150);
        panel = new JPanel();
        panel.setLayout(null);
        titulo = new JLabel("Titulo");
        interprete = new JLabel ("Interprete");
        duracion = new JLabel ("Duracion");
        añadir = new JButton ("Añadir");
        cancelar = new JButton ("Cancelar");

        titulo.setBounds(10, 10, 80, 80);
        panel.add(titulo);
        panel.add(interprete);
        panel.add(duracion);
        panel.add(añadir);
        panel.add(cancelar);

        setVisible(true);
    }
}

I want to see titulo , but nothing comes out.

    
asked by Juanma Perez 26.12.2016 в 23:21
source

3 answers

0

You should add to the end of your code

add(panel);
setVisible(true);

Since your class extends a JFrame when you create the instance of your class you can see it but as long as you do not add the panel to your JFrame you will not see anything.

    
answered by 26.12.2016 / 23:42
source
0

you're missing the add (panel) because you use extends jframe

but if you do not use it extends JFrame and invokes it as a serious Object:

asi: note that you install Jframe as an object and add the panel to the frame

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

public class DialogoCancion  {
private JFrame frame;
private JPanel panel;
private JLabel titulo;
private JLabel interprete;
private JLabel duracion;
private JButton añadir;
private JButton cancelar;

public DialogoCancion(){
    frame=new JFrame();
    frame.setBounds(150, 80, 250, 150);
    panel = new JPanel();
    panel.setLayout(null);
    titulo = new JLabel("Titulo");
    interprete = new JLabel ("Interprete");
    duracion = new JLabel ("Duracion");
    añadir = new JButton ("Añadir");
    cancelar = new JButton ("Cancelar");

    titulo.setBounds(10, 10, 80, 80);
    panel.add(titulo);
    panel.add(interprete);
    panel.add(duracion);
    panel.add(añadir);
    panel.add(cancelar);
    frame.add(panel);
    frame.setVisible(true);
}

public static void main(String args[]){
    DialogoCancion dc=new DialogoCancion();
}
}

here I leave you a screenshot that demonstrates the functionality

    
answered by 26.12.2016 в 23:56
0

I found some inconveniences. You did not have a main method. The main method is not necessary in the DialogoCancion class, but since you did not include the other classes or do not know if it is the only one you use, add it. The idea is for your class to be functional and you can see the result. You have not placed an add to the panel as well as your layer as BoxLayout (panel, BoxLayout.Y_AXIS) so that your elements appear one below the other.

import javax.swing.*;
    public class DialogoCancion extends JFrame {
        private JPanel panel;
        private JLabel titulo;
        private JLabel interprete;
        private JLabel duracion;
        private JButton añadir;
        private JButton cancelar;

        public DialogoCancion(){
            super("Añadir Cancion");
            setBounds(150, 80, 250, 150);
            panel = new JPanel();
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
            titulo = new JLabel("Titulo");
            interprete = new JLabel ("Interprete");
            duracion = new JLabel ("Duracion");
            añadir = new JButton ("Añadir");
            cancelar = new JButton ("Cancelar");

            titulo.setBounds(10, 10, 80, 80);
            panel.add(titulo);
            panel.add(interprete);
            panel.add(duracion);
            panel.add(añadir);
            panel.add(cancelar);

            add(panel);

            setVisible(true);
        }

        public static void main(String[] args){
            DialogoCancion frame = new DialogoCancion();
        }
    }
    
answered by 27.12.2016 в 00:00