新的Java程序员在这里我可能有一个基本的问题。我正在制作一个JTextField数组。我想在公共方法中使用类外的setText,但它不起作用。但是,我能够在类中使用setText(而不是在方法中)。我不知道为什么。这里有一些代码作为SSCCE来显示我正在经历的事情。 import java.awt.BorderLayout; import javax.swing。*;
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();
}
}
答案 0 :(得分:2)
您应该在类区域中声明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
之后不存在。因为它的引用丢失了local variable
..
public void setInMethodWithArray()
{
arrayField[1].setText("This text will never show up");
}
现在设置可以访问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();
}
}
输出&GT;&GT;