【问题标题】:Access a new frames methods访问新的框架方法
【发布时间】:2010-01-04 14:10:09
【问题描述】:

此方法在我的应用程序中显示一个新窗口:

public void ShowNewCustomerView() {
    if (NewCustomer == null) NewCustomer = new NewCustomerView(this);
    NewCustomer.setVisible(true);
}

NewCustomerView 类有这个方法:

public void ClearFields() {
    txtAddress.setText("");
    txtCity.setText("");
    txtCompanyName.setText("");
    txtCustomerNumber.setText("");
    txtOrganisationNumber.setText("");
    txtPhoneNumber1.setText("");
    txtPhoneNumber2.setText("");
    txtPostalCode.setText("");
    txtReferenceName.setText("");
}

如何在该行之前运行该方法:

NewCustomer.setVisible(true);

添加这个:

NewCustomer.ClearFields();

...不起作用...这是为什么?

这是我得到的错误: 找不到符号(方法 ClearFields()) 类:javax.Swing.JFrame

但是我创建了一个扩展 JFrame 的 NewCustomerView 的新实例?对吧?

【问题讨论】:

  • 怎么不工作了?这些字段中是否有文本内容?
  • 更新帖子,看看上面的原因...
  • 很抱歉与编辑赛跑,本以为与您同时进行一个小编辑 - 所以显然不检查并发编辑...

标签: java swing class


【解决方案1】:

听起来您将新框架定义为:

JFrame frame = new NewCustomerView();

相反,您应该这样做:

NewCustomerView frame = NewCustomerView();

【讨论】:

    【解决方案2】:

    从你的 cmets 我认为你声明 NewCustomer 是这样的:

    JFrame NewCustomer; 
    

    如果是这样,请尝试声明:

    NewCustomerView NewCustomer;
    

    public void ShowNewCustomerView() {
        if (NewCustomer == null) NewCustomer = new NewCustomerView(this);
        ((NewCustomerView)NewCustomer).ClearFields();
        NewCustomer.setVisible(true);
    }
    

    【讨论】:

      【解决方案3】:

      以下错误消息是对正在发生的事情的提示:

      Cannot find symbol (method ClearFields()) Class: javax.Swing.JFrame
      

      消息的意思是在JFrame 对象上调用了ClearFields 方法。这是有道理的,因为JFrame 对象上没有ClearFields 方法。

      这似乎表明NewCustomerView 对象被声明为JFrame 而不是NewCustomerView

      我猜想声明NewCustomer 对象的那行是这样写的:

      JFrame NewCustomer = new NewCustomerView();
      

      所做的是将新的NewCustomerView 对象作为JFrame 处理——因此,当尝试调用ClearFields 方法时,我们会尝试调用JFrame.ClearFields 方法,而这不会存在。

      请尝试以下操作,以便将声明的对象作为NewCustomerView 对象处理:

      NewCustomerView NewCustomer = new NewCustomerView();
      

      另外需要注意的是,在 Java 中,变量名用小写字母书写,每个单词边界后跟大写字母,例如,在上面的示例中,NewCustomer 将写作newCustomer

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-24
        • 2017-02-18
        • 2017-04-02
        • 1970-01-01
        • 2020-05-30
        • 1970-01-01
        相关资源
        最近更新 更多