我想知道在另一个对象中执行操作后修改对象的最佳方法是什么。
示例:我有一个包含一些组件的 JPanel,其中一个组件打开一个新的 JPanel。在这个新的 JPanel 中,我有一个按钮,我可以用它来修改第一个 JPanel。
我找到了 2 个可行的解决方案,我想知道哪一个是最好的(或另一个)。
第一个是在第一个类中添加一个 Actionlistener:
public class SomePanel extends JPanel{
private JButton button = new JButton("Open New Frame");
private SomeOtherPanel otherPanel = new SomeOtherPanel();
private int value = 0;
public SomePanel(){
// initialization code
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
otherPanel.setVisible(true);
}
});
otherPanel.getButton().addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
value = 1;
}
});
}
public class SomeOtherPanel extends JPanel{
private JButton button = new JButton("Modify First Panel value");
public SomeOtherPanel(){
}
public JButton getButton() {
return button;
}
}
通过第一个 JPanel 作为第二个 JPanel 的参数传递第二个:
public class SomePanel extends JPanel{
private JButton button = new JButton("Open New Frame");
private SomeOtherPanel otherPanel = new SomeOtherPanel(this);
private int value = 0;
public SomePanel(){
// initialization code ... size, color ...
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
otherPanel.setVisible(true);
}
});
}
public void setValue(int value) {
this.value = value;
}
}
public class SomeOtherPanel extends JPanel{
private JButton button = new JButton("Modify First Panel value");
public SomePanel somePanel;
public SomeOtherPanel(SomePanel panel){
this.somePanel = panel;
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
somePanel.setValue(1);
}
});
}
public JButton getButton() {
return button;
}
}
这是正确的吗?
最佳答案
此外,您的解决方案并不正确,会在这些对象之间产生高耦合,因此我将为您提供另一个解决方案。
您可以采取observer pattern的方法然后解耦视觉组件,actionListener 作为 Controller /“中介”。
我不知道你的值做什么,但我把它作为一个可观察的属性并在其上注册一个观察者。
public class SomePanel extends JPanel{
private JButton button = new JButton("Open New Frame");
private int value;
public SomePanel(){
// initialization code ... size, color ...
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
setValue(1);
}
});
}
public void setValue(int value) {
int oldValue= this.value;
this.value = value;
firePropertyChangeValue("value",oldValue,this.value);
}
}
在其他一些面板中
public class SomeOtherPanel extends JPanel {
private PropertyChangeListener listener = new ValueListener();
public PropertyChangeListener getListener(){
return listener;
}
private class ValueListener implements PropertyChangeListener{
@Override
public void propertyChange(PropertyChangeEvent evt){
if(evt == null)
return;
if(evt.getPropertyName().equals("value") && ((int) evt.getNewValue()) == 1 ){
SomeOtherPanel.this.setVisible(true);
}
}
}
}
在客户端代码中初始化两个面板。
示例:
JPanel panel = new SomePanel();
SomeOtherPanel otherPanel = new SomeOtherPanel();
panel.addPropertyChangeListener("value",otherPanel.getListener());
更新
因为我不明白你想要实现什么,所以你的解决方案很容易,只是简单地不要使用匿名类
public class SomePanel extends JPanel{
private ActionListener myAction = new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt){
value =1;//or what you want
}
};
public ActionListener getMyAction{
return myAction;
}
}
在另一个面板中..
public class SomeOtherPanel extends JPanel {
private JButton button = new JButton();
public void addButtonAction(ActionListener listener){
button.addActionListener(listener);
}
}
在客户端代码中:
JPanel panel = new SomePanel();
SomeOtherPanel otherPanel = new SomeOtherPanel();
otherPanel .addButtonAction(panel .getMyAction());
关于java - 在不同对象中执行操作后修改对象的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18918687/