阅读ArrayList的JTextField的 [英] reading ArrayList JTextFields
问题描述
我的程序通过提示用户开始他们希望有多少文本字段有
公共类textEvent1实现的ActionListener {//动作监听器有多少群众?
公共无效的actionPerformed(ActionEvent的五){
N =(INT)(Double.parseDouble(massNumField.getText()));
接下来我创建了一个for循环来创建标签和文本框(这是我创建的列表,因为我不知道有多少会出现)。有一对夫妇名单,但我会给只是一个例子。
ArrayList的群众=新的ArrayList(); 的for(int i = 1; I< = N;我++){//添加文本事件2文本屏幕
massLabel =新的JLabel(多少群众做弥撒+ I +有吗?);
massField =新的JTextField,(5);
masses.add(massField);
现在我的问题似乎要来当我试图群众列表的元素赋值给一个变量,像这样。
的for(int i = 1; I< = N;我++){
质量= Double.parseDouble(((的JTextComponent)masses.get(ⅰ))的getText());
我已经尝试了几件事情...质量= masses.get(我)......质量= masses.get(I).getText())等等等等。我要么不断收到错误,如空指针异常或事物说我不能parseDouble对象。
有错误的arrise这个例子是如下
异常螺纹AWT-EventQueue的-0java.lang.ArrayIndexOutOfBoundsException:3> = 3
在java.util.Vector.elementAt(来源不明)
在acmx.export.java.util.ArrayList.get(ArrayList.java:54)
在轨道$ textEvent2.actionPerformed(Orbit.java:151)
行151
质量= Double.parseDouble(((JTextComponent的)masses.get(I))的getText());
创建当 JTextField的
,你做的:
的for(int i = 1; I< = N;我++){
...
注意列表
编号从0开始,所以当你获取一个类似的循环的项目,使用 I
作为索引,您试图访问一个过去的最后一个项目。更改阅读循环指数为:
的for(int i = 0; I< N;我++){
...
或者你可以使用增强的for循环,除非你需要用一个古老的Java版本:
的(对象massField:群众){
质量= Double.parseDouble(((的JTextComponent)massField).getText());
...
(那么你真的应该使用泛型过,如果Java版本支持它们)。
My program starts by prompting the user how many text fields they would like to have
public class textEvent1 implements ActionListener { //action listener for "how many masses?"
public void actionPerformed (ActionEvent e) {
n = (int)(Double.parseDouble(massNumField.getText()));
next I create a for loop to create labels and textfields (which I have created lists for because I dont know how many there will be). There are a couple of lists but I will give an example of just one.
ArrayList masses = new ArrayList();
for(int i=1; i<=n; i++) { //adds text event 2 text to the screen
massLabel = new JLabel("How much mass does Mass " +i+ " have? ");
massField = new JTextField(5);
masses.add(massField);
Now my problem appears to come when I try to assign an element of the masses list to a variable like so.
for(int i=1; i<=n; i++) {
mass = Double.parseDouble(((JTextComponent) masses.get(i)).getText());
I have tried a couple of things...mass = masses.get(i).....mass = masses.get(i).getText()) and so on and so on. I either keep getting errors such as Null pointer exceptions or things saying I cant parseDouble an Object.
There errors that arrise for this example are as below
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 >= 3
at java.util.Vector.elementAt(Unknown Source)
at acmx.export.java.util.ArrayList.get(ArrayList.java:54)
at Orbit$textEvent2.actionPerformed(Orbit.java:151)
line 151 is
mass = Double.parseDouble(((JTextComponent) masses.get(i)).getText());
When creating the JTextFields
, you do:
for(int i=1; i<=n; i++) {
...
Note that List
indices start at 0, so when you retrieve the items with a similar loop, using i
as the index, you are trying to access one past the last item. Change the reading loop indices to:
for (int i = 0; i < n; i++) {
...
Or you could use an enhance for loop, unless you need to use an ancient java version:
for (Object massField : masses) {
mass = Double.parseDouble(((JTextComponent) massField).getText());
...
(Then you should really use generics too, if the java version supports them).
这篇关于阅读ArrayList的JTextField的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!