如何将值(数组或数组列表)从一个窗格传递到另一个窗格?
这是我的主类代码:
public static void main(String[] args) {
JFrame f= new JFrame ("My Frame");
f.setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE);
JTabbedPane tp = new JTabbedPane();
tp.addTab("Pane1", new PaneFirst());
tp.addTab("Pane2", new PaneSecond());
f.add(tp);
f.pack();
f.setVisible(true);
}
在 PaneFirst 中,我有一个用户输入值(文本字段)列表,假设我有 5 个。我将每个值存储到一个数组(或者可能是数组列表)中。如何将这 5 个值从 PaneFirst 传递到 PaneSecond?
(抱歉,我是 Java 和面向对象编程语言的新手)
This is my main class code:
public static void main(String[] args) {
JFrame f= new JFrame ("My Frame");
f.setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE);
JTabbedPane tp = new JTabbedPane();
tp.addTab("Pane1", new PaneFirst());
tp.addTab("Pane2", new PaneSecond());
f.add(tp);
f.pack();
f.setVisible(true);
}
In PaneFirst, I have a list of user-entered-value(textfields), lets say I have 5. And I store each of the value into an array(or maybe arraylist). How do I pass those 5 values from PaneFirst to PaneSecond?
(Sorry, I am new to Java and Object Oriented Programming language)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确实不应该将值存储在窗格对象(“视图”)上,而应创建一个单独的类来保存这些值(“模型”)。
然后,您可以将对模型的引用传递给两个窗格,如果一个窗格更改模型类的值,另一窗格也会看到更改。下面的简单例子用代码来展示这个想法。您的模型看起来会有所不同,但它应该给出如何实现、创建和使用它的想法。
You really shouldn't store the values on the pane object ("view") but create a separate class that holds the values ("model").
Then you can pass the reference to the model to both panes and if one pane changes values on model class, the other one will see the changes too. The follwing simple example is to show the idea in code. Your Model will look different but it should give an idea on how to implement, create and use it.
您可以向第一个窗格提供对第二个窗格的引用。类似于:
第一个窗格可以存储它并稍后使用它来调用第二个窗格中的方法。
You could provide a reference to the second pane to the first pane. Something like:
The first pane could then storr this and use it later to call methods from the second pane.