使用ActionListener更新其他类中的变量 [英] Using ActionListener to update variables in other classes

查看:115
本文介绍了使用ActionListener更新其他类中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个applet,以便它允许用户绘制图形.我在构造代码时遇到问题,该代码可以从另一个文件(ControlsB.java)更新Graph.java文件中的变量.下面是我的代码:

I am designing an applet so it allows a user to plot a graph. I am having problems to construct a code which from another file (ControlsB.java) the variables in the Graph.java file are updated. Below is my code:

 import java.awt.*;
import javax.swing.*;


 public class Calculator extends JFrame{

/**
 * 
 */
private static final long serialVersionUID = 1L;
private static final int HORIZONTAL_SCROLLBAR_NEVER = 0;
private static final int VERTICAL_SCROLLBAR_ALWAYS = 0;

public static void main (String[] args){

    JFrame calculator = new JFrame("My Simple Calculator");

    calculator.setSize(500,500);

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

Box a = Box.createHorizontalBox();

JTextArea text = new JTextArea(10,15);

JScrollPane滚动=新的JScrollPane(文本,JScrollPane.VERTICAL_SCROLLBAR_​​ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_​​NEVER);

JScrollPane scroll = new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

Box b = Box.createHorizontalBox();


//JToolBar tool = new JToolBar(SwingConstants.HORIZONTAL);
JButton add = new JButton("+");
JButton minus = new JButton("-");
JButton multi = new JButton("x");
JButton div = new JButton("/");
JButton c = new JButton("C");
JButton eq = new JButton("=");

JTextField field = new JTextField(10);

JButton enter = new JButton("Enter");

b.add(add);
b.add(minus);
b.add(multi);
b.add(div);
b.add(c);
b.add(eq);

a.add(field);
a.add(enter);

panel.add(scroll,BorderLayout.NORTH);
panel.add(b,BorderLayout.CENTER);
panel.add(a,BorderLayout.SOUTH);

calculator.setContentPane(panel);
calculator.setVisible(true);
}

 }

现在,我主要关心的是ControlsB.java文件,我希望在用户输入x轴和y轴范围并点击按钮调整大小时, strong> graph.java文件中的变量会相应更新,从而调整图的大小.

Now my main concerns are in the ControlsB.java file, where I want that when the user inputs the the x-axis and y-axis ranges and hits the button Resize the variables in the graph.java file are updated accordingly and thus the graph resizes.

我在Graph.java文件中讨论的变量在57到65之间.

谢谢

推荐答案

通常,如果您需要在多个类之间共享数据,并从多个类中对该数据进行更新,则可以将数据放入所谓的模型"中,并在不同的班级之间共享该模型.您的视图将是模型中数据的(可视)表示,并且动作侦听器仅在模型上进行操作.

Typically if you need to share data between several classes, and make updates to that data from several classes you put your data in a so-called 'Model', and share that model between the different classes. Your view will be a (visual) representation of the data in the model, and your action listener just operates on the model.

如果确保模型是可观察的,则在更改数据后,视图仅能更新自身.因此,您的ActionListener只是更改数据,该模型让所有相关方知道它已更改(在您的情况下为图形),并且如果需要,这些相关方对此更改做出反应(图形的更新).

If you make sure the model is observable, the view can just update itself when the data is changed. So your ActionListener just changes the data, the model let all interested parties know it has been changed (in your case the graph), and if needed those interested parties react on that change (an update of your graph).

在大多数情况下,我发现代码比文本更能说明问题,所以我举了一个相当愚蠢的示例,但它说明了我的意思.我有一个在视图(DataPanel)中可视化的模型(DataModel),并且一个外部类对该模型进行了定期更新(在我的示例中为Timer),并且当仅知道DataModel而不知道DataPanel.

As I find code more illustrating than text in most case I put together a rather dumb example, but which illustrates what I mean. I have one model (DataModel) which is visualized in a view (DataPanel), and an external class makes regular updates on the model (the Timer in my example), and the view gets updated while the Timer only knows of the DataModel and not of the DataPanel.

哦,是的,代码很长,但是您也向我们扔了很多代码.

Oh yes, the code is rather long, but you threw a lot of code at us as well.

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class ActionListenerDemo {

  private static class DataModel{
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport( this );

    private String data="";

    public void addPropertyChangeListener( PropertyChangeListener listener ) {
      propertyChangeSupport.addPropertyChangeListener( listener );
    }

    public String getData() {
      return data;
    }

    public void setData( String aData ) {
      String old = data;
      data = aData;
      propertyChangeSupport.firePropertyChange( "data", old, data );
    }
  }

  private static class DataPanel extends JFrame{
    private final DataModel dataModel;
    private final JLabel label;
    private DataPanel( DataModel aDataModel ) throws HeadlessException {
      dataModel = aDataModel;
      dataModel.addPropertyChangeListener( new PropertyChangeListener() {
        @Override
        public void propertyChange( PropertyChangeEvent evt ) {
          updateUIFromModel();
        }
      } );
      label = new JLabel( dataModel.getData() );
      add( label );
    }
    private void updateUIFromModel(){
      label.setText( dataModel.getData() );
    }
  }

  public static void main( String[] args ) {
    final DataModel model = new DataModel();
    model.setData( "Change me and watch the UI update !" );
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        DataPanel dataPanel = new DataPanel( model );
        dataPanel.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        dataPanel.pack();
        dataPanel.setVisible( true );
      }
    } );
    Timer timer = new Timer( 1000, new ActionListener() {
      private String[] values = {"Data changed", "More changes", "Lorem ipsum" };
      private int counter = 0;
      @Override
      public void actionPerformed( ActionEvent e ) {
        counter = counter%values.length;
        model.setData( values[counter] );
        counter++;
      }
    } );
    timer.setRepeats( true );
    timer.start();
  }
}

这篇关于使用ActionListener更新其他类中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