When I tried to run my program, it is always an error. The 5 JButtons are not appearing at the west (left) of the MainFrame. I used the BoxLayout for the JButtons so that it can be displayed from top to bottom and called the FirstPanel class to the MainFrame so that I can position it on the west side. It is supposed to be like this, but my application is not running. Please help me with how can I achieve this
MainFrame.java
public class MainFrame extends JFrame {
TitlePanel title;
FirstPanel first;
public MainFrame() {
title = new TitlePanel();
add(title, BorderLayout.NORTH);
first = new FirstPanel();
add(first, BorderLayout.WEST);
setSize(5000,5000);
setVisible(true);
this.pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MainFrame();
}
}
FirstPanel.java
public class FirstPanel extends JPanel {
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JButton b5;
FirstPanel fp;
public FirstPanel() {
fp = new FirstPanel();
BoxLayout layout = new BoxLayout(fp, BoxLayout.Y_AXIS);
fp.setLayout(layout);
b1 = new JButton();
b2 = new JButton();
b3 = new JButton();
b4 = new JButton();
b5 = new JButton();
fp.add(b1);
fp.add(b2);
fp.add(b3);
fp.add(b4);
fp.add(b5);
}
}
This is the ERROR that I got
Exception in thread "main" java.lang.StackOverflowError
at java.awt.Component.setFont(Component.java:1907)
at java.awt.Container.setFont(Container.java:1753)
at javax.swing.JComponent.setFont(JComponent.java:2748)
at javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:208)
at javax.swing.plaf.basic.BasicPanelUI.installDefaults(BasicPanelUI.java:66)
at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:56)
at javax.swing.JComponent.setUI(JComponent.java:660)
at javax.swing.JPanel.setUI(JPanel.java:153)
at javax.swing.JPanel.updateUI(JPanel.java:126)
at javax.swing.JPanel.<init>(JPanel.java:86)
at javax.swing.JPanel.<init>(JPanel.java:109)
at javax.swing.JPanel.<init>(JPanel.java:117)
at FirstPanel.<init>(FirstPanel.java:26)
at FirstPanel.<init>(FirstPanel.java:28)