首先,我们必须确定我们想要解决的问题。最大的问题是创建简单的事件绑定,这需要在以前的 Java 版本中使用内部类。在大多数情况下,这可以替换为单方法侦听器接口的 lambda 表达式,对于多方法接口,您需要适配器,如 this 或 that 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,我们仍然需要使用一个变量来表达方法调用的目标来改变属性,但是在通过lambda表达式实现Consumer时,这些变量可以使用类型推断声明为name ->这样简单。此外,变量的范围会自动限制在初始化期间。
使用这个起点,您可以为经常使用的组件和/或经常使用的属性添加专门的方法,以及已经提到的多方法侦听器的工厂方法。例如
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;
}
但我认为,图片是清晰的。