可以另外的一个ActionListener是短?我可以添加参数actionPerformed?
-
22-09-2019 - |
题
我有一个很大的表含有一个按钮在每个单元。这些按钮是非常相似,这样做几乎相同。如果我增加一个行动监听到的每一个按钮在这一方式:
tmp.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt) {
proposition = proposition + action;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
updatePropositionPanel();
}
});
}
});
实际上,每一行动的听众不同于所有其他人的价值的 action
. proposition
和 updatePropositionPanel
是的一个领域和方法的类。
第一,我认为我可以使它更短的如果我做不使用内部课程。所以,我决定的程序一个新的ActionListener类。但比我意识到,在这种情况下,"提议"不可见于该类的实例。
然后我决定增加actionPerformed方法的前类和做:
addActionListener(this)
.但比我意识到我不知道如何得到参数actionPerformed方法。
因此,它是如何工作。我可以添加一个行动监听器在很短的,高贵的方式?
添加:
我喜欢这个想法的程序内类的构造,可以采取的一些参数和actioPerformed方法,其中可以使用给出的参数中所构造。我开始这样做,然后意识到,它创建了一个冲突有其他的内部匿名类(用于像在上面给出的代码)。因此,我认为我将建立另外一类(不内一个)。
解决方案
你可以创建自己的类,并通过数据转的构造。例如
public class MyActionListener
{
private int proposition;
private MyOtherClass moc;
public MyActionListener(int proposition, MyOtherClass moc) {
this.proposition = proposition;
this.moc = moc;
}
public void actionPerformed(ActionEvent evt) {
proposition += moc.action;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
moc.updatePropositionPanel();
}
});
}
}
然后你可以将它作为正常,通过的任何论点你喜欢的构造:
tmp.addActionListener( new MyActionListener(proposition, this) );
其他提示
编辑:我已经改变了类,以显示建设与MyOuterClass.
这里是一些代码勾勒出来。希望这会在你的1,而是我会怎样实施它。
public class MyOuterClass {
// member variables for MyOuterClass
public MyOuterClass() {
// ...constructor stuff here
}
// ...outer class stuff here - methods, etc.
// The code each place you want to add the listener, somewhere in MyOuterClass
tmp.addActionListener(new MyActionListener(poposition, action));
// below outer class stuff, although it can really be most places, I usually put
// it here - personal style preference. Swing's classes often put inner
// classes first
/**
* An inner class.
*/
private MyActionListener implements ActionListener {
/**
* Object used here as a filler, replace with appropriate
* class types
*/
private Object proposition;
private Object action;
private MyActionListener(Object proposition, Object action) {
this.proposition = proposition;
this.action = action;
}
public void actionPerformed(ActionEvent evt) {
proposition = proposition + action;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
updatePropositionPanel();
}
}
/**
* Setters provided in case you need to change proposition and action. If not,
* feel free not to have them and to have final members
*/
private void setProposition(Object proposition) {
this.proposition = proposition;
}
private void setAction(Object action) {
this.action = action;
}
}
}
编辑:创造另一类正如你要求在你的编辑,做上,但创建一个非私有其他类的另一种。java文件,并代码的距离。
作为唯一的不同是在价值的 action
你可以把内的代码的方法。(还@复盖,是不必要的, +=
是有用的,在这里.)
public void setup(
final AbstractButton button,
final int action
) {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
proposition += action;
EventQueue.invokeLater(new Runnable() {
public void run() {
updatePropositionPanel();
}
});
}
});
}
的 invokeLater
可能是毫无意义的,因为你将在AWT事件分发线程(服务)。
如果你是增加大量的一般目的的行动,然后你可以简化,它通过使用一个接口,没有毫无意义的事件的对象,与它相关联。
如果你想要予以解决了你可以添加的听众的一个亚类的构造。
new ActionHandler(button) { public void action() {
proposition += action;
updatePropositionPanel();
}});
我们希望,JDK7将使Java法为这样的事情不太详细。Java会,但是,总是会有些详细。
备选案文1作如果你做 proposition
可变的(例如 StringBuilder
而不是的 String
).备选案文2的工作,如果声明他们 final
.这样,他们访问的/可见在内的类。
你可以创建一个独立的MyActionListener类,并通过两个数值的主张和行动与构造。它declutters源代码。
你可以创建你自己的听众类实现ActionListener.这一类可能包含的成员变量对应的参你在谈论;你想设他们使用的构造。
呼叫添加监听会,然后看看事情是这样的:
tmp.addActionListener(new MyActionListenerSubClass(proposition, action));
我会假设你的主张和行动变量是为了这个例子。定义一个接口PropositionUpdater,一个接口PropositionPanelUpdater和合作者的主张持有人:
public interface PropositionUpdater() {
public void updateProposition(PropositionHolder holder, String action);
}
public interface PropositionHolder() {
public String getProposition();
public void setProposition(String proposition);
}
public interface PropositionPanelUpdater() {
public void updatePropositionPanel();
}
默认的执行情况的一个提议更新程序很简单:
public class DefaultPropositionUpdater implements PropositionUpdater {
public void updateProposition(final PropositionHolder holder, final String action) {
holder.setProposition(holder.getProposition() + action);
}
}
我会离开的默认的一个PropositionHolder和PropositionPanelUpdater到你的想象力;)
现在,这是你的行动的监听器:
public class PropositionUpdaterActionListener implements ActionListener {
private PropositionHolder holder;
private PropositionUpdater updater;
private PropositionPanelUpdater panelUpdater;
public PropositionUpdaterActionListener(final PropositionHolder holder, final PropositionUpdater updater, final PropositionPanelUpdater panelUpdater) {
super();
this.holder = holder;
this.updater = updater;
this.panelUpdater = panelUpdater;
}
public void actionPerformed(final ActionEvent evt) {
//Not sure how you *got* the action, but whatever...
updater.updateProposition(holder, action);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
panelUpdater.updatePropositionPanel();
}
});
}
}
你没说过多少个按钮你是在谈论。
如果这是一个很小的数目像一个国际象棋棋盘或更少,@juskt的做法是一个很好的一个。
但是,如果你看到的东西越大,我会用这种方法:
public class MyActionListener {
public void actionPerformed(ActionEvent evt) {
JComponent c = (JComponent)evt.getSoource();
int prop = (Integer)c.getclientProperty("PROPOSITION");
int act = (Integer)c.getclientProperty("ACTION");
SomeClass obj = c.getclientProperty("UPDATE");
prop += act;
// If necessary, clientPut("PROPOSITION", prop);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
obj.updatePropositionPanel();
}
});
}
}
这个行动监听器拥有无的状态。结果,一个实例可以用于所有的按钮。为什么喜欢一个去委员会(19×19),这个工作了1的对象,而不是361.