初学者Java的Netbeans:我如何显示循环jlabel? [英] Beginner Java Netbeans: How do I display for loop in jlabel?

查看:143
本文介绍了初学者Java的Netbeans:我如何显示循环jlabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这个网站,没有什么能真正回答我的问题。这就是我的代码:

$ p $ //声明用户输入变量
int min,max;

//将用户输入分配给min和max
min = Integer.parseInt(Min.getText());
max = Integer.parseInt(Max.getText());

//使用for循环显示值
for(int n = min; n <= max; n ++){
System.out.println(Integer.toString( N));
Output.setText(Integer.toString(n));
}

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

 运行:
2
3
4
5
6
7
8
9

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

解决方案

答案依赖。

你想把每一个数字放在一个新的行上,或者追加到 String 的末尾。你希望他们像个柜台一样单独显示吗?

如果你想按顺序显示数字,那么你可以使用像这样的东西...

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

哪个会输出类似...



或者如果您想要一个新行的每一个数字,你可以使用...

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

哪个会输出类似...

< 现在,如果您想要这个,使用 JTextArea 实际上会更简单,只需要将每个数字附加到它上面...

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

$ / code>

会输出类似...




<现在,如果你想动画,你需要做一些不同的事情,或者使用 javax.swing.Timer SwingWorker


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.

解决方案

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

这篇关于初学者Java的Netbeans:我如何显示循环jlabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