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