문제

I'm trying to add various JButtons to a JPanel using "for" but doesn't work. No compilation or other errors. Buttons just won't appear.

A bit of context, I create an ArryList in another class ("GestorFrigo") that gets data from DataBase, this works fine, the array has all the data and there's no problem getting back the data from the array.

This is my code: Thanks in advance.

    import gestor.GestorFrigo;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JScrollBar;

@SuppressWarnings("serial")
public class VentanaInterior extends JFrame {

    private JPanel contentPane;
    private JButton btnPerfiles;
    private JButton btnAadir;
    private JButton btnRecetas;
    private JScrollBar scrollBar;

    private GestorFrigo frigo;

    private ArrayList<JButton> botones;
    private ArrayList<Object> boton;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    VentanaInterior frame = new VentanaInterior();
                    frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public VentanaInterior() {

        //Componentes       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 556, 363);
        setLocationRelativeTo(null);
        setTitle("Tu Frigorífico Inteligente");
        setIconImage(new ImageIcon(getClass().getResource("img/logo.png")).getImage());

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btnPerfiles = new JButton("Perfiles");
        btnPerfiles.setBounds(0, 302, 96, 23);
        contentPane.add(btnPerfiles);

        btnAadir = new JButton("A\u00F1adir alimento");
        btnAadir.setBounds(369, 302, 148, 23);
        contentPane.add(btnAadir);

        btnRecetas = new JButton("Recetas");
        btnRecetas.setBounds(96, 302, 103, 23);
        contentPane.add(btnRecetas);

        scrollBar = new JScrollBar();
        scrollBar.setBounds(523, 0, 17, 325);
        contentPane.add(scrollBar);

        JButton btnQueso = new JButton();
        btnQueso.setBounds(24, 35, 62, 61);
        contentPane.add(btnQueso);

        frigo = new GestorFrigo(); //creamos el gestor
        String imagen = frigo.getArray().get(1).getImagen(); //cogemos la imagen asociada al alimento

        btnQueso.setIcon(new ImageIcon("src/img/"+imagen));

        for(int i=0;i<frigo.getArray().size();i++){

            JButton boton = new JButton();
            String imagen2 = frigo.getArray().get(i).getImagen();
            boton.setIcon(new ImageIcon("src/img/"+imagen2));
            contentPane.add(boton);

        }


    }
}
도움이 되었습니까?

해결책

Don't use a null layout.

By default your buttons have a default size of (0, 0) so there is nothing to paint.

User a layout manager, probably a GridLayout, and the layout manager will determine the size and location of each button for you.

Read the section from the Swing tutorial o Using Layout Managers for more information and working examples.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top