Java 我们可以为WindowListener使用Lambda表达式吗?如果是,怎么做?如果没有,为什么?我可以为下面的代码段设置Lambda表达式吗?
这是我的请求,请使用lambda表达式生成代码。lambda表达式可以替代函数接口(即具有单个非默认方法的接口)。因此,具有多种方法(Java 我们可以为WindowListener使用Lambda表达式吗?如果是,怎么做?如果没有,为什么?我可以为下面的代码段设置Lambda表达式吗?,java,lambda,java-8,windowlistener,Java,Lambda,Java 8,Windowlistener,这是我的请求,请使用lambda表达式生成代码。lambda表达式可以替代函数接口(即具有单个非默认方法的接口)。因此,具有多种方法(windowActivated(WindowEvent e),windowClosed(WindowEvent e),windowClosed(WindowEvent e),…)的WindowAdapter不能被lambda表达式所替代。lambda表达式可以替代函数接口(即具有单个非默认方法的接口). 因此,具有多种方法(windowActivated(Wind
windowActivated(WindowEvent e)
,windowClosed(WindowEvent e)
,windowClosed(WindowEvent e)
,…)的WindowAdapter不能被lambda表达式所替代。lambda表达式可以替代函数接口(即具有单个非默认方法的接口). 因此,具有多种方法(windowActivated(WindowEvent e)
,windowClosed(WindowEvent e)
,windowClosed(WindowEvent e)
,…)的WindowAdapter不能被lambda表达式取代。这里不能直接使用lambda,但如果您有大量的用户界面代码,并且有大量的侦听器,您可以创建如下所示的生成器:
this.addWindowListener(new WindowAdaptor(){
public void windowClosing(WindowEvent we)
{
System.exit(0);//to close the window
}
});
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.function.Consumer;
public class ListenerBuilder {
static final Consumer<WindowEvent> nullConsumer = e -> {};
private Consumer<WindowEvent> opened = nullConsumer;
private Consumer<WindowEvent> closing = nullConsumer;
private Consumer<WindowEvent> closed = nullConsumer;
private Consumer<WindowEvent> iconified = nullConsumer;
private Consumer<WindowEvent> deiconified = nullConsumer;
private Consumer<WindowEvent> activated = nullConsumer;
private Consumer<WindowEvent> deactivated = nullConsumer;
public ListenerBuilder opened(Consumer<WindowEvent> opened)
{ this.opened = opened; return this; }
public ListenerBuilder сlosing(Consumer<WindowEvent> closing)
{ this.closing = closing; return this; }
public ListenerBuilder closed(Consumer<WindowEvent> closed)
{ this.closed = closed; return this; }
public ListenerBuilder iconified(Consumer<WindowEvent> iconified)
{ this.iconified = iconified; return this; }
public ListenerBuilder deiconified(Consumer<WindowEvent> deiconified)
{ this.deiconified = deiconified; return this; }
public ListenerBuilder activated(Consumer<WindowEvent> activated)
{ this.activated = activated; return this; }
public ListenerBuilder deactivated(Consumer<WindowEvent> deactivated)
{ this.deactivated = deactivated; return this; }
public WindowListener build() {
return new WindowListener() {
public void windowOpened(WindowEvent e) {
opened.accept(e);
}
public void windowIconified(WindowEvent e) {
iconified.accept(e);
}
public void windowDeiconified(WindowEvent e) {
deiconified.accept(e);
}
public void windowDeactivated(WindowEvent e) {
deactivated.accept(e);
}
public void windowClosing(WindowEvent e) {
closing.accept(e);
}
public void windowClosed(WindowEvent e) {
closed.accept(e);
}
public void windowActivated(WindowEvent e) {
activated.accept(e);
}
};
}
public void attachTo(Window w) {
w.addWindowListener(build());
}
}
this.addWindowListener(new ListenerBuilder()
.opened(e -> log.info("Window is opened!"))
.closed(e -> System.exit(0)).build());
或者像这样:
this.addWindowListener(new WindowAdaptor(){
public void windowClosing(WindowEvent we)
{
System.exit(0);//to close the window
}
});
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.function.Consumer;
public class ListenerBuilder {
static final Consumer<WindowEvent> nullConsumer = e -> {};
private Consumer<WindowEvent> opened = nullConsumer;
private Consumer<WindowEvent> closing = nullConsumer;
private Consumer<WindowEvent> closed = nullConsumer;
private Consumer<WindowEvent> iconified = nullConsumer;
private Consumer<WindowEvent> deiconified = nullConsumer;
private Consumer<WindowEvent> activated = nullConsumer;
private Consumer<WindowEvent> deactivated = nullConsumer;
public ListenerBuilder opened(Consumer<WindowEvent> opened)
{ this.opened = opened; return this; }
public ListenerBuilder сlosing(Consumer<WindowEvent> closing)
{ this.closing = closing; return this; }
public ListenerBuilder closed(Consumer<WindowEvent> closed)
{ this.closed = closed; return this; }
public ListenerBuilder iconified(Consumer<WindowEvent> iconified)
{ this.iconified = iconified; return this; }
public ListenerBuilder deiconified(Consumer<WindowEvent> deiconified)
{ this.deiconified = deiconified; return this; }
public ListenerBuilder activated(Consumer<WindowEvent> activated)
{ this.activated = activated; return this; }
public ListenerBuilder deactivated(Consumer<WindowEvent> deactivated)
{ this.deactivated = deactivated; return this; }
public WindowListener build() {
return new WindowListener() {
public void windowOpened(WindowEvent e) {
opened.accept(e);
}
public void windowIconified(WindowEvent e) {
iconified.accept(e);
}
public void windowDeiconified(WindowEvent e) {
deiconified.accept(e);
}
public void windowDeactivated(WindowEvent e) {
deactivated.accept(e);
}
public void windowClosing(WindowEvent e) {
closing.accept(e);
}
public void windowClosed(WindowEvent e) {
closed.accept(e);
}
public void windowActivated(WindowEvent e) {
activated.accept(e);
}
};
}
public void attachTo(Window w) {
w.addWindowListener(build());
}
}
this.addWindowListener(new ListenerBuilder()
.opened(e -> log.info("Window is opened!"))
.closed(e -> System.exit(0)).build());
这里不能直接使用lambdas,但如果您有大量侦听器的UI代码,则可以创建如下生成器:
this.addWindowListener(new WindowAdaptor(){
public void windowClosing(WindowEvent we)
{
System.exit(0);//to close the window
}
});
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.function.Consumer;
public class ListenerBuilder {
static final Consumer<WindowEvent> nullConsumer = e -> {};
private Consumer<WindowEvent> opened = nullConsumer;
private Consumer<WindowEvent> closing = nullConsumer;
private Consumer<WindowEvent> closed = nullConsumer;
private Consumer<WindowEvent> iconified = nullConsumer;
private Consumer<WindowEvent> deiconified = nullConsumer;
private Consumer<WindowEvent> activated = nullConsumer;
private Consumer<WindowEvent> deactivated = nullConsumer;
public ListenerBuilder opened(Consumer<WindowEvent> opened)
{ this.opened = opened; return this; }
public ListenerBuilder сlosing(Consumer<WindowEvent> closing)
{ this.closing = closing; return this; }
public ListenerBuilder closed(Consumer<WindowEvent> closed)
{ this.closed = closed; return this; }
public ListenerBuilder iconified(Consumer<WindowEvent> iconified)
{ this.iconified = iconified; return this; }
public ListenerBuilder deiconified(Consumer<WindowEvent> deiconified)
{ this.deiconified = deiconified; return this; }
public ListenerBuilder activated(Consumer<WindowEvent> activated)
{ this.activated = activated; return this; }
public ListenerBuilder deactivated(Consumer<WindowEvent> deactivated)
{ this.deactivated = deactivated; return this; }
public WindowListener build() {
return new WindowListener() {
public void windowOpened(WindowEvent e) {
opened.accept(e);
}
public void windowIconified(WindowEvent e) {
iconified.accept(e);
}
public void windowDeiconified(WindowEvent e) {
deiconified.accept(e);
}
public void windowDeactivated(WindowEvent e) {
deactivated.accept(e);
}
public void windowClosing(WindowEvent e) {
closing.accept(e);
}
public void windowClosed(WindowEvent e) {
closed.accept(e);
}
public void windowActivated(WindowEvent e) {
activated.accept(e);
}
};
}
public void attachTo(Window w) {
w.addWindowListener(build());
}
}
this.addWindowListener(new ListenerBuilder()
.opened(e -> log.info("Window is opened!"))
.closed(e -> System.exit(0)).build());
或者像这样:
this.addWindowListener(new WindowAdaptor(){
public void windowClosing(WindowEvent we)
{
System.exit(0);//to close the window
}
});
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.function.Consumer;
public class ListenerBuilder {
static final Consumer<WindowEvent> nullConsumer = e -> {};
private Consumer<WindowEvent> opened = nullConsumer;
private Consumer<WindowEvent> closing = nullConsumer;
private Consumer<WindowEvent> closed = nullConsumer;
private Consumer<WindowEvent> iconified = nullConsumer;
private Consumer<WindowEvent> deiconified = nullConsumer;
private Consumer<WindowEvent> activated = nullConsumer;
private Consumer<WindowEvent> deactivated = nullConsumer;
public ListenerBuilder opened(Consumer<WindowEvent> opened)
{ this.opened = opened; return this; }
public ListenerBuilder сlosing(Consumer<WindowEvent> closing)
{ this.closing = closing; return this; }
public ListenerBuilder closed(Consumer<WindowEvent> closed)
{ this.closed = closed; return this; }
public ListenerBuilder iconified(Consumer<WindowEvent> iconified)
{ this.iconified = iconified; return this; }
public ListenerBuilder deiconified(Consumer<WindowEvent> deiconified)
{ this.deiconified = deiconified; return this; }
public ListenerBuilder activated(Consumer<WindowEvent> activated)
{ this.activated = activated; return this; }
public ListenerBuilder deactivated(Consumer<WindowEvent> deactivated)
{ this.deactivated = deactivated; return this; }
public WindowListener build() {
return new WindowListener() {
public void windowOpened(WindowEvent e) {
opened.accept(e);
}
public void windowIconified(WindowEvent e) {
iconified.accept(e);
}
public void windowDeiconified(WindowEvent e) {
deiconified.accept(e);
}
public void windowDeactivated(WindowEvent e) {
deactivated.accept(e);
}
public void windowClosing(WindowEvent e) {
closing.accept(e);
}
public void windowClosed(WindowEvent e) {
closed.accept(e);
}
public void windowActivated(WindowEvent e) {
activated.accept(e);
}
};
}
public void attachTo(Window w) {
w.addWindowListener(build());
}
}
this.addWindowListener(new ListenerBuilder()
.opened(e -> log.info("Window is opened!"))
.closed(e -> System.exit(0)).build());
关于lambda和功能接口的简短参考:关于lambda和功能接口的简短参考:类似问题:类似问题:这种设计的缺点是,如果一个方法被多次调用,它会自动覆盖处理程序。此外,生成的侦听器通过构建器实例引用处理程序,这使得它是可变的,并且构建器模式的整个使用毫无意义。Lombok可以为您生成一个生成器。自动生成的生成器修复了所有提到的缺点。这种设计的缺点是,如果一个方法被多次调用,它会自动覆盖处理程序。此外,生成的侦听器通过构建器实例引用处理程序,这使得它是可变的,并且构建器模式的整个使用毫无意义。Lombok可以为您生成一个生成器。自动生成的生成器修复了所有提到的缺点。