0

I am trying to get the the bilde() methode add images too my JLabel Array when the nyOmgang button is pressed, its been a hard day and I have gotten stuck.

private JLabel[] kort = new JLabel[16];
private JLabel[] kortForside = new JLabel[16];
private JButton nyOmgang = new JButton("Del ut kortene");
private ImageIcon bakside = new ImageIcon("image/bgi14.gif");
private ImageIcon[] forside = new ImageIcon[8];
private HovedVinduet vindu;
private int[] index = new int[8];


public Hukommelse(HovedVinduet vindu){
    super(new GridBagLayout());

    this.vindu = vindu;

    setBackground(Color.GREEN);
    setPreferredSize(new Dimension(1280,720));

    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.RELATIVE;
    c.gridx = 5;
    c.gridy = GridBagConstraints.RELATIVE;

    nyOmgang.addActionListener(this);
    add(nyOmgang, c);
    Kortene();





    forside[0] = new ImageIcon("");
    forside[1] = new ImageIcon(""); 
    forside[2] = new ImageIcon("");
    forside[3] = new ImageIcon("");
    forside[4] = new ImageIcon("");
    forside[5] = new ImageIcon("");
    forside[6] = new ImageIcon("");
    forside[7] = new ImageIcon("");

}

public JLabel Kortene() {

    GridBagConstraints c = new GridBagConstraints();

    for (int i = 0; i< kort.length; i++){
        kort[i] = new JLabel();
        add(kort[i]);
        kort[i].addMouseListener(this);
    }
    return null;
}

public JLabel kortForside(){
    int tilfeldig;
    int tracker;
    tracker = 0;
    tilfeldig = (int)(Math.random()*8);
    while (tracker < kort.length ){
        if (index[tilfeldig]<2){
            kortForside[tracker] = new JLabel(forside[tilfeldig]);
            tracker++;
            index[tilfeldig]++;
        }
    }

    return null;
}

public void bilder(){

    for (int i = 0; i<kort.length; i++){
        kort[i] = new JLabel(bakside);
    }

}


@Override
public void mouseClicked(MouseEvent agr0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("Del ut kortene")){  
        bilder();

    }

}
0

2 Answers 2

1

In bilder, Change:

 kort[i] = new JLabel(bakside);

to:

 kort[i].setIcon(bakside);

This way the actual labels which are on your panel are updated. Before you were creating new labels and not altering the existing ones.

You might need to call a repaint on the container holding the labels.

Sign up to request clarification or add additional context in comments.

1 Comment

@Fadderman Glad to hear. Don't forget you can accept answers if they worked ;)
1

I see you create some labels with icons like so:

kortForside[tracker] = new JLabel(forside[tilfeldig]);

However, there is nowhere in your code that you add these labels to the GUI.

Unlike the other array of labels you create without images:

for (int i = 0; i< kort.length; i++){
    kort[i] = new JLabel();
    add(kort[i]);

1 Comment

Jzd That array is for the mousepressed event, so when i press my mouse on the labels they will change to the other label array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.