更有效的制作 JButtons 的方法

More Effective way of making JButtons

我想知道是否有更简单、更有效的方法来制作 JButton。我的设计看起来像这样,但这样做根本不实用。在有人建议使用 JButtons 以外的东西之前,我是不允许的。谢谢

由于只有几个属性不同(主要是位置和大小),您可以创建一个工厂方法并传入不同的属性。

private JButton createJButton(String label, int xcoordinate, int ycoordinate, int width, int height, ActionListener actionListener) {

    JButton button = new JButton(label);
    button.setBounds(xcoordinate, ycoordinate, width, height);
    button..setBackground(new Color(0x32BD12));
    button.setForeground(Color.BLACK);
    button.setFont(new Font("arial", Font.BOLD, 40))
    button.addActionListener(actionListener);
    button.setBorder(new EmptyBorder(1, 1,1,1));

    return button;
}

这将有助于摆脱一些样板代码。