愚蠢的 For 循环实现
Stupid For loop implementation
使用 java.awt 和 javax.swing 我一直在制作一个小型 GUI,它将在 JPanel 中显示多个条目。在成功显示带有默认文本的单个条目后,我开始尝试实现一个 for 循环以从少数数组创建多个条目,并且在加载标记每个组件的文本字符串时遇到困难。添加 for 循环和数组后,我现在只在一个没有数据的小 space 周围得到一个黑色边框。
这是我专门使用的代码:
package myInterfaceComponents;
import java.awt.*;
public class ListOfEntries extends JComponent {
//instance variables
//worked until arrays added
String[] telephones = {"5551234567", "5557654321", "5555671234"};
String[] names = {"Emily", "Billy Bob", "Wiley Coyote"};
String[] periods = {"2p - 3p", "1a - 5a", "4:30p - 11p"};
private JLabel telephone = new JLabel();
private JTextField name = new JTextField();
private JButton period = new Button();
private StatusCombo serviceCycle = new StatusCombo();
private AreaCombo area = new AreaCombo();
//constructors
public ListOfEntries() {
setLayout(new GridLayout(2, 3));
for (int x = 0; x == 2; x++) { //worked until added
telephone.setText(telephones[x]); //worked until added
name.setText(names[x]); //worked until added
period.setText(periods[x]); //worked until added
add(telephone);
add(name);
add(area);
add(period);
add(serviceCycle); } //worked until add: "}"
Border line = BorderFactory.createLineBorder(Color.BLACK);
Border titled = BorderFactory.createTitledBorder(line, "Day: DD MMM YYYY");
setBorder(visible);
setVisible(true); }
}
显然我在数组或组件设置上做错了,但在多次尝试和 google 搜索之后我遗漏了一些东西。非常感谢任何帮助。
我还没有真正尝试过 运行你的程序,但我确实注意到你的循环:
for (int x = 0; x == 2; x++) { //worked until added
telephone.setText(telephones[x]); //worked until added
name.setText(names[x]); //worked until added
period.setText(periods[x]); //worked until added
add(telephone);
add(name);
add(area);
add(period);
add(serviceCycle); }
在这个循环中,您将计数器变量 "x" 初始化为 0,但是在下一个语句中您有:x == 2,这 运行 只有当 x 是等于 2,并且由于 x 被初始化为 0,所以这个循环永远不会 运行。尝试将语句 x == 2 更改为 x < 2,看看是否有帮助。
使用 java.awt 和 javax.swing 我一直在制作一个小型 GUI,它将在 JPanel 中显示多个条目。在成功显示带有默认文本的单个条目后,我开始尝试实现一个 for 循环以从少数数组创建多个条目,并且在加载标记每个组件的文本字符串时遇到困难。添加 for 循环和数组后,我现在只在一个没有数据的小 space 周围得到一个黑色边框。
这是我专门使用的代码:
package myInterfaceComponents;
import java.awt.*;
public class ListOfEntries extends JComponent {
//instance variables
//worked until arrays added
String[] telephones = {"5551234567", "5557654321", "5555671234"};
String[] names = {"Emily", "Billy Bob", "Wiley Coyote"};
String[] periods = {"2p - 3p", "1a - 5a", "4:30p - 11p"};
private JLabel telephone = new JLabel();
private JTextField name = new JTextField();
private JButton period = new Button();
private StatusCombo serviceCycle = new StatusCombo();
private AreaCombo area = new AreaCombo();
//constructors
public ListOfEntries() {
setLayout(new GridLayout(2, 3));
for (int x = 0; x == 2; x++) { //worked until added
telephone.setText(telephones[x]); //worked until added
name.setText(names[x]); //worked until added
period.setText(periods[x]); //worked until added
add(telephone);
add(name);
add(area);
add(period);
add(serviceCycle); } //worked until add: "}"
Border line = BorderFactory.createLineBorder(Color.BLACK);
Border titled = BorderFactory.createTitledBorder(line, "Day: DD MMM YYYY");
setBorder(visible);
setVisible(true); }
}
显然我在数组或组件设置上做错了,但在多次尝试和 google 搜索之后我遗漏了一些东西。非常感谢任何帮助。
我还没有真正尝试过 运行你的程序,但我确实注意到你的循环:
for (int x = 0; x == 2; x++) { //worked until added
telephone.setText(telephones[x]); //worked until added
name.setText(names[x]); //worked until added
period.setText(periods[x]); //worked until added
add(telephone);
add(name);
add(area);
add(period);
add(serviceCycle); }
在这个循环中,您将计数器变量 "x" 初始化为 0,但是在下一个语句中您有:x == 2,这 运行 只有当 x 是等于 2,并且由于 x 被初始化为 0,所以这个循环永远不会 运行。尝试将语句 x == 2 更改为 x < 2,看看是否有帮助。