0

Here is some of my code. It should be noted that this is not my exact code as I did have much more added, but long story short, I can guarantee that none of the controls touch each other, and that they are all added to this frame. Where then is my problem. It should be noted that I am not highly experienced with this, and as such need specific help. I don't see why these are giving me problems, such as buttons being too big and out of place, but I need them in the exact location which I specify at the exact size, and never disappearing or flickering. What can I do?

public class TesterFrame extends JFrame{
    public TesterFrame(){
        setSize(500,500);
        JButton jb = new JButton("Button");
        jb.setLocation(100, 100);
        jb.setSize(20,20);
        JCheckBox jcb = new JCheckBox("CheckBox");
        jcb.setLocation(20,20);
        jcb.setSize(30,30);
        add(jb); add(jcb);
        JButton jb2=new JButton("BUTTON");
        jb2.setLocation(60,60);
        jb2.setSize(30,30);
        add(jb2);
        setVisible(true);
    }
}

1 Answers1

1

JFrame uses a BorderLayout by default, which will only allow a single component to be position at any of it's five available slots.

You're basically adding all the components to the BorderLayout.CENTER position, but only the last is actually be laid out.

See How to Use BorderLayout for more details

Try calling setLayout(new FlowLayout()); before you start adding components.

Take a look at Laying Out Components Within a Container for more details and a list of other layout managers which will help you

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366