如何将变量从一个 class 传递到 Java 中的另一个?
How can I pass variables from one class to another in Java?
我正在尝试将变量从一个 class 传递到另一个。我有 3 个 classes,如下所示。
public class OvalPanel extends JPanel {
//declare variables
public OvalPanel() {
//create panel here
ovalShape = new OvalShape();
ovalShape.setMyColor(255, 255, 255);
//set layout down here
}
那我有OvalShape.java
public class OvalShape extends JPanel {
public OvalShape() {
Dimension size = getPreferredSize();
size.width = 300;
size.height= 300;
setPreferredSize(size);
setBorder(BorderFactory.createLoweredSoftBevelBorder());
}
public void setMyColor(int r, int g, int b) {
this.myColor = new Color(r,g,b);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(myColor);
g.fillOval(0,0,100,100);
}
最后 DetailsPanel.java 设置了 r、g、b 值
public class DetailsPanel extends JPanel {
//declare variables
public DetailsPanel() {
Dimension size = getPreferredSize();
size.width = 250;
setPreferredSize(size);
redSlider = new JSlider(SwingConstants.VERTICAL, 0, 255, 0);
redSlider.addChangeListener(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
red = redSlider.getValue();
}
}
);
//greenSlider
//blueSlider
//set layout
}
我的问题是如何将变量 red, green, blue 从 DetailsPanel
传递到方法 ovalShape.setMyColor()
in OvalPanel
?
我认为您需要通过添加了所有面板的父对象来完成。你应该看看 mediator design pattern:
a set of objects communicate in well-defined but complex ways. The
resulting interdependencies are unstructured and difficult to
understand.
通过使用调解器模式,您可以减少 3 类 之间的依赖关系。在良好的面向对象设计中,这 3 个 类 不应相互依赖,因此您可以在必要时删除或替换它们,而不会破坏其余代码。
我正在尝试将变量从一个 class 传递到另一个。我有 3 个 classes,如下所示。
public class OvalPanel extends JPanel {
//declare variables
public OvalPanel() {
//create panel here
ovalShape = new OvalShape();
ovalShape.setMyColor(255, 255, 255);
//set layout down here
}
那我有OvalShape.java
public class OvalShape extends JPanel {
public OvalShape() {
Dimension size = getPreferredSize();
size.width = 300;
size.height= 300;
setPreferredSize(size);
setBorder(BorderFactory.createLoweredSoftBevelBorder());
}
public void setMyColor(int r, int g, int b) {
this.myColor = new Color(r,g,b);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(myColor);
g.fillOval(0,0,100,100);
}
最后 DetailsPanel.java 设置了 r、g、b 值
public class DetailsPanel extends JPanel {
//declare variables
public DetailsPanel() {
Dimension size = getPreferredSize();
size.width = 250;
setPreferredSize(size);
redSlider = new JSlider(SwingConstants.VERTICAL, 0, 255, 0);
redSlider.addChangeListener(
new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
red = redSlider.getValue();
}
}
);
//greenSlider
//blueSlider
//set layout
}
我的问题是如何将变量 red, green, blue 从 DetailsPanel
传递到方法 ovalShape.setMyColor()
in OvalPanel
?
我认为您需要通过添加了所有面板的父对象来完成。你应该看看 mediator design pattern:
a set of objects communicate in well-defined but complex ways. The resulting interdependencies are unstructured and difficult to understand.
通过使用调解器模式,您可以减少 3 类 之间的依赖关系。在良好的面向对象设计中,这 3 个 类 不应相互依赖,因此您可以在必要时删除或替换它们,而不会破坏其余代码。