I am new to Java and trying to create a button through my class and it has a method with arguments. But when I create two instances of my class, it shows only one button i.e., the latest one. Could you tell me what mistake I have done here?
my class file
public class CreateButton {
int posx;
int posy;
int buttonWidth;
int buttonHeight;
public void newButton(int x, int y, int w, int h) {
posx = x;
posy = y;
buttonWidth = w;
buttonHeight = h;
JFrame f = new JFrame();
JButton b = new JButton("Test");
b.setBounds(posx, posy, buttonWidth, buttonHeight);
f.add(b);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
my file
class Project {
public static void main(String[] args) {
CreateButton myButton1 = new CreateButton();
CreateButton myButton2 = new CreateButton();
myButton1.newButton(50, 200, 100, 50);
myButton2.newButton(100, 250, 100, 50);
}
}