如何在 JPanel 类之间传递变量?
我有一个 JTabbedPane 和两个 JPanel,需要保留在不同的类中。在 PageOne 中,我希望能够通过单击添加按钮来增加 MyInteger,然后我希望能够通过单击那里的按钮在 PageTwo 中打印该整数。它在 PageOne 中打印正确的值,但当我将其传递给 PageTwo 类并在那里打印时,它会打印 0。
如何传递值,以便在单击两个 JPanel 中的按钮时打印正确的值?我认为这与我从 PageOne 继承的方式有关,但找不到解决我的问题的更改它的方法。
主类:
import javax.swing.*;
public class MyJFrame {
PageOne pageOne;
PageTwo pageTwo;
public MyJFrame() {
JFrame f = new JFrame();
pageOne = new PageOne();
pageTwo = new PageTwo();
JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("Page One", pageOne);
jTabbedPane.addTab("Page Two", pageTwo);
f.add(jTabbedPane);
f.setSize(200,120);
f.setVisible(true);
}
public static void main(String[] args) throws InterruptedException {
new MyJFrame();
}
}
JPanel一:
import javax.swing.*;
public class PageOne extends JPanel {
public Integer myInteger = 0;
public JButton add;
public PageOne() {
add = new JButton();
add.setText("Increment number");
add(add);
add.addActionListener(actionEvent -> {
myInteger++;
printOne();
});
}
public void printOne() {
System.out.println("Page One:" + myInteger);
}
}
JPanel二:
import javax.swing.*;
public class PageTwo extends JPanel {
PageOne pageOneRef = new PageOne();
public JButton button;
public PageTwo() {
JPanel panel = new JPanel();
button = new JButton("Click me");
panel.add(button);
add(panel);
button.addActionListener(e -> printTwo());
}
public void printTwo() {
System.out.println("Page Two:" + pageOneRef.myInteger);
}
}
I have a JTabbedPane with two JPanels that need to stay in seperate classes. In PageOne, I want to be able to increment MyInteger by clicking the add button, and I then want to be able to print that integer in PageTwo by clicking the button there. It prints the correct value in PageOne, but prints 0 when I pass it to the PageTwo class and print it there.
How can I pass the value in such a way that it prints the correct value when clicking the button in both JPanels? I figure it has something to do with how I inherit from PageOne, but couldn't find a way of changing it on SO that solved my problem.
Main class:
import javax.swing.*;
public class MyJFrame {
PageOne pageOne;
PageTwo pageTwo;
public MyJFrame() {
JFrame f = new JFrame();
pageOne = new PageOne();
pageTwo = new PageTwo();
JTabbedPane jTabbedPane = new JTabbedPane();
jTabbedPane.addTab("Page One", pageOne);
jTabbedPane.addTab("Page Two", pageTwo);
f.add(jTabbedPane);
f.setSize(200,120);
f.setVisible(true);
}
public static void main(String[] args) throws InterruptedException {
new MyJFrame();
}
}
JPanel One:
import javax.swing.*;
public class PageOne extends JPanel {
public Integer myInteger = 0;
public JButton add;
public PageOne() {
add = new JButton();
add.setText("Increment number");
add(add);
add.addActionListener(actionEvent -> {
myInteger++;
printOne();
});
}
public void printOne() {
System.out.println("Page One:" + myInteger);
}
}
JPanel Two:
import javax.swing.*;
public class PageTwo extends JPanel {
PageOne pageOneRef = new PageOne();
public JButton button;
public PageTwo() {
JPanel panel = new JPanel();
button = new JButton("Click me");
panel.add(button);
add(panel);
button.addActionListener(e -> printTwo());
}
public void printTwo() {
System.out.println("Page Two:" + pageOneRef.myInteger);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本答案是,您需要某种可以在两个组件之间共享的“容器”。这通常是通过使用某种“模型”来实现的。
请参阅:
了解下面提供的概念的概述
可运行示例
停下来思考一下每个组件的职责。
PageOne
想要更新模型,为此,它还需要知道模型的值。该模型没有假设该模型的使用者“如何”执行此操作(因此我没有提供增量方法),它只是允许使用者设置它想要的值PageTwo
只是想显示值(并在发生某些更改时收到通知),因此它不需要模型的可变版本。这限制了消费者可以对模型执行的操作,而不是将功能暴露给不需要它的各方(并且可能会试图滥用它)
这是一个演示,您的需求可能会有所不同,但我有点守财奴当我设计这类东西时,我需要消费者向我证明他们需要功能,而不是“假设”他们“可能”需要什么功能
The basic answer is, you need some kind of "container" which can be shared between the two components. This is commonly achieved through the use of a "model" of some kind.
See:
for an overview of the concepts presented below
Runnable example
Stop for a second and think about the responsibilities of each component.
PageOne
want's to update the model, in order to do so, it also needs to know the value of the model. The model makes no assumption about "how" the consumer of this model will do that (so I didn't provide aincrement
method), it just allows the consumer to set the value it wantsPageTwo
just wants to display the value (and be notified when some change occurs), so it doesn't need a mutable version of the model.This restricts what consumers maybe able to do to the model rather the exposing functionality to parties which don't need it (and might be tempted to abuse it)
This is a demonstration and your needs may differ, but I'm bit of a scrooge when I design these kinds of things, I need the consumers to prove to me that they need functionality, rather then "assuming" what functionality they "might" require ????
This is a practice known is "information hiding", which is supported by Polymorphism in OO languages