I am using setBounds() method on my button to set the size and location of the button . After running, the size of the button is taken up all the frame. Here's my source code
public class Main {
JFrame frame = new JFrame();
JButton cal = new JButton();
public Main() {
cal.setText("Calculate");
cal.setBounds(150, 50, 100, 100);
cal.addActionListener(e -> System.out.println("Calculate"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Calculation Program");
frame.setSize(1000,1000);
frame.setVisible(true);
frame.add(cal);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Main());
}
}
I would like to know how can I possibly set the size to it actual?
You need to add this line to your code:
Then it works. You need to set the layout to null, because the default value when you init the JFrame is
BorderLayoutand this breaks yousetBoundsinput.