有些人可以告诉我为什么我无法按照我设置的方式从我的'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的方式是问题,那么是否可以按照我的方式创建它们并且仍然可以从中获取值?
答案 0 :(得分:1)
不应该是:
for(int i = 0 ; i < alist.size(); i++)
{
System.out.println(digiProdDetailBx[i].getText());
}
您正在使用的构造函数设置文本而不是名称,getName()
返回对象的名称,而不是值。
顺便说一句,使用Lists而不是数组通常会更舒服,并且给它们提供比somethingBX,somethingBX2等更有意义的名称。