-1

I have a JLabel array but when an image is attached to it and added to my container it doesn't appear on the JFrame, unlike the non-array JLabels.

I've attempted changing Opaque settings of the image it overlays, setting it to visible, and changing the layout of the image.

I'm new to Stack Overflow and relatively new to Swing so all criticism is welcome.

import java.awt.Container;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Connect_Four extends JFrame {
    private static final long serialVersionUID = 1L;
    static JLabel[] piecelabel;
    static int piececounter;

    public Connect_Four(String title) {
        piececounter = 0;

        Container c = getContentPane();

        piecelabel = createLabels();
        piecelabel[piececounter].setIcon(new ImageIcon("F:/redpiece.jpg"));
        piecelabel[piececounter].setBounds(0, (750 - (counter[0] * 100)), 135, 100);
        c.add(piecelabel[piececounter]);
    }

    public JLabel[] createLabels() {
        JLabel[] labels = new JLabel[42]; //used for connect 4 pieces
        for (int i = 0; i < 42; i++) {
            labels[i] = new JLabel();
        }
        return labels;
    }
}
Eric Galluzzo
  • 3,191
  • 1
  • 20
  • 20
Bredward
  • 7
  • 3
  • is this alll?????? do you have a main?? do you make things visible?? I dont see no nothing – gpasch Jun 15 '16 at 16:47
  • Is your USB connected? Is it on the `F:` port? Also avoid the use of `null` layout! See: [Why is it frowned upon to use null layout](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) and [Null Layout is Evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) – Frakcool Jun 15 '16 at 19:43
  • 2
    By the way it will work the same if you place it once or 42 times, please post a [mcve] that includes the relevant code but still shows your issue, also as you're new to Stack Overflow take the [tour](http://stackoverflow.com/tour) and [how to ask](http://stackoverflow.com/help/how-to-ask) – Frakcool Jun 15 '16 at 19:44

2 Answers2

1

I am not sure about your main method. But the below code works fine for me. Can you check and let me know if there is any difference?

import java.awt.Container;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Connect_Four extends JFrame {

    static JLabel[] piecelabel;
    static int piececounter;
    private static final long serialVersionUID = 1L;

    public Connect_Four(String title) {

        piececounter = 0;

        Container c = getContentPane();

        JLabel[] piecelabel = createLabels();
        piecelabel[piececounter].setIcon(
                new ImageIcon("C:/sunshine.png"));
        piecelabel[piececounter].setBounds(0, 0, 135, 100);
        c.add(piecelabel[piececounter]);
    }

    public JLabel[] createLabels() {
        JLabel[] labels = new JLabel[42]; //used for connect 4 pieces
        for (int i = 0; i < 42; i++) {
            labels[i] = new JLabel();
        }
        return labels;
    }

    public static void main(String[] args) {
       Connect_Four c = new Connect_Four("Example");
       c.setVisible(true);
       c.pack();
    }
}
Beniton Fernando
  • 1,533
  • 1
  • 14
  • 21
0

did you make it visibile of call method repaint() after set it

Francesco Taioli
  • 2,687
  • 1
  • 19
  • 34