0

I just got started learning JFrame and is trying to create a frame containing JLabels and JTextFields frame using grouplayout, but the contents inside my panel aren't appearing when I run the program. All help is appreciated.

    package practice;

import java.awt.*;

import javax.swing.*;


public class Boxc {
    public static void main(String[] args){
        JFrame frame = new JFrame("---------------------(-_-)---------------------"); 
        JLabel headText = new JLabel("Teach Me"); 

        //head text
        headText.setVerticalAlignment(JLabel.TOP);
        headText.setHorizontalAlignment(JLabel.CENTER);
        headText.setFont(headText.getFont().deriveFont(20f));




        frame.setVisible(true); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700, 500);
        frame.setResizable(false);

        frame.setLocationRelativeTo(null); //Center start position
        frame.add(headText);

        //panel 1
        JPanel panel1 = new JPanel();
        GroupLayout layout = new GroupLayout(panel1);
        panel1.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);


        JLabel uInput = new JLabel("When You Type:");
        JTextField uText = new JTextField("Enter Here");
        JLabel iReply = new JLabel("I Reply:");
        JTextField iText = new JTextField("Enter Here");

        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().
                addComponent(uInput).addComponent(iText));
        hGroup.addGroup(layout.createParallelGroup().
                addComponent(uText).addComponent(iText));
       layout.setHorizontalGroup(hGroup);





        frame.add(panel1);
    }}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
X and Y
  • 91
  • 1
  • 7

1 Answers1

0

Java is weird.

It all has to do with the ordering of your JFrame methods. You must ALWAYS do frame.setVisible(true); at the end, preferably as the last line of code in whatever you're using to initiate the JFrame. In that case,

frame.setVisible(true); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(700, 500);
    frame.setResizable(false);

    frame.setLocationRelativeTo(null); //Center start position
    frame.add(headText);

would be changed to

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(700, 500);
    frame.setResizable(false);

    frame.setLocationRelativeTo(null); //Center start position
    frame.add(headText);
    frame.setVisible(true); 

Hope this helps.

EDIT: ignore the changing from/to. Put the frame.setVisible(true); after frame.add(panel1);.

Factorx122
  • 24
  • 5
  • The panel1 section still isn't appearing after I switched the position. Thanks for the advice though. Also, I believe it has to do with the panel1 section itself, as the rest of the code, such as the headText, shows on the frame. – X and Y Mar 13 '19 at 05:49
  • Did you put `frame.setVisible(true);` after `frame.add(panel);`? I'm sure you did, I'm just making sure because your problem sounds a lot like it stems from incorrect placement of the `frame.setVisible(true);`. – Factorx122 Mar 13 '19 at 05:55
  • I tried that, but I was met with a great amount of errors, the first few includes: Exception in thread "main" java.lang.IllegalStateException: javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=When You Type:,verticalAlignment=CENTER,verticalTextPosition=CENTER] is not attached to a vertical group at javax.swing.GroupLayout.checkComponents(Unknown Source) – X and Y Mar 13 '19 at 05:58
  • pretty sure i made some error in my panel1 section, not too familiar with grouplayout. – X and Y Mar 13 '19 at 05:58
  • @X and Y and also, add your `headText` to `panel1` instead of the frame. I'm pretty sure the panel will just cover it up once you get it working. – Factorx122 Mar 13 '19 at 05:59
  • @X and Y oh, I know the problem. you need to call the method `label.setBounds(x, y, width, height);`. Try that. (this method sets the width, height, x, and y position of the JLabel. It seems you have not done that.) (also, `label` that I have referenced is meant to be replaced with your desired label to change the bounds of) – Factorx122 Mar 13 '19 at 06:01
  • For all the labels or just one? Also what would be a recommended bound. – X and Y Mar 13 '19 at 06:17
  • @X and Y all JLabels. X and Y is relative to the top left corner of the JFrame, and the actual JLabel's "middle" is the top left corner of the JLabel as well. so I'd recommend `label.setBounds(0, 0, 50, 50);` for the first one, `label.setBounds(50, 0, 50, 50);` for the second, `label.setBounds(100, 0, 50, 50);` for the third, and `label.setBounds(150, 0, 50, 50);` for the fourth. This is just to give you an idea of the relatives of JLabels to JFrames, from there you can experiment where you want your JLabels and stuff. – Factorx122 Mar 13 '19 at 06:22
  • Ok thanks, I'll keep testing until I get the problem fixed. – X and Y Mar 13 '19 at 06:33
  • @X and Y you might want to also, as I am not the biggest pro at groupLayout, you might want to try and add your group layout to your panel. If that doesn't work, trying `panel1.setLayout(layout);` might also work out. If none of these work, then that's as far as my knowledge goes. I apologize if none of these work. `layout` called in `panel.setLayout(layout);` of course stands for the name of your layout, which I don't quite remember. – Factorx122 Mar 13 '19 at 06:36
  • I changed to gridlayout and got it working, much easier for me. Thank you for your help nonetheless :). – X and Y Mar 13 '19 at 06:38