Beginner Java Netbeans: How do I display for loop in jlabel?

那年仲夏 提交于 2019-12-11 18:14:52

问题


I have looked on this site and nothing has really answered my question. This is what my code looks like:

    // declare user input variables
    int min, max;

    //assign user input to min and max
    min = Integer.parseInt(Min.getText ());
    max = Integer.parseInt(Max.getText ());

    //use for loop to display the values
    for (int n = min; n<= max; n++){
        System.out.println(Integer.toString(n));
        Output.setText(Integer.toString(n));
    }

and the System.out.println () yields the correct answer. for instance, the user inputs 2 and 9, it will say:

run:
2
3
4
5
6
7
8
9

but the jLabel I'm trying to set the text to, Output, only displays 9. I know his might be stupidly simple but hey, I'm a beginner. Any help would be appreciated.


回答1:


The answer "depends".

Do you want each number on a new line or appended to the end of the String. Do you want them displayed individually like a counter?

If you want the numbers display in sequence, then you could use something like this...

JLabel label = new JLabel();
int min = 0;
int max = 10;
StringBuilder sb = new StringBuilder(128);
for (int n = min; n <= max; n++) {
    sb.append(n);
}
label.setText(sb.toString());

Which will output something like...

Or if you wanted each number of a new line, you could use...

JLabel label = new JLabel();
int min = 0;
int max = 10;
StringBuilder sb = new StringBuilder(128);
sb.append("<html>");
for (int n = min; n <= max; n++) {
    System.out.println(Integer.toString(n));
    sb.append(n).append("<br>");
}
sb.append("</html>");
label.setText(sb.toString());

Which will output something like...

Now, if you want this, it would actually be easier to use a JTextArea and simply append each number to it...

JTextArea editor = new JTextArea(10, 10);
editor.setEditable(false);
int min = 0;
int max = 10;
StringBuilder sb = new StringBuilder(128);
for (int n = min; n <= max; n++) {
    editor.append(Integer.toString(n) + "\n");
}

Which will output something like...

Now, if you want to animate it, you're going to need to do things different, either using a javax.swing.Timer or SwingWorker




回答2:


Every time you call System.out.println, it is writing the string you give it to STDOUT (aka your terminal). However, every time you call Output.setText, it is replacing the last string you set it to.

The solution would be to create a single string containing the concatenation of all the individual numbers and then send that string into Output.setText.

To create a string in a loop in Java you could do

// Create string "0\n1\n2\n3..."
String s = "";
for (int i=0; i<10; i++) {
    s += i + "\n";
}

Alternatively, there is a class built into Java called StringBuilder which is preferred in situations where efficiency is a concern. Using + is very wasteful because it creates lots of intermediate objects that must be garbage collected.

StringBuilder s = new StringBuilder();
for (int i=0; i<10; i++) {
    s.append(i);
    s.append("\n");
}



回答3:


String nums = "";

for (..........) {
    nums += yourResult + " ";  // this will give a space separating your output
}

label.setText(nums);

Keep adding to the String every iteration, then just set the final text to the label.




回答4:


This is what I'd normally do:

String vals = "";
int min = Integer.parseInt(Min.getText ());
int max = Integer.parseInt(Max.getText ());

for (int i = min; i <= max; i++)
    vals = vals + Integer.toString(i) + " ";

output.setText(vals);


来源:https://stackoverflow.com/questions/19968550/beginner-java-netbeans-how-do-i-display-for-loop-in-jlabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!