0

I have searched many times to find why my JLabel is not showing up but I cannot figure it out. I tried playing around with the setLayout() but I couldn't figure it out. Everything else is showing up except for my 2 labels. All four buttons with the pictures show up but these do not and I cannot figure out why Do I need to set the location or bounds of them?? Please help! Thanks

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class summativeFile {

public static void main (String[] args){

    //Declaring and setting properties of the JFrame
    JFrame mainFrame = new JFrame("iLearn");
    mainFrame.setVisible(true);
    mainFrame.setSize(1000,800);
    mainFrame.setBackground(Color.white);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Declaring and setting properties of the JPanel
    JPanel panel = new JPanel();
    mainFrame.add(panel);

    //Declaring objects
    Font fontStyle1 = new Font("Arial",Font.PLAIN,25);
    JLabel welcomeLabel = new JLabel("Welcome to iLearn");
    JLabel hi = new JLabel("HI");
    JButton mathButton = new JButton("Math");
    JButton scienceButton = new JButton("Science");
    JButton englishButton = new JButton("English");
    JButton computersButton = new JButton("Computers");

    //Setting attributes of objects
    welcomeLabel.setFont(fontStyle1);
    mathButton.setIcon(new ImageIcon("//Users//ben//IdeaProjects//grade12Summative//src//math.png"));
    scienceButton.setIcon(new ImageIcon("//Users//ben//IdeaProjects//grade12Summative//src//science.png"));
    englishButton.setIcon(new ImageIcon("//Users//ben//IdeaProjects//grade12Summative//src//english.png"));
    computersButton.setIcon(new ImageIcon("//Users//ben//IdeaProjects//grade12Summative//src//computers.png"));

    //Setting bounds of objects
    mathButton.setBounds(150,150,300,200);
    scienceButton.setBounds(550,150,300,200);
    englishButton.setBounds(150,450,300,200);
    computersButton.setBounds(550,450,300,200);

    //Adding objects to panel
    panel.add(hi);
    panel.add(welcomeLabel);
    panel.add(mathButton);
    panel.add(scienceButton);
    panel.add(englishButton);
    panel.add(computersButton);

    mathButton.addActionListener (new mathAction());
    scienceButton.addActionListener (new scienceAction());
    englishButton.addActionListener (new englishAction());
    computersButton.addActionListener (new computersAction());

}
static class mathAction implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        JFrame mathFrame = new JFrame("Mathematics");
        mathFrame.setVisible(true);
        mathFrame.setSize(1000,800);
        JLabel label = new JLabel("Welcome to Mathematics");
        JPanel mathPanel = new JPanel();
        mathFrame.add(mathPanel);
        mathPanel.add(label);
    }
}
static class scienceAction implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        JFrame scienceFrame = new JFrame("Science");
        scienceFrame.setVisible(true);
        scienceFrame.setSize(1000,800);
        JLabel label = new JLabel("Welcome to Science");
        JPanel sciencePanel = new JPanel();
        scienceFrame.add(sciencePanel);
        sciencePanel.add(label);
    }
}
static class englishAction implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        JFrame englishFrame= new JFrame("English");
        englishFrame.setVisible(true);
        englishFrame.setSize(1000,800);
        JLabel label = new JLabel("Welcome to English");
        JPanel englishPanel = new JPanel();
        englishFrame.add(englishPanel);
        englishPanel.add(label);
    }
}
static class computersAction implements ActionListener {
    public void actionPerformed (ActionEvent e) {
        JFrame computersFrame = new JFrame("Computers");
        computersFrame.setVisible(true);
        computersFrame.setSize(1000,800);
        JLabel label = new JLabel("Welcome to Computers");
        JPanel computersPanel = new JPanel();
        computersFrame.add(computersPanel);
        computersPanel.add(label);
    }
}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
b mmm
  • 11
  • 5
  • Which labels? The main frame labels or your secondary frame labels? Did you try setting their bounds? – AJNeufeld Dec 10 '17 at 23:57
  • 1
    As a rule, you cannot modify the Swing UI components when not on the Event Dispatching Thread (EDT). So your main function should actually start with a `SwingUtilities.invokeLater(...)`, which does a switch to the EDT, and then you would create your main UI. At the very least, you can create your entire UI unrealized, and call `mainFrame.setVisible(true)` as the very last step. For the best compatibility, do both. – AJNeufeld Dec 11 '17 at 00:05
  • 3
    In future questions, simplify your problem code to the absolute minimum. If the problem is with the mainframe layout, get rid of all `ActionListener` code. Is it problem font dependent? No? Get rid of all font code. The simpler you make your [mcve], the more likely you are to (a) identify the problem yourself, (b) attract interested eyeballs. If you provide a wall of unnecessary code, a person who may have otherwise been able to help may lose interest — or write these comments instead of looking for the issue. – AJNeufeld Dec 11 '17 at 00:12
  • 2
    Take `mainFrame.setVisible(true);` and make it the last thing you call – MadProgrammer Dec 11 '17 at 00:13
  • 1
    Also, consider reducing the number of frames you have and consider using use a `CardLayout` instead – MadProgrammer Dec 11 '17 at 00:13
  • The JLabels in the main method @AJNeufield – b mmm Dec 11 '17 at 00:28
  • 1
    @AJNeufeld, `Did you try setting their bounds?` - you should not be setting the bounds of any component, that is the job of the layout manager. – camickr Dec 11 '17 at 03:41

