1

I am trying to create a basic Java program to help me learn java better so I apologize if this is a noob question but I've been doing a lot of research and just haven't found a solid answer that I can understand

I have heard a lot that it is a bad practice to use multiple JFrames, and that isn't exactly what I want anyway. I want to have a 'home screen' that has a few buttons on it that would each take me to a different view but then of course remove all of what was on the home screen. So should I have my main class like this?:

package buildeditor;

public class BuildEditor {
    public static void main(String[] args) {
        MainFrame main = new MainFrame(400, 400);
    }
}

And then have package buildeditor;

import javax.swing.*;

public class MainFrame extends JFrame {
    public MainFrame(int height, int width){
        setSize(height, width);
        setVisible(true);
    }
}

And then where should I put all of the different panels and then call them individually so that when I call a new one, the old one goes away and this new one takes over the whole JFrame.

  • 1
    1) `setSize(height, width);` Don't guess a width height. Add components (with layout padding and borders for white space) then `pack()` the frame. 2) Don't extend frame when you really just need an instance of one. 3) For swapping views: Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Jan 31 '15 at 20:14
  • You must call the superclass constructor in your MainFrame's constructor by using `super()` on the first line of the constructor – Victor2748 Jan 31 '15 at 20:15
  • @Victor2748: your recommendation makes no sense at all. `super()` is always called by default if a default constructor exists for the super class, and in this case, there is one. – Hovercraft Full Of Eels Jan 31 '15 at 20:20

0 Answers0