gpt4 book ai didi

java - 愚蠢的 For 循环实现

转载 作者:行者123 更新时间:2023-12-02 04:41:31 27 4
gpt4 key购买 nike

使用 java.awt 和 javax.swing 我一直在制作一个小型 GUI,它将在 JPanel 中显示多个条目。在成功显示带有默认文本的单个条目后,我开始尝试实现一个 for 循环以从几个数组创建多个条目,并且在加载标记每个组件的文本字符串时遇到困难。添加 for 循环和数组后,我现在只在一个没有数据的小空间周围得到一个黑色边框。

这是我专门使用的代码:

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

显然我对数组做了一些错误的操作,或者设置了组件,但是经过多次尝试和谷歌搜索后我丢失了一些东西。非常感谢任何帮助。

最佳答案

我实际上没有尝试运行你的程序,但我确实注意到你的循环:

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 - 愚蠢的 For 循环实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30130560/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com