im having trouble getting the jbuttton to plus 1 number it keeps returning just one in the printout, i want to create a method that is a base method for other jbuttons which will all max out at 10 and print out the value onto a label using a settext but i cant figure out why the amount stays at 1 every click
package calculator;
import javax.swing.JOptionPane;
/**
*
* @author Dominic
*/
public class calc extends javax.swing.JFrame {
int clicked;
public int method() {
if (clicked < 10)
clicked++;
else {
JOptionPane.showMessageDialog(null, "maxed");
}
System.out.println(clicked);
return clicked ;}
public calc() {
initComponents();
}
private void jbtn12ActionPerformed(java.awt.event.ActionEvent evt) {
calc newcalc = new calc();
newcalc.method();
}
Make clicked public static. every time you are making a new instance of calc and that initializes 'clicked' with zero every time;
Every time you call jbtn12ActionPerformed
, you create a new calc, and increase and print the value of that new calc.
There are different approaches that might help you:
Make the variable clicked of type static . Just changing this will solve your problem, but I don't believe it's the right way to do so.
Make the variable clicked of type static . AND make the method method static, and call it instead of what you are doing (change the actionPerformed method to:
calc.method();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.