I wonder if there is a way to reuse a code for multiple jbuttons by changing the text of multiple labels. here is part of the code:
petrificarSetaEsquerda = new JButton("");
petrificarSetaEsquerda.addActionListener(actionListener);
petrificarSetaEsquerda.setBounds(39, 58, 15, 15);
petrificarSetaEsquerda.setBorder(emptyBorder);
petrificarSetaEsquerda.setIcon(new ImageIcon(Mago.class.getResource("/mainPackage/left_arrow.png")));
contentPane.add(petrificarSetaEsquerda);
petrificarSetaDireita = new JButton("");
petrificarSetaDireita.addActionListener(actionListener);
petrificarSetaDireita.setBorder(emptyBorder);
petrificarSetaDireita.setIcon(new ImageIcon(Mago.class.getResource("/mainPackage/right_arrow.png")));
petrificarSetaDireita.setBounds(68, 58, 15, 15);
contentPane.add(petrificarSetaDireita);
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == petrificarSetaDireita){
if (contadorPetrif <= 10 && contadorTotal > 0 ) {
lblPetrif.setText(String.format("%d", contadorPetrif++));
lblTotal.setText(String.format("Total : %d", contadorTotal--));
}
}
if(e.getSource() == petrificarSetaEsquerda){
if (contadorPetrif > 0 && contadorTotal > 0) {
lblPetrif.setText(String.format("%d", contadorPetrif--));
lblTotal.setText(String.format("Total : %d", contadorTotal++));
}
}
}
};
The code is running normal, (taking a little trouble I'm going to look at later) the focus here is if you guys can give me a hint on how to reuse the code correctly In the program are 13Labels, and a left arrow to decrease and a right arrow to increase ... If you pay attention, the code will always be exactly the same to increase and decrease the number in the labels, just change the labels ... If it does not have to write 26 if followed ... it does not seem right to put so much code repeated like this
Does anyone have an idea? Full Code Here: link