我的问题是:如何从第二个类引用我的主类的实例以获取我的主类的私有字段?
我有4个班级:
这是我的ValuablesRegister类,它在一个字段中初始化一个ValuablesList实例,并在main方法中创建一个Window实例:
import java.util.*;
public class ValuablesRegister {
private ValuablesList currentValuablesList = new ValuablesList("Register");
private void runApplication() {
currentValuablesList.addValuables(some values here)
currentValuablesList.returnValues();
new Window();
}
public ValuablesList getCurrentList() {
return currentValuablesList;
}
public static void main(String[] args) {
new ValuablesRegister().runApplication();
}
}
这是我的ValuablesList类,包含Valuables对象的实际arraylist:
import java.util.ArrayList;
public class ValuablesList {
private String listName;
private ArrayList<Valuables> valuablesList = new ArrayList<Valuables>();
public ValuablesList(String name) {
this.listName = name;
}
public void addValuable(Valuables v) {
valuablesList.add(v);
}
public void returnValues() {
for(Valuables v : valuablesList) {
System.out.println(v.toString());
}
}
public ArrayList<Valuables> getList() {
return valuablesList;
}
}
最后我有我的Window类,它有actionlisteners的内部类。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Window extends JFrame {
private JTextArea contentList = new JTextArea();
public Window() {
super("Valuables Register");
setLayout(new BorderLayout());
JPanel north = new JPanel();
add(north, BorderLayout.NORTH);
JLabel titel = new JLabel("Valuables Register");
north.add(titel);
contentList.setEditable(false);
JScrollPane scrollPane = new JScrollPane(contentList);
add(scrollPane, BorderLayout.CENTER);
JPanel south = new JPanel();
add(south, BorderLayout.SOUTH);
JButton show = new JButton("Show");
show.addActionListener(new showListener());
south.add(show);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1000, 800);
setVisible(true);
}
class showListener implements ActionListener {
public void actionPerformed(ActionEvent ave) {
ArrayList<Valuables> list = SOME FUNCTION TO GET THE REFERENCE TO THE ARRAYLIST;
valuablesAppend(list);
}
}
private void valuablesAppend(ArrayList<Valuables> valuablesList) {
for (Valuables v : valuablesList) {
contentList.append(v.toString());
contentList.append("\n");
}
}
}
所以我想要发生的是以下内容:
问题是我不知道如何获得包含这些对象的arraylist的引用。我尝试在我的ValuablesRegister-class(getCurrentList)中创建一个get-method,但我不知道如何调用这个方法,因为我不知道如何引用我的主类的实例。或者应该以另一种方式完成?
我在想,一个可能的解决方案是将ValuablesRegister的实例传递给Window的构造函数,并将其保存在Window的私有字段的字段中,但我不确定如何将其作为Window(this) ;似乎不起作用。
答案 0 :(得分:0)
没关系,我通过在Window类的构造函数中传递主类的实例来修复它。我就这样做了:
通过添加字段并在构造函数中设置它来更改Window类:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Window extends JFrame {
private JTextArea contentList = new JTextArea();
private ValuablesRegister main;
public Window(ValuablesRegister mainClass) {
super("Valuables Register");
this.main = mainClass;
然后将其传递给&#34;这个&#34;在ValuablesRegister中:
private void runApplication() {
currentValuablesList.addValuable(some values here)
currentValuablesList.returnValues();
new Window(ValuablesRegister.this);
希望这有助于某人!
答案 1 :(得分:0)
嗯,实际上解决问题非常简单。您只需要为Window
类提供对ArrayList
:
好的,这是类main
的{{1}}方法。首先,我只是初始化Application
,就像你一样,但使用Window
的实例而不是继承它。
JFrame
public static void main(final String[] arguments) {
final Application instance = new Application();
final Valuable valuable00 = new Valuable() {
@Override
public String toString() {
return "I'm very valuable.";
}
};
final Valuable valuable01 = new Valuable() {
@Override
public String toString() {
return "I'm not very valuable.";
}
};
final ValuablesList register = new ValuablesList("Register", 2);
final JFrame frame = new JFrame("StackOverflow");
register.getList().add(valuable00);
register.getList().add(valuable01);
SwingUtilities.invokeLater(() -> {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(1000, 800);
frame.setLocationRelativeTo(null);
if (instance.initialiseUserInterface(frame, register)) {
System.out.println("Successfully initialised the user interface.");
}
frame.setVisible(true);
});
}
方法有两个参数:对initialiseUserInterface
的引用,以及JFrame
方法对ValuablesList
对象的引用。
main
在初始化程序的其余部分后,只需使用引用的 public boolean initialiseUserInterface(final JFrame reference, final ValuablesList register) {
final JPanel north = new JPanel(), south = new JPanel();
final JLabel title = new JLabel("Register");
final JButton button = new JButton("Show");
final JTextArea textArea = new JTextArea();
final JScrollPane scrollPane = new JScrollPane(textArea);
if (SwingUtilities.isEventDispatchThread()) {
north.add(title);
textArea.setEditable(false);
reference.add(scrollPane, BorderLayout.CENTER);
reference.add(north, BorderLayout.NORTH);
对象来获取存储的ValuableList
对象。
ArrayList
现在包含所需 button.addActionListener(event -> {
for (final Valuable valuable : register.getList())
{
textArea.append(valuable.toString() + System.lineSeparator());
System.out.println(valuable.toString());
}
});
south.add(button);
reference.add(south, BorderLayout.SOUTH);
} else {
SwingUtilities.invokeLater(() -> {
this.initialiseUserInterface(reference, register);
});
}
return true;
}
对象的ValuableList
类。
List
我强烈建议您删除 public class ValuablesList {
private final String name;
private final ArrayList<Valuable> arrayList;
public ValuablesList(final String name, final int capacity)
{
this.name = name;
this.arrayList = new ArrayList<>(capacity);
}
public String getName()
{
return this.name;
}
public ArrayList<Valuable> getList()
{
return this.arrayList;
}
}
类,因为它是多余的。您可以直接使用ValuableList
来增强性能并整理代码。如果您对我的回答有任何疑问,请随时发表评论。