'GridLayout not displaying JButtons
I currently have GridLayout(3,0) and I want to add some buttons. However, nothing shows up when I run my program.
Here is the code (the class extends JFrame):
public void initializeGraphics() {
setLayout(new BorderLayout());
setMinimumSize(new Dimension(1900, 1000));
getContentPane().setLayout(null);
setVisible(true);
createMenu();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void createMenu() {
JPanel menuArea = new JPanel();
menuArea.setLayout(new GridLayout(3,0));
menuArea.setBounds(40,40,740,870);
JButton button1 = new JButton("test1");
JButton button2 = new JButton("test2");
JButton button3 = new JButton("test3");
menuArea.add(button1);
menuArea.add(button2);
menuArea.add(button3);
add(menuArea);
}
Solution 1:[1]
You need to create your UI component before setting visible attributes.
edit your code as:
createMenu();
setVisible(true);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sagun Devkota |
