gpt4 book ai didi

java - 从动态创建的 JTextField 获取值

转载 作者:行者123 更新时间:2023-12-01 13:07:31 25 4
gpt4 key购买 nike

有人可以告诉我为什么我无法按照我设置的方式从“JTextFields”获取文本值吗?

我根据歌曲数量创建了“JTextFields”,但如果我尝试从这些字段中获取文本,结果总是空白?

JTextField[] digiProdDetailBx = new JTextField[p.getAlbum().getSongList().size()];
JTextField[] digiProdDetailBx2 = new JTextField[p.getAlbum().getSongList().size()];
JLabel[] digiProdDetailLb = new JLabel[p.getAlbum().getSongList().size()];


for (int i = 0; i < p.getAlbum().getSongList().size(); i++) {

int num = i +1;

if (i == 0) {
gc.gridx = 0;
gc.gridy = 0;
gc.anchor = GridBagConstraints.WEST;
songs.add(songNumber, gc);
}

gc.gridx = 0;
gc.gridy = i + 1;
gc.gridwidth = 1;
gc.gridheight = 1;
gc.weighty = 0.0;
gc.weightx = 2.0;
gc.anchor = GridBagConstraints.WEST;
digiProdDetailLb[i] = new JLabel(" " + num);
digiProdDetailLb[i].setFont(font);
songs.add(digiProdDetailLb[i], gc);

if (i == 0) {
gc.gridx = 1;
gc.gridy = 0;
songs.add(songName, gc);
}
gc.gridx = 1;
gc.gridy = i + 1;
gc.gridwidth = 1;
gc.gridheight = 1;
gc.weighty = 0.0;
gc.weightx = 2.0;
gc.gridwidth = 2;
digiProdDetailBx[i] = new JTextField(p.getAlbum().getSongList().get(i).getSong_name());
digiProdDetailBx[i].setPreferredSize(new Dimension(250, 20));
songs.add(digiProdDetailBx[i], gc);

if (i == 0) {
gc.gridx = 3;
gc.gridy = 0;
songs.add(songLength, gc);
}
gc.gridx = 3;
gc.gridy = i + 1;
gc.gridwidth = 1;
gc.gridheight = 1;
gc.weighty = 0.0;
gc.weightx = 2.0;
gc.gridwidth = 2;
digiProdDetailBx2[i] = new JTextField(p.getAlbum().getSongList().get(i).getSong_length());
digiProdDetailBx2[i].setPreferredSize(new Dimension(50, 20));
songs.add(digiProdDetailBx2[i], gc);

}
;

this.add(songs, BorderLayout.EAST);


for(int i = 0 ; i < p.getAlbum().getSongList().size(); i++)
{

Song s = new Song(p.getProd_id(),p.getAlbum().getSongList().get(i).getProd_id(),
digiProdDetailBx[i].getText(), digiProdDetailBx2[i].getText());
alist.add(s);
}


for(int i = 0 ; i < alist.size(); i++)
{
System.out.println(digiProdDetailBx[i].getName());
}

我假设我创建 JTextFields 的方式有问题,那么是否可以按照我的方式创建它们并且仍然能够从中获取值?

最佳答案

不应该是:

for(int i = 0 ; i < alist.size(); i++)
{
System.out.println(digiProdDetailBx[i].getText());
}

您使用的构造函数设置文本而不是名称,并且 getName() 返回对象的名称,而不是值。

顺便说一句,使用列表通常比使用数组更舒服,并为它们提供比somethingBX、somethingBX2等更有意义的名称。

关于java - 从动态创建的 JTextField 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23155628/

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