I will make a java game similar to cookie clickers, but is seems that I can't put a variable as a text in the button.
This is what is causing the problems, because the variable isn't a string(I cut out the other part of the code, because it's not important for now):
import javax.swing.JFrame;
import javax.swing.JButton;
public class Frame {
public static int num1 = 0;
public static void main(String[] args){
JFrame f = new JFrame("Cookie Clicker");
JButton b1 = new JButton(num1);
f.setSize(500, 300);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(b1);
}
}
As you can see there's the num1 variable in there and it won't let it to be there. Any ideas how to make it work?


intnumber to aString, e.g.new JButton(String.valueOf(num1))JButtonfunction ispublic JButton(String text, Icon icon). Whereiconis provided by default if not passed. But the text should beString. You can donew JButton(""+num1);