Huang Lee
Huang Lee

Reputation: 53

JAVA - trying to make panel visible in frame

import javax.swing.*;

public class CipherGUI{

    public static void main(String args[]){
    try {
     UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    } catch (Exception e) {}
    JFrame cipherGUIFrame = new CipherGUIFrame();
    cipherGUIFrame.setVisible(true);
    }
}

class CipherGUIFrame extends JFrame {
  public CipherGUIFrame() {
    super("Caesar Cipher GUI");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400, 600);

    JLabel inputLabel = new JLabel("Enter numbers below");
    JPanel p1 = new JPanel();
    p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
    p1.add(inputLabel);

  }
}

As you can see, I'm trying to add p1 to my CipherGUIFrame() function, but I can't quite make it appear. What is the function for this and, more importantly, should I be adding all the panels OUTSIDE of the constructor method and instead in the MAIN class? It seems I'm adding them in the correct place inside of the constructor of CipherGUIFrame() because it belongs there. Either way, please help me find a way to make my panel show up! Thank you~

Upvotes: 0

Views: 591

Answers (1)

MByD
MByD

Reputation: 137282

You need to add the panel to the frame:

p1.add(inputLabel);
this.add(p1);

Upvotes: 2

Related Questions