简体   繁体   English

为什么不能调用已创建对象的公共方法?

[英]Why can I not call a public method of a created object?

When I am trying to run this Java program to make a JTextField: 当我尝试运行此Java程序以创建JTextField时:

import java.awt.*; 
import javax.swing.*; 

public class TextField1 extends JFrame{
  private final int WIDTH = 320; 
  private final int HEIGHT = 250; 
  private FlowLayout flow = new FlowLayout(); 
  private JTextField myOutput = new JTextField();

  public TextField1() {
    super("My TextField Example");
    setSize(WIDTH, HEIGHT); 
    setLayout(flow);
    JTextField myOutput = new JTextField(20);
    add(myOutput);
    setVisible(true); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  }

  public void createGUI(){
    myOutput.setText("I am a text field showing output!");
    myOutput.setEditable(false);
  }

  public static void main(String[]args) {
 TextField1 myTextField = new TextField1(); 
 myTextField.createGUI();
 System.out.println(myTextField.myOutput.getText());
  }
}

It seems as if it does not register the createGUI() method. 似乎它没有注册createGUI()方法。 Can you explain why this is? 你能解释为什么吗?

Inside the constructor, you are creating a new variable called myOutput , which is not the same as the instance variable declared above. 在构造函数内部,您正在创建一个名为myOutput的新变量,该变量与上面声明的实例变量不同。 JTextField myOutput should be myOutput JTextField myOutput应该是myOutput

EDIT: 编辑:

You declare a method-local variable myOutput and add it to your frame. 您声明一个方法局部变量myOutput并将其添加到框架中。 createGUI() uses the instance variable declared at the top of the class, therefore it does not affect the TextField in the frame. createGUI()使用在类顶部声明的实例变量,因此它不会影响框架中的TextField

I am just extending the answer which was provided by blueygh2. 我只是在扩展blueygh2提供的答案。 The main problem is that : 主要的问题是:
you have not given the size of the JTextField in the class which will be 0 by default 您没有在类中给定JTextField的大小,默认情况下将为0

private JTextField myOutput = new JTextField();//no size specified 

Also in constructor you are Declaring another JTextField with same name that will be eligible for Garbage collection after Object Creation : 同样在构造函数中,您正在声明另一个具有相同名称的JTextField,该名称将在对象创建后可用于垃圾回收:

JTextField myOutput = new JTextField(20);

Now there are 2 solutions : 现在有2个解决方案:
1>Specify the size of JTextField in class definition like : 1>在类定义中指定JTextField的大小,例如:

JTextField myOutput = new JTextField(20); // new object created for JTextField

2> Do not create a new JTextField Object in Constructor like : 2>不要在构造器中创建新的JTextField对象,例如:

myOutput = new JTextField(20);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能在不使用main方法的引用开头的情况下调用继承的public方法? - Why can't I call an inherited public method without prefacing it with a reference from main method? 为什么我不能从超类对象调用子类方法 - why can't i call a subclass method from a superclass object 为什么我不能使用 class 的实例调用 java class 的公共方法? - Why can't I call a public method of a java class using an instance of the class? 我可以覆盖隐藏(但公共)方法并调用其超级方法吗? - Can I override a hidden (but public) method and call its super method? 为什么我可以用公共方法覆盖受保护的方法? - Why can I override a protected method with public method? 我调用没有 object 的方法为什么它会运行? - I call method without object why is it run? 在另一个函数中创建子类的实例时,为什么不能从子类类型的 Abstact 类调用方法(公共或静态)? - Why can't I call a method (public or static) from an Abstact class of type subclass when creating an instance of the subclass in another function? 为什么用通配符创建的比较器的比较方法不能接受对象? - Why compare method of Comparator created with wildcard can't accept object? 为什么不能调用接口方法? - Why can I not call the interface method? 为什么我们不应该从另一个公共调用公共方法? - Why we should not call public method from another public?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM