首先,我们必须确定我们想要解决哪些问题。最大的问题是创建简单的事件绑定,这需要在以前的 Java 版本中使用内部类。在大多数情况下,这可以用单方法侦听器接口的 lambda 表达式替换,对于多方法接口,您需要适配器,就像在这个或那个Q&A 中讨论的那样,但这只能完成一次。
另一个问题是初始化代码的结构。原则上,命令式代码可以正常工作,但是如果您想修改要添加到容器中的组件的某些属性,则必须更改container.add(new ComponentType());
以引入新的局部变量以供后续语句使用。此外,添加到容器本身需要将其保存在变量中。虽然 Java 允许使用花括号限制局部变量的范围,但结果仍然很笨拙。
这是最好的起点。如果我们使用,例如
public class SwingBuilder {
public static <T> T build(T instance, Consumer<T> prepare) {
prepare.accept(instance);
return instance;
}
public static <T extends Container> T build(
T instance, Consumer<T> prepare, Component... ch) {
return build(build(instance, prepare), ch);
}
public static <T extends Container> T build(T instance, Component... ch) {
for(Component c: ch) instance.add(c);
return instance;
}
}
这些简单的泛型方法已经非常强大,因为它们可以组合。使用import static
,一个使用站点可以看起来像
JFrame frame = build(new JFrame("Example"),
f -> {
f.getContentPane().setLayout(
new BoxLayout(f.getContentPane(), BoxLayout.LINE_AXIS));
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
},
build(new JLabel("\u263A"), l -> l.setFont(l.getFont().deriveFont(36f))),
Box.createHorizontalStrut(16),
build(new JPanel(new GridLayout(0, 1, 0, 5)),
new JLabel("Hello World"),
build(new JButton("Close"), b -> {
b.addActionListener(ev -> System.exit(0));
})
)
);
frame.pack();
frame.setVisible(true);
与 Groovy 相比,我们仍然必须使用变量来表示方法调用的目标以更改属性,但是在实现via lambda 表达式name ->
时,可以像使用类型推断一样简单地声明这些变量。Consumer
此外,变量的范围会自动限制在初始化的持续时间内。
使用这个起点,您可以为经常使用的组件和/或经常使用的属性添加专门的方法,以及已经提到的多方法侦听器的工厂方法。例如
public static JFrame frame(String title, Consumer<WindowEvent> closingAction,
Consumer<? super JFrame> prepare, Component... contents) {
JFrame f = new JFrame(title);
if(closingAction!=null) f.addWindowListener(new WindowAdapter() {
@Override public void windowClosing(WindowEvent e) {
closingAction.accept(e);
}
});
if(prepare!=null) prepare.accept(f);
final Container target = f.getContentPane();
if(contents.length==1) target.add(contents[0], BorderLayout.CENTER);
else {
target.setLayout(new BoxLayout(target, BoxLayout.PAGE_AXIS));
for(Component c: contents) target.add(c);
}
return f;
}
但我认为,情况很清楚。