I am trying to add buttons to a Frame which I am trying in two ways.
Changing the Layout of
JPanel
and then adding buttons directly to the panel. (Commented section in below code). Then I am adding the panel to a frame. This approach worked and it shows buttons in aJFrame
.Creating a
BorderLayout
, adding buttons usingaddLayoutComponents()
method. Then adding thisbl
(BorderLayout
reference) to the panel and then JFrame. Why is this approach not showing buttons in the frame? Where did I go wrong?
Can anyone help me in learning AWT components? I mean what to read first and sequence of concepts .
jf = new JFrame();
jp= new JPanel(new BorderLayout());
/*jp.add(new JButton("North"), BorderLayout.NORTH);
jp.add(new JButton("South"), BorderLayout.SOUTH);
jp.add(new JButton("East"), BorderLayout.EAST);
jp.add(new JButton("West"), BorderLayout.WEST);
jp.add(new JButton("Center"), BorderLayout.CENTER);
jf.add(jp);
*/
BorderLayout bl = new BorderLayout();
bl.addLayoutComponent(new JButton("North"), BorderLayout.NORTH);
bl.addLayoutComponent(new JButton("South"), BorderLayout.SOUTH);
bl.addLayoutComponent(new JButton("East"), BorderLayout.EAST);
bl.addLayoutComponent(new JButton("West"), BorderLayout.WEST);
bl.addLayoutComponent(new JButton("Center"), BorderLayout.CENTER);
jp.setLayout(bl);
jf.add(jp);