0

我对两个类有困难:Program 类和 EventHandler 类。

程序类创建两个独立的窗口,一个是“地面”(背景为 jpg 的窗口),另一个窗口有“添加”按钮,可以在“地面”上添加一朵花。“添加”按钮调用 ActionListener 方法,该方法创建一个 Eventhandler 类的对象,

   public  class Program implements Runnable {

    @Override
    //implements interface
    public void run() {
        example1.Ground g;
        g = new example1.Ground();
    // ground object
        javax.swing.JFrame window = new javax.swing.JFrame("windowwithbutton");
        //window (JFRAME)
        javax.swing.JPanel panel = new javax.swing.JPanel();
        //content (JPANEL)
        javax.swing.JButton ab = new javax.swing.JButton("add");
        ab.addActionListener(new eventHandler());

事件处理类:

public class Eventhandler implements java.awt.event.ActionListener {


@Override

   public void actionPerformed(java.awt.event.ActionEvent e) {

    example1.Flower flower;
    flower = new example1.Flower();

一切正常,但我很难向事件处理程序添加一段代码,所以当按下按钮时 - 它应该在程序类创建的地面上创建花朵。我应该如何设置对象之间的这种关联?

谢谢 :)

4

1 回答 1

1

You're going to need to pass some kind of reference of some kind of object to your EventHandler, what you don't want to do is give more power to the EventHandler then it should have, for example, your add flower event handler should only be capable of doing that and not, for example, setting fire to the world (or removing everything from the component).

The event handler doesn't need to know anything about HOW things happen, only that when it calls some method, it does.

Start by creating a couple of interfaces, for example...

public interface Flower {
    // What ever properties you want you flower to have
}

public interface Ground {

    public void add(Flower flower);
    // Other stuff you might like ground to have/do

}

These describe the contract to other users of these interfaces, describing what can be done or obtained from them. This is a very important concept in OO programming.

You then need to provide some kind of implementation for these classes

public class AFlower extends ... implements Flower {

}

public class SomeGround extends ... implements Ground {

}

These are the physical implementations of these interfaces, you could have any number of implementations of Flower, Rose, VenusFlyTrap, but Ground won't care...

Now, in order for your EventHandler to be able to actually do anything useful, you will need to pass it an instance of Ground to work with, for example...

public class Eventhandler implements java.awt.event.ActionListener {

    private Ground ground;

    public Eventhandler(Ground ground) {
        this.ground = ground;
    }

    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {
        Flower flower = new AFlower();
        ground.add(flow);
    }

Then when you create an instance of EventHandler, you would pass it an instance of Ground, for example...

example1.Ground g = new SomeGround();
// ground object
javax.swing.JFrame window = new javax.swing.JFrame("windowwithbutton");
//window (JFRAME)
javax.swing.JPanel panel = new javax.swing.JPanel();
//content (JPANEL)
javax.swing.JButton ab = new javax.swing.JButton("add");
ab.addActionListener(new EventHandler(g));
于 2014-10-30T06:52:09.350 回答