两个类之间的ActionListener [英] ActionListener between two classes

查看:109
本文介绍了两个类之间的ActionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导入javax.swing.*;

import javax.swing.*;

类标签扩展了JFrame { JPanel pnl =新的JPanel();

class Labels extends JFrame{ JPanel pnl = new JPanel();

ImageIcon duke = new ImageIcon("duke.png");

JLabel lbl1 = new JLabel(duke);
JLabel lbl2 = new JLabel("Duke is the friendly mascot of Java technology.");
JLabel lbl3 = new JLabel ("Duke", duke, JLabel.CENTER);

public Labels(){

    super("Swing Labels");
    setSize(1000 , 800);
    setDefaultCloseOperation( EXIT_ON_CLOSE);
    add(pnl);
    setVisible(true);

    lbl1.setToolTipText("Duke - the Java Mascot");

    lbl3.setHorizontalTextPosition(JLabel.CENTER);
    lbl3.setVerticalTextPosition(JLabel.BOTTOM);

    pnl.add(lbl1);
    pnl.add(lbl2);
    pnl.add(lbl3);

}
    public static void main(String [] args){
        Labels gui = new Labels();
    }

}

如果我想将此用作JApplet,该怎么办?必须做什么?很难改变吗?

What if I want to use this as a JApplet? what has to be done? Is it hard to change?

在JFrame上运行的东西与在JApplet上运行的东西一样吗?

The things that run on a JFrame are the same as the ones on a JApplet?

推荐答案

某些类(最好是B的内部类)如果要仅允许从JPanel B输入,则必须实现ActionListener.该ActionListener必须添加到JTextField中:

Some class, preferably an inner class of B if you're going to allow input only from JPanel B, has to implement ActionListener. That ActionListener has to be added to the JTextField:

myInputTextField.addActionListener(theActionListenerIAmGoingToWrite). 

ActionListener必须定义如下的actionPerformed():

That ActionListener has to define actionPerformed() something like this :

String text = inputTextField.getText();
Integer inputInteger = Integer.getInteger(text).toInt(); 
graph.doStuffWithIntegerInput(inputInteger);

要回答的设计问题是:

ActionListener是匿名内部类吗?如果只打算将它们与彼此了解的图和B的特定实例一起使用,请采用这种方式.如果是这样,则它必须从其包含的类中获取对Graph实例的引用,这意味着B有提供该实例的此类实例.

Is the ActionListener an anonymous inner class? If you're only going to use it with a specific instances of graph and B which know about each other, then go this way. If so, it has to get a reference to the Graph instance from its containing class, which implies B has such an instance to offer it.

ActionListener的操作是否可以在其他地方重用? 输入数字"小部件会出现在其他JPanels上的其他位置吗?如果是这样,请使其成为独立的课程.您仍然需要对图形的引用,因此要么将图形作为构造器参数和/或使用setGraph方法,因为它某种程度上需要了解图形实例,以便它可以向其发送消息.

Is the action of the ActionListener reusable elsewhere? Will the "enter numbers" widget appear elsewhere on other JPanels? If so make it a stand alone class. You still need a reference to graph so either graph in as a constrcutor parameter and/or have a setGraph method because somehow it needs to know about the graph instance so it can send it a message.

完成!

修改2

好的,让我们将您需要的内容映射到MVC框架.因此,您的视图由Graph类,B类和textField组成.

OK so let's map what you have to an MVC framework. So your view consists of the Graph class and your class B and your textFields.

现在包含在您的图形类中,您还拥有要图形化的数据.那就是您定义的开始,结束,x,y变量.要成为MVC-land的良好公民,您必须将它们拉出并将它们包含在一个单独的对象b/c中,它们构成了图形的模型.从根本上讲,模型是您希望呈现的事实集,而与呈现方式无关,而视图是与在屏幕上绘画有关的所有代码.

Now contained within your graph class, you also have the data you want to graph. That's the start, finish, x , y variables you've defined. To be a good citizen of MVC-land, you would have to pull these out and contain them within a separate object b/c they constitute the model of your graph. A model is, very basically, the set of facts you wish to present, irrespective of how they are going to be presented, while the view is all the code concerned with painting stuff to the screen.

因此,此模型对象将是一个简单的数据对象,其中包含已定义的所有变量的getter和setter,并将在构造函数中或通过Graph对象上的setter传递到Graph对象中.

So this model object would be a simple data object with getters and setters for all the variables you've defined and would be passed into the Graph object either at the constructor or through a setter on the Graph object.

通过创建此GraphData对象,您已经将视图(图形从GraphData对象建模)中分离了出来.

By creating this GraphData object, you've separated the view, the Graph from it's model the GraphData object.

使用更复杂的Graph时,您实际上是在GraphModel中指定要创建的Graph对象的种类",轴名称是什么,要绘制哪种线,类似的东西,但是这对于您拥有的东西来说是过分杀伤力的.

With a more complicated Graph, you'd actually specify in a GraphModel what "kind" of Graph object you want to create, what the axis names would be, what kind of line you want to draw, stuff like that, but that's overkill for what you have.

好的,现在您有了Graph和GraphData对象,并且有了一些将它们组合在一起的方法.

OK so now you have your Graph and GraphData objects and some way to get them together.

接下来,让我们看一下您的类B.由于它是一个JPanel视图,因此它实质上是其所包含的Views的聚集器和表示者.这些将是Graph和JTextFields.因此,您可以按需添加它们,并指定布局.

Next let's look at your class B. Since it's a JPanel, a view, it's essentially an aggregator and presenter of its contained Views. Those would be the Graph and the JTextFields. So you add them as you have, specifying the layout.

假设的重构是这样的:

创建主程序.在该主创建中:JTextFields,ActionListener,JPanel(B),Graph和GraphModel.

Create main program. In that main create: JTextFields, ActionListener, JPanel (B), Graph and GraphModel.

将GraphModel与Graph关联.

Associate GraphModel with Graph.

将图形与JPanel关联.

Associate Graph with JPanel.

将ActionListener与JJtextField关联

Associate ActionListener with JJtextField

将JTextField与JPanel关联

Associate JTextField with JPanel

就编写ActionListener而言,只是使其成为自己的类,这是最容易编写和理解的.因此:

As far as writing ActionListener goes just make it its own class, that's the easiest to write and understand. So thus:

public class MyTextFieldListener implements ActionListener
{
   private Graph graph;
   public   MyTextFieldListener(Graph graph)
   {   
      this.graph = graph;
   }// construtor

   public void actionPerformed (ActionEvent ae) 
  {
      String text =  ((JTextField)ae.getSource()).getText();
      Integer inputInteger = Integer.getInteger().(text).toInt();
      graph.doStuffWithIntegerInput(inputInteger); 
  } // actionPerformed
}// class

现在的麻烦之处在于,直到获得所有四个文本字段的信息后,您才能更新图形.有多种方法可以更改此代码以适应这种情况,但这是您需要去的基本概念.

Now a complication could be that you can't update the graph until you have info from all four textfields. There are different ways to change this code to accommodate that but this is the basic idea of where you need to go.

HTH

这篇关于两个类之间的ActionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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