Reputation: 11
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
Reputation: 168845
The layout you seem to be wanting, can be achieved by using a combination of three layouts.
GridLayout
for the buttons.FlowLayout
for the labels.BorderLayout
to constrain the labels to the top, and the buttons in the rest of the available space.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);
}
}
EachWordUpperCaseClass
, firstWordLowerCaseMethod()
, firstWordLowerCaseAttribute
unless it is an UPPER_CASE_CONSTANT
) and use it consistently. Runnable
combined with SwingUtilities.invokeLater(..
achieves that.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).Insets
used for the buttons & the EmptyBorder
to adjust the size of the GUI. Consult the Java docs for each, for details.Upvotes: 1
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
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