#jframes & jpanels

1 messages · Page 1 of 1 (latest)

zinc owl
#

i have

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(900,900);

        JPanel panel = new JPanel();
        JButton button1 = new JButton("Button 1");
        button1.setSize(50, 50);
        panel.add(button1);

        panel.setSize(50, 50);
        panel.setLocation(50, 50);
        frame.add(panel);

        frame.setVisible(true);```
but the button isnt going to the location 50, 50, its at the top center of the gui and it isnt moving no matter what location i set it to go
outer craneBOT
# zinc owl i have ``` JFrame frame = new JFrame("GUI"); frame.setDefaultClos...

Detected code, here are some useful tools:

Formatted code
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 900);
JPanel panel = new JPanel();
JButton button1 = new JButton("Button 1");
button1.setSize(50, 50);
panel.add(button1);
panel.setSize(50, 50);
panel.setLocation(50, 50);
frame.add(panel);
frame.setVisible(true);
#

<@&987246399047479336> please have a look, thanks.

outer craneBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

harsh pulsar
#

Try to change the button position instead of the panel position

#

@zinc owl

zinc owl
#

i tried that same problem

jade nexus
# zinc owl i tried that same problem

That's because the frame you are adding the panel to has a BorderLayout. By default all Components added to said frame are set to the center meaning that they are streched to fit the screen size. The panel you placed your button in also doesn't have a layoutManager and uses FlowLayout with the components placed horizontally in the center and vertically at the top. If you want to use absolute positioning you will have to either set the Layout of the panel to null, then you can move the button freely in the area the panel occupies. Or set the layout of the frame to null to move the panel itself to the area you want it to be. Most of the time you wouldn't use absolute positioning at all and use a concrete layout manager instead but for your purposes this should solve your issue. (NOTE: You can always visually confirm what space a component occupies by giving it a distinct background colour. This might help you when debugging)