初学者 Java Netbeans:如何在 jlabel 中显示 for 循环?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19968550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:56:38  来源:igfitidea点击:

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

javanetbeansfor-loopjlabel

提问by F_Ryan

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:

System.out.println () 产生正确的答案。例如,用户输入 2 和 9,它会说:

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.

但是我试图将文本设置为输出的 jLabel 只显示 9。我知道他的可能非常简单,但是嘿,我是初学者。任何帮助,将不胜感激。

采纳答案by MadProgrammer

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?

您希望每个数字都在一个新行上还是附加到String. 您希望它们像计数器一样单独显示吗?

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...

这将输出类似...

labels in a row

连续标签

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...

这将输出类似...

labels per line

每行标签

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

现在,如果你想要这个,使用 aJTextArea并简单地将每个数字附加到它实际上会更容易......

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...

这将输出类似...

enter image description here

在此处输入图片说明

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

现在,如果你想为它制作动画,你需要做一些不同的事情,要么使用 ajavax.swing.Timer要么SwingWorker

回答by danmcardle

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.

每次您调用 时System.out.println,它都会写入您提供给 STDOUT(也就是您的终端)的字符串。但是,每次调用 时Output.setText,它都会替换您设置的最后一个字符串。

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.

解决方案是创建一个包含所有单个数字串联的字符串,然后将该字符串发送到Output.setText.

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

要在 Java 中的循环中创建字符串,您可以执行以下操作

// 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 StringBuilderwhich 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.

或者,有一个内置于 Java 中的类称为StringBuilder,它在考虑效率的情况下是首选。使用+是非常浪费的,因为它创建了许多必须被垃圾收集的中间对象。

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

回答by Paul Samsotha

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.

每次迭代都继续添加到字符串,然后将最终文本设置为标签。

回答by M. Fire

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);