xxxxxxxxxx
public JButton makeButton(JFrame frame, String text, Runnable runnable) {
JButton button = new JButton();
button.setText(text);
frame.add(button);
button.addActionListener(event -> {
frame.setVisible(false);
runnable.run();
});
return button;
}
//Which you would use like this:
JButton button1 = makeButton(frame, "button 1", () -> {
// TODO action for button 1
});
JButton button2 = makeButton(frame, "button 2", () -> {
// TODO action for button 2
});