I am trying to make JPanels that add components vertically instead of horizontally but I can't get it to work. I used new GridLayout(0,1) and it works for my JFrame so I am confused as to why it won't work for the JPanel as well. My code is posted below
import java.awt.*; import java.awt.event.*; import javax.swing.*; class GUIClass{ GUIClass(){ JFrame jfrm = new JFrame("Testing"); jfrm.setLayout(new GridLayout(0,1)); jfrm.setSize(500,500); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Ballot[] ballotArray = new Ballot[3]; for(int i=0;i<2;i++){ ballotArray[i] = new Ballot("Button "+i); jfrm.add(ballotArray[i]); } jfrm.setVisible(true); } }
import java.io.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class Project4 extends JFrame{ public static void main(String []args) throws IOException{ SwingUtilities.invokeLater(new Runnable() { public void run() { new GUIClass(); } }); } }
Any help is appreciated.
Thanks