由于只有几个属性是不同的(主要是位置和大小),因此您可以创建一个工厂方法并传递不同的属性。
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;
}
这将有助于摆脱一些样板代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句