简体   繁体   English

从另一个类中的一个类调用变量

[英]Calling a variable from one class in another

I am writing the code for a primitive address book for one of my classes, and it is required that we use a class for the address book itself, and then another class for putting in new entries. 我正在为我的一个类编写原始地址簿的代码,并且要求我们对地址簿本身使用一个类,然后对另一个类使用新的条目。 I understand this, but when I call the class method for entering new contacts, I want to change the GUI. 我理解这一点,但是当我调用用于输入新联系人的类方法时,我想更改GUI。 I am using JFrame, and so for my GUI base i am using the contentPane . 我正在使用JFrame,因此对于我的GUI基础,我正在使用contentPane In my first class I created my contentPane with a basic GUI: 在第一堂课中,我使用基本的GUI创建了contentPane

public class Address extends JFrame implements ActionListener
{
    Container contentPane;

    public Address()
    {
        super();
        contentPane = getContentPane();
        contentPane.setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Address Book");
        setSize(775,775);
        setLocation(0,0);
        setBackground(Color.BLUE);
    }
}

And now in my second class, the input one, I want to clear the contentPane with contentPane.removeAll() . 现在在第二个类(输入类)中,我想使用contentPane.removeAll()清除contentPane

class Entries
{
    public void newContact()
    {
        contentPane.removeAll();
    }
}

Unfortunately, nothing in my class Entries is recognizing contentPane and other variables i am attempting to modify. 不幸的是,我的班级条目中没有任何东西可以识别contentPane和我尝试修改的其他变量。 Is there a special way to name the contentPane and other variables in class Address so that they are usable in Entries, or do i need to recreate the variable? 有没有一种特殊的方法可以在Address类中命名contentPane和其他变量,以便它们在Entries中可用,还是我需要重新创建变量?

When you create an object of Entries you need to give it a reference to the Address object so you can use its variables. 创建Entries对象时,需要为其提供对Address对象的引用,以便可以使用其变量。 I'm not sure how your program layout is set up, but if Entries is created within Address you'd call Entries entry = new Entry(this); 我不确定您的程序布局是如何设置的,但是如果在“ Address创建了“ Entries则会调用“ Entries entry = new Entry(this); or if it is from a parent to both classes you'd need to set up variables... 或者是从父类到两个类,您都需要设置变量...

Address address = new Address();
Entries entry = new Entry(address);

Code for how you'd access it in Entries: 您如何在条目中访问代码:

class Entries
{
    private Address reference;

    public Entries(Address reference) {
        this.reference = reference;
    }

    public void newContact()
    {
        reference.contentPane.removeAll();
    }
}

您需要将contentPane的引用作为构造函数arg传递给Entries,或将contentPane属性(getter,setter)添加到Entries。

You need a reference to access stuff, either you duplicate the variable and give the contentPane in the Entries constructor : 您需要引用来访问内容,或者复制变量并在Entries构造函数中提供contentPane:

private Container contentPane;

public Entries(Container contentPane) {
    this.contentPane = contentPane;
}

Or you create a getter on your contentPane in the Address class and pass the contentPane. 或者,您可以在Address类的contentPane上创建一个getter并传递contentPane。

Both solutions are quite bad in my opinion however, maybe you should consider extending a JPanel for your entries class, and add the Entries to your main contentPane, then you will be able to do whatever UI stuff you want in the Entries class. 在我看来,这两种解决方案都非常糟糕,也许您应该考虑为您的entrys类扩展一个JPanel,并将Entries添加到您的主contentPane中,然后您就可以在Entries类中执行您想要的任何UI东西。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM