1

Trying to get a 4 by 4 grid of grass pictures to appear in my panel, code is below am i missing something obvious ? Any help greatly appreciated. The panel is appearing blank no errors. I tried adding in pack but all it did was make the frame appear small.

public class frame extends JFrame {

private JPanel contentPane;
private JPanel panel;

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

/**
 * Create the frame.
 */
public frame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    contentPane.add(getPanel());


}
private JPanel getPanel() {
    if (panel == null) {
        panel = new JPanel();
        panel.setBounds(49, 43, 252, 129);
        ImageIcon grassIcon = new ImageIcon("images/grass.gif");
        int size = 4;

        JPanel panel = new JPanel(new GridLayout(size,size,0,0));

        JLabel labels[] = new JLabel[(size*size)];

        for (int i =  0; i < size*size; i++)
        {
              labels[i] = new JLabel(grassIcon);
              panel.add(labels[i]);
        }

    }
    return panel;
}
}
Helen
  • 31
  • 5
  • Trashgod could you point me in the direction of the already answered ? I have tried everything I found on here and it made no difference unless I have missed something – Helen Apr 12 '15 at 22:59
  • *"point me in the direction of the already answered ?"* The link is at the top of the question.. – Andrew Thompson Apr 12 '15 at 23:00

0 Answers0