3 Answers3

1

The layout you seem to be wanting, can be achieved by using a combination of three layouts.

  1. A GridLayout for the buttons.
  2. A FlowLayout for the labels.
  3. A BorderLayout to constrain the labels to the top, and the buttons in the rest of the available space.

enter image description here

This is the fixed code. See further tips below.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class SummativeFile {

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                //Declaring and setting properties of the JFrame
                JFrame mainFrame = new JFrame("iLearn");
                mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JPanel contentPane = new JPanel(new BorderLayout(5, 20));
                contentPane.setBorder(new EmptyBorder(20, 20, 20, 20));

                mainFrame.setContentPane(contentPane);
                //Declaring and setting properties of the JPanel
                JPanel panel = new JPanel();
                contentPane.add(panel, BorderLayout.PAGE_START);

                //Declaring labels
                JLabel welcomeLabel = new JLabel("Welcome to iLearn");
                JLabel hi = new JLabel("HI");

                //Adding objects to panel
                panel.add(hi);
                panel.add(welcomeLabel);

                //Declaring buttons and their container
                JPanel buttonMenuPanel = new JPanel(new GridLayout(2, 0, 20, 20));
                JButton mathButton = new JButton("Math");
                JButton scienceButton = new JButton("Science");
                JButton englishButton = new JButton("English");
                JButton computersButton = new JButton("Computers");

                // make the buttons larger
                Insets insets = new Insets(15, 5, 15, 5);
                mathButton.setMargin(insets);
                scienceButton.setMargin(insets);
                englishButton.setMargin(insets);
                computersButton.setMargin(insets);

                buttonMenuPanel.add(mathButton);
                buttonMenuPanel.add(scienceButton);
                buttonMenuPanel.add(englishButton);
                buttonMenuPanel.add(computersButton);

                contentPane.add(buttonMenuPanel, BorderLayout.CENTER);

                mainFrame.pack();
                mainFrame.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(runnable);
    }
}

Other tips:

  1. Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.
  2. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
  3. For best results, the GUI should be constructed and shown on the EDT. The code creating the Runnable combined with SwingUtilities.invokeLater(.. achieves that.
  4. The actions, icons and font are all irrelevent to the layout. Leave them out of the example.
  5. Calling setVisible(true); should be done after all components are added, and pack() is called to lay them out. Packing the container renders setting the size obsolete (pack() will determine the correct size).
  6. Adjust the numbers used in the contructors for the layouts, the Insets used for the buttons & the EmptyBorder to adjust the size of the GUI. Consult the Java docs for each, for details.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

You need to setBounds() for the two labels i.e welcomelabel and hi similar to what you done for the four buttons You have not setBounds() to the lables of the other frames created on click of the buttons and its working because by default JPanel has flowlayout and it is working accordingly

  • *"You need to setBounds() for the two labels.."* Until you understand why setting the bounds of components is inadvisable, please top offering advice on laying out cross-platform GUIs. See point (2) under 'other tips' in my answer, for the beginnings of that understanding. – Andrew Thompson Dec 11 '17 at 05:30
0

If you want to see the JLabel you created on the panel, you need to set the label visible.

So in your code, I would edit the following:

...
//Declaring and setting properties of the JFrame
JFrame mainFrame = new JFrame("iLearn");
mainFrame.setVisible(true);
mainFrame.setSize(1000,800);
mainFrame.setBackground(Color.white);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Declaring and setting properties of the JPanel
JPanel panel = new JPanel();
mainFrame.add(panel);

//Declaring objects
Font fontStyle1 = new Font("Arial",Font.PLAIN,25);
JLabel welcomeLabel = new JLabel("Welcome to iLearn");
welcomeLabel.setVisible(true)
JLabel hi = new JLabel("HI");
hi.setVisible(true);
JButton mathButton = new JButton("Math");
JButton scienceButton = new JButton("Science");
JButton englishButton = new JButton("English");
JButton computersButton = new JButton("Computers");
...

Just how you set the mainFrame visible with true, you should do the same with the a JLabel.

Here are some helpful question asked in the past: Change jLabel Visibility

Mona Wade
  • 192
  • 1
  • 16
  • *"If you want to see the JLabel you created on the panel, you need to set the label visible."* Incorrect. Components are visible by default, once added to a container that is itself visible. – Andrew Thompson Dec 11 '17 at 05:28