Search code examples
javaswinglayout-managerborder-layoutflowlayout

Border and Flow Layout doesn't show two windows?


I created two method with parameters one as BorderLayout and other as FlowLayout and each method has its own frame.

But only one window popups with mix layout.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JFrame;

public class BLayOut extends JFrame
{
private JFrame fr,fr2;
private JLabel label,label2,label3;

public void win(BorderLayout bl)

{
fr =new JFrame("BorderLayout");

setSize(300,200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);


setLayout(bl); 

label= new JLabel("Label 1");
label2 = new JLabel("Label 2");
label3 = new JLabel("Label 2");
add(label,BorderLayout.NORTH);
add(label2,BorderLayout.SOUTH);
add(label3,BorderLayout.CENTER);

}

public void win(FlowLayout fl)
{
fr2 =new JFrame("FlowLayout");
setSize(500,200);
setVisible(true);
setLocation(300, 0);
setDefaultCloseOperation(EXIT_ON_CLOSE);


setLayout(fl); 

label= new JLabel("Label 1");
label2 = new JLabel("Label 2");
label3 = new JLabel("Label 3");
add(label);
add(label2);
add(label3);

}


}

class BLayOutMain
{
    public static void main (String args [])
    {
        BLayOut bl = new BLayOut();
        bl.win(new BorderLayout());
        bl.win(new FlowLayout());
    }
}

Solution

  • You're mixing up your references...

    First, you create a class that extends from JFrame...

    public class BLayOut extends JFrame {
    

    Then you declare two instance variables of JFrame...

    private JFrame fr, fr2;
    

    Then in your methods, you create an instance of JFrame and assign it to one of these variables and promptly ignore them...

    fr = new JFrame("BorderLayout");
    
    // Which frame are you modifying now...??
    setSize(300, 200);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    setLayout(bl);
    
    label = new JLabel("Label 1");
    label2 = new JLabel("Label 2");
    label3 = new JLabel("Label 2");
    add(label, BorderLayout.NORTH);
    add(label2, BorderLayout.SOUTH);
    add(label3, BorderLayout.CENTER);
    

    Basically, what this is doing is setting the properties of the instance of BLayOut and not fr or fr2.

    Start by dropping the extends JFrame from BLayOut, it's confusing the issue, this will generate a list of compiler errors where the methods cannot be found. These can be fixed by using either fr or fr2, depending on the method...

    fr = new JFrame("BorderLayout");
    
    // Which frame are you modifying now...??
    fr.setSize(300, 200);
    fr.setVisible(true);
    fr.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    fr.setLayout(bl);
    
    fr.label = new JLabel("Label 1");
    fr.label2 = new JLabel("Label 2");
    fr.label3 = new JLabel("Label 2");
    fr.add(label, BorderLayout.NORTH);
    fr.add(label2, BorderLayout.SOUTH);
    fr.add(label3, BorderLayout.CENTER);
    

    You really should only call setVisible when you are ready to display the initialized UI

    fr = new JFrame("BorderLayout");
    //...
    fr.setVisible(true);
    

    This way, your UI will show up without having the need to revalidate the frame in some way...