Jlabel does not receive new text

Asked

Viewed 28 times

1

That code was meant to be when he clicked the button the same became invisible and the lblVira becomes a number between 0 and 10, gives no error message and compiles only that the lblVira not "grab" the new text (I’m new to java).

private void btnstartActionPerformed(java.awt.event.ActionEvent evt) {                                         
    btnstart.setVisible(false);
    Random random = new Random();
        int array[] = new int[1]; 

        for (int i=1; i<array.length; i++) {
             array[i] = random.nextInt(10); 
            lblVira.setText(Integer.toString(array[i]));
        }    }
  • You created a 1 position vector, then made a for that runs starting at 1 and going as 1 < 1, ie never. The for shouldn’t start at 0? And why a 1 position array? Only int would not be enough?

2 answers

0


private void btnstartActionPerformed(java.awt.event.ActionEvent evt) {                                         
    btnstart.setVisible(false);
    Random random = new Random();
        int array[] = new int[1]; 

        for (int i=1; i<array.length; i++) {
             array[i] = random.nextInt(10); 
            lblVira.setText(Integer.toString(array[i]));
        }    }

Look at this one for. It rotates while i<1. It happens that once it starts with i=1, then the condition of for already starts fake and he won’t do anything.

I think you wanted i=0 instead of i=1.

0

I think you want something like this:

private void btnstartActionPerformed(java.awt.event.ActionEvent evt) {
    btnstart.setVisible(false);
    Random random = new Random();
    int numeroAleatorio = random.nextInt(10); 
    lblVira.setText(Integer.toString(numeroAleatorio));
}

No need to create a ArrayList

  • actually that’s what I really wanted, but as I’m starting to program now array was the first thing that came to my mind anyway, thank you

  • I suggest you start with javascript, it is a simple and very used language

Browser other questions tagged

You are not signed in. Login or sign up in order to post.