【问题标题】:JTextField.setText() does not work in method as an arrayJTextField.setText() 在方法中不能作为数组工作
【发布时间】:2014-11-01 03:42:53
【问题描述】:

这里是新的 Java 程序员,所以我在这里可能有一个基本问题。我正在制作一个 JTextField 数组。我想在公共方法中在类之外使用 setText,但它不起作用。但是,我可以在类中使用 setText(而不是在方法中)。我不确定为什么。这是一些作为 SSCCE 的代码,以显示我正在经历的事情。 导入 java.awt.BorderLayout; 导入 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();
    }
}

【问题讨论】:

    标签: java arrays swing jtextfield settext


    【解决方案1】:

    您应该在类区域中声明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");
    }
    

    现在 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 没有全局变量。另外,我记得在我编写的另一个程序中使用了 setText,但它在类的构造函数之外。相反,它位于 ActionPerformed 下的 ActionLister 中。这工作得很好,所以如果在类内的方法中调用它,我很难理解差异。
    • @xxgiuzeppexx 现在检查它。我只是更新..如果你收到错误通知我
    • @xxgiuzeppexx 你可以从任何地方调用 settext 但文本字段应该在范围内。
    • 谢谢!我现在知道了。现在我的实际程序运行良好。
    猜你喜欢
    • 1970-01-01
    • 2015-03-06
    • 2011-12-14
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多