score:1

Accepted answer

the first thing is that you add both panels to window, whose content pane has a borderlayout.

so

window.add(universe);
window.add(ui;

is the same as

window.add(universe, borderlayout.center);
window.add(ui, borderlayout.center);

and both components get added to the same place (the buttons are probably hidden)

to avoid that, you may give the constraints indicating where in the borderlayout you want to add your components, .eg :

window.add(universe, borderlayout.center);
window.add(ui, borderlayout.south);

the second thing is that you are using setsize(), which won't be taken into account except if you have no layout manager .

you may use setpreferredsize instead, its height value will be honored by some layout manager, like borderlayout for the south or north component . :

ui.setpreferredsize(new dimension(1000, 250));

now if you want it to adapt to the window's size, override the method instead :

jpanel ui = new jpanel() {

            @override
            public dimension getpreferredsize() {

                return new dimension(window.getcontentpane().getsize().width, window.getcontentpane().getsize().height / 4);

            }
        };

putting it all together (as the preferred height of the south component is honored, no need in your case to give one to the center component, it will take the remaining space) :

   //define main frame
    final jframe window = new jframe("simulator");
    window.setsize(1000, 1000);
    window.setdefaultcloseoperation(jframe.exit_on_close);
    window.setlocationrelativeto(null);

    //define universe panel
    jpanel universe = new jpanel();
    universe.setbackground(color.black);

    //define ui panel
    jpanel ui = new jpanel() {

        @override
        public dimension getpreferredsize() {

            return new dimension(window.getcontentpane().getsize().width, window.getcontentpane().getsize().height / 4);

        }
    };

    //define buttons
    jbutton femalespawnbutton = new jbutton("spawn female");
    femalespawnbutton.setsize(250, 100);
    jbutton malespawnbutton = new jbutton("spawn male");
    malespawnbutton.setsize(250, 100);

    //fill
    ui.add(femalespawnbutton);
    ui.add(malespawnbutton);
    window.add(universe, borderlayout.center);
    window.add(ui, borderlayout.south);
    window.setvisible(true);

final important note :

despite this approach working in some cases, it should be discouraged to set sizes of the components by yourself (except for top level containers like windows) :

see : should i avoid the use of set(preferred|maximum|minimum)size methods in java swing?

score:0

i am sorry - currently i have not much time - but for now: its a problem with the layoutmanager. you should set one when initializing the jpanel. e.g. new jpanel(new gridlayout(1,2)); -> will work but is not nice ;) further information about layouts:

https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html


Related Query

More Query from same tag