[英]JTextField.setText() does not work in method as an array
new Java programmer here so I might have a basic question here. 新的Java程序员,所以在这里我可能有一个基本问题。 I am making an array of JTextFields.
我正在制作一个JTextFields数组。 I want to use setText outside of the class in a public method but it does not work.
我想在公共方法的类之外使用setText,但是它不起作用。 However, I am able to use setText in the class (not in a method).
但是,我可以在类中使用setText(而不是在方法中)。 I am not sure why.
我不知道为什么。 Here is some code as a SSCCE to show what I am experiencing.
这是一些作为SSCCE的代码,用于显示我正在经历的事情。 import java.awt.BorderLayout;
导入java.awt.BorderLayout; import javax.swing.*;
导入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();
}
}
you should declare arrayField[]
in class area so array Field is accessible from setInMethodWithArray()
method. 您应该在类区域中声明
arrayField[]
,以便可以通过setInMethodWithArray()
方法访问array Field。
JTextField[] arrayField = new JTextField[2]; //class area
in constructor initial it 在构造函数中初始化
arrayField[i]= new JTextField(10);
arrayField[i].setEditable(false);
arrayField[0].setText("Array index in class");
because arrayField in the constructor scope ..and you are accessing arrayField Within constructor .so it works.. 因为arrayField在构造函数范围内..并且您正在构造函数.so中访问arrayField所以它起作用。
This won't? 不会吗 Why?
为什么?
setInMethodWithArray();
because method setInMethodWithArray()
can't access arrayField[]
because it's not in method scope or in class scope.that's because you have declare it in the constructor so after constructor code block execute arrayField
doesn't exist .it's reference lost because it's local variable
.. 因为
setInMethodWithArray()
方法无法访问arrayField[]
因为它不在方法范围内或类范围内。那是因为您在构造函数中声明了它,所以构造函数代码块执行arrayField
就不存在了。它的引用丢失了,因为它是local variable
..
public void setInMethodWithArray() { arrayField[1].setText("This text will never show up"); }
now set can access arratField[] so now your code will work 现在设置可以访问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(); } }
output>> 输出>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.