1

When I put the mainframe layout equal to null and run the app, it show nothing, but if I put it equal to flowlayout () it shows the labels. I don't want to use flowlayout as I want to set bounds and specify the coordinates of each components. What is the problem in the following code?

public class pr8 extends JFrame 
{    

  Font font;
  Map attributes;

  JPanel MainFrame;
  JPanel PicFrame;

  JLabel MainTitle;

  JLabel Name;
  JTextField NameInput;

  public pr8 (String Title)
  { 
    super (Title);

    MainFrame = new JPanel ();
    this.add (MainFrame);
    MainFrame.setVisible (true);
    MainFrame.setLayout (null);

    MainTitle = new JLabel("Student Entry Form");
    MainFrame.add (MainTitle);
    MainTitle.setFont(new Font("Serif", Font.PLAIN, 24));
    font = MainTitle.getFont();
    attributes = font.getAttributes();
    attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
    MainTitle.setFont(font.deriveFont(attributes));
    MainTitle.setBounds(400, 50, 200, 30);

    Name = new JLabel ("Enter your name");
    MainFrame.add (Name);
    Name.setFont(new Font("Serif", Font.PLAIN, 24));
    Name.setBounds (100, MainTitle.getY () + 50, 30, 30 );
    NameInput = new JTextField ("Name");
    MainFrame.add (NameInput);
    NameInput.setBounds(Name.getX() + 30, Name.getY(), 60, 30);
   }
 }
TheGrimBoo
  • 199
  • 1
  • 4
  • 14
  • Do you have a JFrame? You need a JFrame to contain a JPanel – Dan Dec 12 '15 at 19:17
  • Please post an [SSCCE](http://sscce.org). `MainTitle` and `attributes` are not defined. – markspace Dec 12 '15 at 19:18
  • @Dan yes the class itself extends JFrame – TheGrimBoo Dec 12 '15 at 19:23
  • @markspace they are defined outside the constructor of the class, the code I posted is written inside the class constructor – TheGrimBoo Dec 12 '15 at 19:25
  • @HwangHeeRa_Gogo Can you see the window (JFrame) when you run? Or are you seeing an empty window? – user3437460 Dec 12 '15 at 19:29
  • @user3437460 the JFrame is visible with its title, but the inner content are not visible. – TheGrimBoo Dec 12 '15 at 19:37
  • `setVisible` should be at the end. Also please avoid the use of null Layout, Swing was desgined to use [Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) Or combinations of them. For more about this please check [Null Layout is Evil](http://www.fredosaurus.com/notes-java/GUI/layouts/nulllayout.html) and [Why is it frowned upon to use a null layout in Swing?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing). You're setting the null layout after making it visible, so this change is not taken. – Frakcool Dec 12 '15 at 19:57
  • Also please check [Java Naming Conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html). Your variable names should start with lower case while your classes names should start with an upper case, this will improve the readability of your code to you and others who read it – Frakcool Dec 12 '15 at 19:58
  • @HwangHeeRa_Gogo I think I know what is going on. Take a look at my solution below. – user3437460 Dec 12 '15 at 20:14
  • 1
    @HwangHeeRa_Gogo, `I don't want to use flowlayout as I want to set bounds and specify the coordinates of each components` Why? Why are you reinventing the wheel. The point of using a layout manager is to set the size/location of a components so you don't need to do all the calculations manually. Use Swing the way it was designed to be used (for too many reasons to mention here) and use layout managers. You are not force to used the FlowLayout. – camickr Dec 12 '15 at 20:16
  • @user3437460 I can't see your solution, did you post it as an answer or what ? – TheGrimBoo Dec 12 '15 at 20:18
  • @HwangHeeRa_Gogo Just posted, take a look and try it. Let me know whether it works. It should work. – user3437460 Dec 12 '15 at 20:23

1 Answers1

3

If you set the layout of your JPanel as null, you may have to set the size of your JPanel:

panel.setPreferredSize(new Dimension(400, 300));
//In your case: mainFrame.setPreferredSize( /*your dimension*/ );

Also ensure you invoke pack() on your JFrame and your JFrame's layout is not null.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • @HwangHeeRa_Gogo According to my experience, this is what caused your JPanel invisible. Try it and let me know the outcome. – user3437460 Dec 12 '15 at 20:19