Java 在不同操作之后修改对象的最佳方法

Java 在不同操作之后修改对象的最佳方法,java,swing,object,listener,Java,Swing,Object,Listener,我想知道在另一个动作之后修改对象的最佳方式是什么 示例:我有一个带有一些组件的JPanel,其中一个打开了一个新的JPanel。在这个新的JPanel中,我有一个按钮,我可以使用它来修改第一个JPanel 我找到了两个可行的解决方案,我想知道哪一个是最好的(或者另一个) 第一个是在第一个类中添加Actionlistener: public class SomePanel extends JPanel{ private JButton button = new JButton("Open

我想知道在另一个动作之后修改对象的最佳方式是什么

示例:我有一个带有一些组件的JPanel,其中一个打开了一个新的JPanel。在这个新的JPanel中,我有一个按钮,我可以使用它来修改第一个JPanel

我找到了两个可行的解决方案,我想知道哪一个是最好的(或者另一个)

第一个是在第一个类中添加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作为第二个参数传递给第二个:

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;
    } 
}

这是正确的吗?

此外,您的解决方案并不错误,在这些对象之间生成高耦合,因此我将为您提供另一个解决方案

您可以采取这样的方法,然后将可视组件解耦,actionListener充当控制器/中介器

我不知道你的值是什么,但我把它作为一个可观察的属性,并在上面注册一个观察者

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());

两种方法都可以。如果你想找到最干净的方法,我会说这要看情况而定

这是一个衔接问题。你必须问问自己,每节课的目的是什么

如果您认为SomeOtherPanel的目的只是在按下按钮时将更改(无论更改是什么)应用于SomePanel,那么第一个方法是好的,因为不必接触SomeOtherPanel中的任何代码,您可以选择该类对SomePanel的影响


如果您认为SomeOtherPanel的目的是将实例变量值设置为1,那么第二种解决方案是好的。

此代码打开SomeOtherPanel。如果somePanel中的值设置为1,我想要的是SomeOtherPanel中的按钮将somePanel中的值设置为1。也许可以创建类似的东西:使用fireProperty对应于其他面板中按钮的点击(而不是值的变化),并将侦听器放在某个面板中?@caRameL确切地说,我不知道你想做什么,但基本上想法是一样的。。。让我输入clear并编辑答案,当在
SomeOther面板
中单击按钮时,您需要设置值,如果是,您可以注册一个新的侦听器到按钮:D,这样更容易。