Newbie here.
I'm trying to create a frame which has a Black panel to create some animations in it, and a ui panel to put the control buttons. But when i create JButtons and add them to ui panel, they seem outside of ui panel.
I think it's because of layouts, but i cannot decide which layout fits best for my situation. Tried GridLayout, GridBoxLayout, BoxLayout...none of them seems okay and i'm pretty sure i'm missing something.
Oracle documents are very rich, but i cannot say it helped me a lot, having trouble to understand layouts.
Here's my code: I edited my code, thanks to Berger's answer, but still, having the trouble, so i updated the code and screenshots to be clear.
Never mind, i forgot fixing some part, Berger's answer is perfeclty working. Thanks!
import java.awt.*;
import javax.swing.*;
public class gui {
public static void InitializeGUI(){
//DEFINE MAIN FRAME
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(){
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
return new Dimension(window.getContentPane().getSize().width, 3 * window.getSize().height / 4);
}
};
universe.setBackground(Color.BLACK);
//DEFINE UI PANEL
JPanel ui = new JPanel() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
return new Dimension(window.getContentPane().getSize().width, window.getSize().height / 4);
}
};
//DEFINE BUTTONS
JButton femalespawnbutton = new JButton("Spawn Female");
femalespawnbutton.setSize(150, 100);
JButton malespawnbutton = new JButton("Spawn Male");
malespawnbutton.setSize(150, 100);
//FILL
ui.add(femalespawnbutton, BorderLayout.CENTER);
ui.add(malespawnbutton, BorderLayout.CENTER);
window.add(universe, BorderLayout.NORTH);
window.add(ui, BorderLayout.SOUTH);
window.setVisible(true);
}
}