0
I wonder if there is a way to reuse a code for several jbuttons by changing the text of several Abels. 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 out a little problem I’ll look at later) the focus here is if you 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 Abels, will only change the Abels ... If I don’t have to write 26 if in a row... it doesn’t seem right to put so much repeated code
Anyone have an idea? Whole code here : https://gist.github.com/91c1056d83c607c9e26d314be9285fa3
Do Abels really need to have different names? It can’t be something like "label1, label2,...labelN"?
– user28595
Actually it is not mandatory, I only did it to make it easier to read the code @diegofm ... it seems that you have a solution for me! heheh
– Alan Willian Duarte