Java JTextField.setText()在方法中不能作为数组使用
这里是新的Java程序员,所以我可能有一个基本问题。我正在制作一个JTextFields数组。我想在公共方法的类外使用setText,但它不起作用。但是,我能够在类中使用setText(而不是在方法中)。我不知道为什么。这里有一些代码作为SSCCE来展示我的体验。 导入java.awt.BorderLayout; 导入javax.swing.*Java JTextField.setText()在方法中不能作为数组使用,java,arrays,swing,jtextfield,settext,Java,Arrays,Swing,Jtextfield,Settext,这里是新的Java程序员,所以我可能有一个基本问题。我正在制作一个JTextFields数组。我想在公共方法的类外使用setText,但它不起作用。但是,我能够在类中使用setText(而不是在方法中)。我不知道为什么。这里有一些代码作为SSCCE来展示我的体验。 导入java.awt.BorderLayout; 导入javax.swing.* public class testSetText extends JFrame { private JPanel panel
public class testSetText extends JFrame
{
private JPanel panel;
private JTextField[] arrayField;
private JTextField singleField;
public testSetText()
{
// Specify an action for close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make a panel
panel = new JPanel();
// Make array of JTextField components
JTextField[] arrayField = new JTextField[2];
for (int i = 0; i < 2; i++)
{
arrayField[i] = new JTextField(10);
arrayField[i].setEditable(false);
arrayField[i].setText("<>!");
panel.add(arrayField[i]);
}
// Make just one JTextField component
singleField = new JTextField(10);
singleField.setText("Works here");
panel.add(singleField);
// Add to panel to frame
add(panel);
// Pack the contents of the window and display it
pack();
setVisible(true);
// This will work!
arrayField[0].setText("Array index in class");
// This won't? Why?
setInMethodWithArray();
// Is this a problem with JTextField itself? Let me try a single element
setInMethodWithSingleElement();
// Hmmm so an element in an array of JTextFields can be addressed with setText in a class but not
// in a method in same class with same statement. But a single JTextField can be used in a method
// by that same class. Why do arrays behave so differently?
// EDIT: So I misunderstood, it does not work with a non array as well either!!
}
public void setInMethodWithArray()
{
arrayField[1].setText("This text will never show up");
}
public void setInMethodWithSingleElement()
{
//singleField.setText("Works here as non-array"); <--- before edit
singleField.setText("nevermind, it does not work here either");
}
public static void main(String[] args)
{
new testSetText();
}
}
公共类testSetText扩展JFrame
{
私人JPanel小组;
私有JTextField[]数组字段;
私有JTextField单字段;
公共testSetText()
{
//指定关闭按钮的操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//拼板
panel=新的JPanel();
//生成JTextField组件的数组
JTextField[]arrayField=新的JTextField[2];
对于(int i=0;i<2;i++)
{
arrayField[i]=新的JTextField(10);
arrayField[i].setEditable(false);
arrayField[i].setText(“!”);
新增(arrayField[i]);
}
//只制作一个JTextField组件
singleField=新的JTextField(10);
singleField.setText(“在这里工作”);
panel.add(单字段);
//添加到面板到框架
添加(面板);
//打包窗口内容并显示它
包装();
setVisible(真);
//这会有用的!
arrayField[0].setText(“类中的数组索引”);
//这不会吗?为什么?
setInMethodWithArray();
//这是JTextField本身的问题吗?让我尝试一个元素
setInMethodWithSingleElement();
//Hmmm,因此JTextFields数组中的元素可以在类中使用setText进行寻址,但不能使用setText
//在具有相同语句的同一类中的方法中。但一个方法中可以使用单个JTextField
//为什么数组的行为如此不同?
//编辑:所以我误解了,它也不适用于非数组!!
}
public void setInMethodWithArray()
{
arrayField[1].setText(“此文本永远不会显示”);
}
public void setInMethodWithSingleElement()
{
//singleField.setText(“在这里作为非数组工作”);您应该在类区域中声明arrayField[]
,这样就可以从setInMethodWithArray()
方法访问数组字段
JTextField[] arrayField = new JTextField[2]; //class area
在构造器中初始化它
arrayField[i]= new JTextField(10);
arrayField[i].setEditable(false);
是的,这是工作
arrayField[0].setText("Array index in class");
因为arrayField在构造函数作用域中..并且您正在访问构造函数中的arrayField。所以它可以工作
这不会?为什么?
setInMethodWithArray();
因为方法setInMethodWithArray()
无法访问arrayField[]
,因为它不在方法范围或类范围内。这是因为您在构造函数中声明了它,所以在构造函数代码块执行后arrayField
不存在。它的引用丢失,因为它是局部变量
public void setInMethodWithArray()
{
arrayField[1].setText("This text will never show up");
}
现在set可以访问arratField[],所以现在您的代码可以正常工作了
import java.awt.BorderLayout;
import javax.swing.*;
public class testSetText extends JFrame
{
private JPanel panel;
private JTextField singleField;
// Make array of JTextField components
private JTextField[] arrayField = new JTextField[2];
public testSetText()
{
// Specify an action for close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make a panel
panel = new JPanel();
for (int i = 0; i < 2; i++)
{
arrayField[i] = new JTextField(10);
arrayField[i].setEditable(false);
arrayField[i].setText("<>!");
panel.add(arrayField[i]);
}
// Make just one JTextField component
singleField = new JTextField(10);
singleField.setText("Works here");
panel.add(singleField);
// Add to panel to frame
add(panel);
// Pack the contents of the window and display it
pack();
setVisible(true);
// This will work!
arrayField[0].setText("Array index in class");
// This won't? Why?
setInMethodWithArray();
// Is this a problem with JTextField itself? Let me try a single element
setInMethodWithArray();
// Hmmm so an element in an array of JTextFields can be addressed with setText in a class but not
// in a method in same class with same statement. But a single JTextField can be used in a method
// by that same class. Why do arrays behave so differently?
}
public void setInMethodWithArray()
{
arrayField[1].setText("This text will never show up");
}
public void setInMethodWithSingleElement()
{
singleField.setText("Works here as non-array");
}
public static void main(String[] args)
{
new testSetText();
}
}
导入java.awt.BorderLayout;
导入javax.swing.*;
公共类testSetText扩展了JFrame
{
私人JPanel小组;
私有JTextField单字段;
//生成JTextField组件的数组
私有JTextField[]arrayField=新JTextField[2];
公共testSetText()
{
//指定关闭按钮的操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//拼板
panel=新的JPanel();
对于(int i=0;i<2;i++)
{
arrayField[i]=新的JTextField(10);
arrayField[i].setEditable(false);
arrayField[i].setText(“!”);
新增(arrayField[i]);
}
//只制作一个JTextField组件
singleField=新的JTextField(10);
singleField.setText(“在这里工作”);
panel.add(单字段);
//添加到面板到框架
添加(面板);
//打包窗口内容并显示它
包装();
setVisible(真);
//这会有用的!
arrayField[0].setText(“类中的数组索引”);
//这不会吗?为什么?
setInMethodWithArray();
//这是JTextField本身的问题吗?让我尝试一个元素
setInMethodWithArray();
//Hmmm,因此JTextFields数组中的元素可以在类中使用setText进行寻址,但不能使用setText
//在具有相同语句的同一类中的方法中。但一个方法中可以使用单个JTextField
//为什么数组的行为如此不同?
}
public void setInMethodWithArray()
{
arrayField[1].setText(“此文本永远不会显示”);
}
public void setInMethodWithSingleElement()
{
setText(“在这里作为非数组工作”);
}
公共静态void main(字符串[]args)
{
新的testSetText();
}
}
输出>>
您应该在类区域中声明arrayField[]
,以便可以从setInMethodWithArray()
方法访问数组字段
JTextField[] arrayField = new JTextField[2]; //class area
在构造器中初始化它
arrayField[i]= new JTextField(10);
arrayField[i].setEditable(false);
是的,这是工作
arrayField[0].setText("Array index in class");
因为arrayField在构造函数作用域中..并且您正在访问构造函数中的arrayField。所以它可以工作
这不会?为什么?
setInMethodWithArray();
因为方法setInMethodWithArray()
无法访问arrayField[]
,因为它不在方法范围或类范围内。这是因为您在构造函数中声明了它,所以在构造函数代码块执行后arrayField
不存在。它的引用丢失,因为它是局部变量
public void setInMethodWithArray()
{
arrayField[1].setText("This text will never show up");
}
现在set可以访问arratField[],所以现在您的代码可以正常工作了
import java.awt.BorderLayout;
import javax.swing.*;
public class testSetText extends JFrame
{
private JPanel panel;
private JTextField singleField;
// Make array of JTextField components
private JTextField[] arrayField = new JTextField[2];
public testSetText()
{
// Specify an action for close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make a panel
panel = new JPanel();
for (int i = 0; i < 2; i++)
{
arrayField[i] = new JTextField(10);
arrayField[i].setEditable(false);
arrayField[i].setText("<>!");
panel.add(arrayField[i]);
}
// Make just one JTextField component
singleField = new JTextField(10);
singleField.setText("Works here");
panel.add(singleField);
// Add to panel to frame
add(panel);
// Pack the contents of the window and display it
pack();
setVisible(true);
// This will work!
arrayField[0].setText("Array index in class");
// This won't? Why?
setInMethodWithArray();
// Is this a problem with JTextField itself? Let me try a single element
setInMethodWithArray();
// Hmmm so an element in an array of JTextFields can be addressed with setText in a class but not
// in a method in same class with same statement. But a single JTextField can be used in a method
// by that same class. Why do arrays behave so differently?
}
public void setInMethodWithArray()
{
arrayField[1].setText("This text will never show up");
}
public void setInMethodWithSingleElement()
{
singleField.setText("Works here as non-array");
}
public static void main(String[] args)
{
new testSetText();
}
}
导入java.awt.BorderLayout;
导入javax.swing.*;
公共类testSetText扩展了JFrame
{
私人JPanel小组;
私有JTextField单字段;
//大肆宣扬