简体   繁体   中英

How do I display the numbers in a for loop in a jLabel?

    int intStartingNumber = Integer.parseInt(this.txtInputStartingNumber.getText());
    int intEndingNumber = Integer.parseInt(this.txtInputEndingNumber.getText());

    for (int x = intStartingNumber; x <= intEndingNumber; x = x + 1) {
        System.out.print(x + " ");
        this.lblOutputNumbers.setText(x + " ");
    }

Hi this is my code how come it won't set my jLabel as all the numbers in the for loop but when i print it out, all the numbers show up. In the jLabel only whatever is inputted as the EndingNumber is printed out in the jLabel.

RE

In the jLabel only whatever is inputted as the EndingNumber is printed out in the jLabel.

for (int x = intStartingNumber; x <= intEndingNumber; x++) {
    System.out.print(x + " ");
    this.lblOutputNumbers.setText(lblOutputNumbers.getText() + " " + x);
}

The trick here is JLabel does not have an appendText function, but you can call the JLabel.getText() function inside of its setText function to append text to the label.

This answer stems from Append text in JLabel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM