0

I've got a task for school and I'm not overly good at Java yet... I'm making a borderlayout frame wherein there will several panels including some that will swap from not-visible to visible and the other way around. For some reason nothing shows when I run the program.

Screen: http://prntscr.com/5r2s6s

public class QuizIT extends JFrame {
    public static JFrame Frame;
    public static Welkom Welkom;
    public static Vraag1 Vraag1; 
    public static Vraag2 Vraag2;
    public static Vraag3 Vraag3;
    public static Vraag4 Vraag4;
    public static Vraag5 Vraag5;
    public static Vraag6 Vraag6;
    public static Punten Punten;
    public static Uitleg Uitleg;


public static void main(String[] args){

    // Frame Eigenschappen
    Frame = new JFrame();
    Frame.setSize(500, 500);
    Frame.setLocation(0, 0);
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame.setTitle("QuizIT | Naesen Technologies");
    Frame.setVisible(true);

}

public QuizIT(){
    // Layout -> BorderLayout
    setLayout(new BorderLayout());

    // Initialiseren Klassen
    Welkom = new Welkom();
    Uitleg = new Uitleg();
    Vraag1 = new Vraag1();
    Vraag2 = new Vraag2();
    Vraag3 = new Vraag3();
    Vraag4 = new Vraag4();
    Vraag5 = new Vraag5();
    Vraag6 = new Vraag6();
    Punten = new Punten();

    // Panel -> Frame
    Frame.add(Welkom, BorderLayout.CENTER);
    Frame.add(Uitleg, BorderLayout.NORTH);
    Frame.add(Vraag1, BorderLayout.CENTER);
    Frame.add(Vraag2, BorderLayout.CENTER);
    Frame.add(Vraag3, BorderLayout.CENTER);
    Frame.add(Vraag4, BorderLayout.CENTER);
    Frame.add(Vraag5, BorderLayout.CENTER);
    Frame.add(Vraag6, BorderLayout.CENTER);        
    Frame.add(Punten, BorderLayout.SOUTH);

    // Layout -> Bounds
    Uitleg.setBounds(0,0,500,100);
    Welkom.setBounds(0,100,500,300);
    Vraag1.setBounds(0,100,500,300);
    Vraag2.setBounds(0,100,500,300);
    Vraag3.setBounds(0,100,500,300);
    Vraag4.setBounds(0,100,500,300);
    Vraag5.setBounds(0,100,500,300);
    Vraag6.setBounds(0,100,500,300);
    Punten.setBounds(0,400,500,100);

    Uitleg.setBackground(Color.YELLOW);
    Welkom.setBackground(Color.YELLOW);

    // Visibility
    Welkom.setVisible(true); 
    Uitleg.setVisible(true);  
    Vraag1.setVisible(false);
    Vraag2.setVisible(false);
    Vraag3.setVisible(false);
    Vraag4.setVisible(false);
    Vraag5.setVisible(false);
    Vraag6.setVisible(false);
    Punten.setVisible(true);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Benjamin Naesen
  • 174
  • 1
  • 10
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) `Frame.add(Vraag4, BorderLayout.CENTER); Frame.add(Vraag5, BorderLayout.CENTER);` A maximum of one component can be added to any single layout constraint in a `BorderLayout`. .. – Andrew Thompson Jan 11 '15 at 08:39
  • .. 3) `Uitleg.setBounds(0,0,500,100);` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 4) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is a `CONSTANT`) and use it consistently. .. – Andrew Thompson Jan 11 '15 at 08:41
  • .. 5) Provide ASCII art or a simple drawing of the layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Jan 11 '15 at 08:43
  • Try adding `QuizIT qIT=new QuizIT();` just after `Frame = new JFrame();` – Spikatrix Jan 11 '15 at 09:55

1 Answers1

1

It might be that that the constructor is being invoked first when the program starts. and after that the main method. That might be why it only loads the window but not any components.

You could try replacing constructor with a method instad and invoke the method on the last line in the main method.

This code worked fine. just replace what i wrote in the method with your code!

public static void main(String[] args){

// Frame Eigenschappen
Frame = new JFrame();
Frame.setSize(500, 500);
Frame.setLocation(0, 0);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setTitle("QuizIT | Naesen Technologies");
Frame.setVisible(true);
methodQuizIT();

}

public static void methodQuizIT(){
 NOW THIS CODE EXECUTES :)

} }

Johan
  • 475
  • 4
  • 17