SuperBrea
SuperBrea

Reputation: 1

Cardlayout on a null layout jframe

Whenever I change my JFrame's layout to null, instead of actually setting it, the JPanels never get added. I'm not sure why and I haven't found any sufficient information on this topic. Here's My JFrame class:

  public class Frame extends JFrame
{
public Frame () {
    super("frame");
    this.setLayout(null);
    this.setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getProtectionDomain().getClassLoader().getResource("icon.png")));
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setSize(1250, 550);
    this.getContentPane().setBackground(Color.GRAY);
}

}

Here's where I instantiate the JFrame:

public static Frame frame;
public static void initialize()
{
    frame = new Frame();
    CardLayout cardLayout = new CardLayout();
    PanelContainer panelContainer = new PanelContainer(cardLayout);

    panelContainer.setLayout(cardLayout);

    JPanel panel1 = new Panel1();
    JPanel panel2 = new Panel2();

    panelContainer.add(panel1, "panel1");
    panelContainer.add(panel2, "panel2");

    cardLayout.show(panelContainer, "panel1");

    gui.add(panelContainer, null);

    gui.validate();
    gui.setVisible(true);
}

Here's the panel container class:

public class PanelContainer extends JPanel
{
public PanelContainer(CardLayout cardLayout)
{
    super(cardLayout);
    this.setVisible(true);
  }
}

And finally here is panel 1 and 2, they have the same code:

 public class Panel1 extends JPanel
  {
   public Panel1 ()
   {
    super();
    this.setBackground(Color.GRAY);
    addButtons(this);
}
   public static void addButtons(FrontPanel panel)
{
    JButton testButton = new JButton(new ImageIcon(Images.TestImage);
    testButton (30,30);
    testButton (0,0);
    panel.addtestButton 
}
}

Thank you for your time and assistance!

Upvotes: 0

Views: 301

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Several issues:

  • Whenever you give a container a null layout, you the coder become completely responsible for specifying the size and location of all added components.
  • If you don't do this, the components added have 0 size.
  • Having said this, you should almost never use a null layout since it leads to GUI's that are inflexible and almost impossible to upgrade and debug.
  • Your code also shows a gross over-use of static everything, suggesting that you may do well to study Java OOPs concepts before trying to create complex GUI's. Creating static methods and fields leads to code that is difficult to enhance, that can't be inherited and is difficult to test including unit testing.

As an aside, I don't see where you add anything to the JFrame. And you appear to be using a variable, gui, that is never declared or initialized. Your Frame variable is named frame -- could gui and frame be one and the same thing? Did you mistranscribe your code for us?


Edit

...and as for the overuse of static "everything"; I'm new to java and I'm trying to get better at the use of static, the problem is I don't entirely know the meaning of it.

This is why I strongly urge you to study Java object-oriented programming concepts before trying to create a complex GUI. Otherwise you risk placing the cart before the horse.

Another thing is I don't use a layout because I want alot of control over everything,

Which is exactly what the layout managers do for you. Your thinking that null layout works better marks you as a newbie who has not yet grasped the power of the layout managers. Once you do, you'll understand why most similar questions about null layouts on this site get responses similar to mine.

looking in the oracle docs I didn't find a layout that would let me do that. Maybe you could suggest something for me to use?

You usually don't use just one layout. I often nest JPanels, each one using a programmer-friendly layout manager, and in this way am able construct pleasing and easy to adjust and enhance GUI's. As for recommendations -- I don't know how your program should look, so I'm not able to give any specific recs at this time.

Luck!

Upvotes: 5

Related Questions