Java - 可重用按钮操作处理程序概念问题

发布于 2024-11-26 03:17:20 字数 2320 浏览 1 评论 0原文

首先——对代码墙感到抱歉,但它并不太可怕,只是我试图解释的一个框架。它运行没有错误。

目标

我正在为我的 GUI 创建一个可重用的按钮类,每个按钮对象在单击时需要有一个不同的处理程序。我想为每个新按钮分配一个 ClickHandler 对象。然后,按钮将在处理程序上调用 init() 并继续运行。不幸的是,存在打字问题,因为每个处理程序类都有不同的名称。

当前进度

现在,处理程序的类型为 HandlerA,但我想让它处理任何名称,例如“SettingsHandler”或“GoToTheWahWah”等。

我'我尝试过使用泛型类型,但由于我是新手,并且具有 Webdev 背景,因此我一直在克服一个概念障碍。这是解决问题的正确方法吗?

代码

ReuseableButton.java 是一个可重用的类,唯一改变的是单击它时的操作:

package gui;

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

public class ReuseableButton extends JButton implements ActionListener {
  private static final long serialVersionUID = 1L;

  // I want a generic type here, not just HandlerA!
  private HandlerA ClickHandler;

  // Assemble generic button
  public ReuseableButton(Container c, String s) {
    super(s);
    addActionListener(this);
    c.add(this);
  }

  // Once again, generic type, not just HandlerA!
  public void SetClickHandler(HandlerA ch) {
    ClickHandler = ch;
  }

  // Call init() from whatever class has been defined as click handler.
  public void actionPerformed(ActionEvent e) {
    ClickHandler.init();
  }
}

Controller.java 触发框架并根据需要组装按钮(目前只有一个按钮)。

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

public class Controller extends JFrame {
  private static final long serialVersionUID = 1L;


  public Controller() {
    JFrame frame = new JFrame("Handler Test GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        

    Container pane = frame.getContentPane();   
    pane.setLayout(new FlowLayout());

    ReuseableButton b = new ReuseableButton(pane,"Reuseable Button A");
    // THE QUESTION IS HERE: Pass a generic object?
    b.SetClickHandler(new HandlerA());

    frame.pack();
    frame.setSize(200,200);    
    frame.setVisible(true);        
  }

  public static void main(String args[]) {
    new Controller();
  }
}

HandlerA.java 是按钮单击的随机处理程序的示例。稍后,可能会有 HandlerBHandlerC 等。

package gui;

// A random handler
public class HandlerA {
  public void init() {
    System.out.println("Button clicked.");
  }
}

提前非常感谢!

First off - sorry for the wall of code but it's not too horrendous, just a framework for what I'm trying to explain. It runs without errors.

The goal

I'm making a reuseable button class for my GUI and each button object needs to have a different handler when it's clicked. I want to to assign a ClickHandler object to each new button. Then, the button would call init() on the handler, and be on its way. Unfortunately, there's a typing problem, since each handler class would have a different name.

Current progress

Right now, the handler is typed as HandlerA, but I'd like to have it handle any name, like "SettingsHandler" or "GoToTheWahWah" etc.

I've tried messing about with generic types, but since I'm new to this, and from a webdev background, there seems to be a conceptual hurdle I keep knocking over. Is this the right way to approach the problem?

The code

ReuseableButton.java is a reuseable class, the only thing that changes is the action when it's clicked:

package gui;

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

public class ReuseableButton extends JButton implements ActionListener {
  private static final long serialVersionUID = 1L;

  // I want a generic type here, not just HandlerA!
  private HandlerA ClickHandler;

  // Assemble generic button
  public ReuseableButton(Container c, String s) {
    super(s);
    addActionListener(this);
    c.add(this);
  }

  // Once again, generic type, not just HandlerA!
  public void SetClickHandler(HandlerA ch) {
    ClickHandler = ch;
  }

