Java - 可重用按钮操作处理程序概念问题
首先——对代码墙感到抱歉,但它并不太可怕,只是我试图解释的一个框架。它运行没有错误。
目标
我正在为我的 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
是按钮单击的随机处理程序的示例。稍后,可能会有 HandlerB
、HandlerC
等。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有处理程序都应该实现类似 Clickable 之类的接口。这样接口就指定了 init 函数的存在:
定义 HandlerA:
All of you handlers should implement an interface like
Clickable
or something. That way the interface specifies the existence of the init function:Making the HandlerA definition:
我建议在这种情况下使用继承:
然后:
Controller
不确定这是否是您正在寻找的...
顺便说一句:您可以将
AbstractHandler
定义为interface
也是如此,但您可能还想在这里实现一些通用逻辑 - 在处理程序之间共享。I recommend to work with inheritence in this case:
Then:
Controller
Not sure if this is what you're looking for...
BTW: You can define the
AbstractHandler
as aninterface
as well, but you may want to implement some common logic here as well - shared across handlers.您应该使用处理程序的接口。
这与普通
JButton
的概念相同。其中有ActionListener
接口和actionPerformed
方法。PS如果我不明白你的问题,请纠正我。
You should use an interface for the handler.
This is the same concept as the normal
JButton
. There you have theActionListener
interface and theactionPerformed
method in it.P.S. If I don't understand your question, please correct me.