b mmm
b mmm

Reputation: 11

JLabel does not appear on JFrame

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);
    }
}

}

Upvotes: 0

Views: 1260

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168845

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.

Upvotes: 1

Mona Wade
Mona Wade

Reputation: 192

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

Upvotes: 0

Himanshu sandha
Himanshu sandha

Reputation: 11

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

Upvotes: 0

Related Questions