  // Call init() from whatever class has been defined as click handler.
  public void actionPerformed(ActionEvent e) {
    ClickHandler.init();
  }
}

Controller.java fires the frame and assembles buttons as needed (right now, only one button).

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

public class Controller extends JFrame {
  private static final long serialVersionUID = 1L;


  public Controller() {
    JFrame frame = new JFrame("Handler Test GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        

    Container pane = frame.getContentPane();   
    pane.setLayout(new FlowLayout());

    ReuseableButton b = new ReuseableButton(pane,"Reuseable Button A");
    // THE QUESTION IS HERE: Pass a generic object?
    b.SetClickHandler(new HandlerA());

    frame.pack();
    frame.setSize(200,200);    
    frame.setVisible(true);        
  }

  public static void main(String args[]) {
    new Controller();
  }
}

HandlerA.java is a sample of a random handler for the button click. Later, there could be HandlerB, HandlerC, etc.

package gui;

// A random handler
public class HandlerA {
  public void init() {
    System.out.println("Button clicked.");
  }
}

Thanks very much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春风十里 2024-12-03 03:17:20

所有处理程序都应该实现类似 Clickable 之类的接口。这样接口就指定了 init 函数的存在:

public interface Clickable 
{
    public void init();
}

定义 HandlerA:

public class HandlerA implements Clickable {
    public void init() {
       System.out.println("Button clicked.");
    }
}

All of you handlers should implement an interface like Clickable or something. That way the interface specifies the existence of the init function:

public interface Clickable 
{
    public void init();
}

Making the HandlerA definition:

public class HandlerA implements Clickable {
    public void init() {
       System.out.println("Button clicked.");
    }
}
Bonjour°[大白 2024-12-03 03:17:20

我建议在这种情况下使用继承:

public abstract class AbstractHandler {

    public abstract void init();
}

然后:

public class ConcreteHandlerA extends AbstractHandler {

    @Override
    public void init() {
        // do stuff...
    }

}

Controller

public class ReuseableButton extends JButton implements ActionListener {
    // I want a generic type here, not just HandlerA!
    private AbstractHandler ClickHandler;

    public Controller() {
        //...

        ReuseableButton b = new ReuseableButton(pane,"Reuseable Button A");
        AbstractHandler handlerA = new ConcreteHandlerA();
        b.SetClickHandler(handlerA);

        // ...
    }
}

不确定这是否是您正在寻找的...

顺便说一句:您可以将 AbstractHandler 定义为interface 也是如此,但您可能还想在这里实现一些通用逻辑 - 在处理程序之间共享。

I recommend to work with inheritence in this case:

public abstract class AbstractHandler {

    public abstract void init();
}

Then:

public class ConcreteHandlerA extends AbstractHandler {

    @Override
    public void init() {
        // do stuff...
    }

}

Controller

public class ReuseableButton extends JButton implements ActionListener {
    // I want a generic type here, not just HandlerA!
    private AbstractHandler ClickHandler;

    public Controller() {
        //...

        ReuseableButton b = new ReuseableButton(pane,"Reuseable Button A");
        AbstractHandler handlerA = new ConcreteHandlerA();
        b.SetClickHandler(handlerA);

        // ...
    }
}

Not sure if this is what you're looking for...

BTW: You can define the AbstractHandler as an interface as well, but you may want to implement some common logic here as well - shared across handlers.

破晓 2024-12-03 03:17:20

您应该使用处理程序的接口。

public interface ClickHandler() {
    void init();
}

ReuseableButton b = new ReuseableButton(pane,"Reuseable Button A");
b.SetClickHandler(object which implements the ClickHandler interface);

这与普通 JButton 的概念相同。其中有 ActionListener 接口和 actionPerformed 方法。

PS如果我不明白你的问题,请纠正我。

You should use an interface for the handler.

public interface ClickHandler() {
    void init();
}

ReuseableButton b = new ReuseableButton(pane,"Reuseable Button A");
b.SetClickHandler(object which implements the ClickHandler interface);

This is the same concept as the normal JButton. There you have the ActionListener interface and the actionPerformed method in it.

P.S. If I don't understand your question, please correct me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文