'JButtons do not appear until Mouse Over

In fact, I figured out the reason. I use a cardlayout. Once I commented out the getPreferredSize method in the classes that instantiate JPanels that were added to the main JPanel(which uses the cardlayout), the problem resolves. I'm using replit. Can someone help me on this issue?

Here's the class that sets the cardlayout. It's also used to paint an image on the first JPanel in the cardlayout.

import java.awt.*;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import javax.swing.*;  
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;  
public class CardLayoutExample3 extends JFrame   
{  
  
// Initializing the value of  
// currCard to 1 .  
private int currCard = 1;  
  
// Declaring of objects  
// of the CardLayout class.  
private CardLayout cObjl;  
  
// constructor of the class  
public CardLayoutExample3()  
{  
  
// Method to set the Title of the JFrame  
setTitle("Card Layout Methods");  
  
// Method to set the visibility of the JFrame  
setSize(500, 500);  
  
// Creating an Object of the "Jpanel" class  
JPanel cPanel = new JPanel();  
  
// Initializing of the object "cObjl"  
// of the CardLayout class.  
cObjl = new CardLayout();  
  
// setting the layout  
cPanel.setLayout(cObjl);  
  
// Initializing the object  
// "jPanel1" of the JPanel class.  
/*JPanel image1 = new JPanel();
BufferedImage img1 = null;
  try {
      img1 = ImageIO.read(new File("ff.jpg"));
  } catch (IOException e) {
      e.printStackTrace();
  }
  Image dimg1 = img1.getScaledInstance(400, 400,Image.SCALE_SMOOTH);
    JLabel picLabel1 = new JLabel(new ImageIcon(dimg1));
 */
 

Mainpage jPanel1 = new Mainpage();  
JPanel image = new JPanel();
BufferedImage img = null;
  try {
      img = ImageIO.read(new File("enemy.jpg"));
  } catch (IOException e) {
      e.printStackTrace();
  }
  Image dimg = img.getScaledInstance(400, 400,Image.SCALE_SMOOTH);
    JLabel picLabel = new JLabel(new ImageIcon(dimg));
 
    image.add(picLabel);

    jPanel1.add(image);

 
JPanel btnPanel = new JPanel();  
  
// Initializing the object  
// "firstButton" of the JButton class.  
JButton firstButton = new JButton("First");  
  
// Initializing the object  
// "nextButton" of the JButton class.  
JButton nextButton = new JButton("->");  
  
// Initializing the object  
// "previousbtn" of JButton class.  
JButton previousButton = new JButton("<-");  
  
// Initializing the object  
// "lastButton" of the JButton class.  
JButton lastButton = new JButton("Last");  
  
// Adding the JButton "firstbutton" on the JPanel.  
btnPanel.add(firstButton);  
  
// Adding the JButton "nextButton" on the JPanel.  
btnPanel.add(nextButton);  
  
// Adding the JButton "previousButton" on the JPanel.  
btnPanel.add(previousButton);  
  
// Adding the JButton "lastButton" on the JPanel.  
btnPanel.add(lastButton);     

// Initializing the object  
// "jPanel2" of the CardLayout class.  
Projectile drawi = new Projectile(); 
 
JPanel jPanel2 = new JPanel();  
 

// Initializing the object  
// "jPanel3" of the CardLayout class.  
JPanel jPanel3 = new JPanel();  
  
// Initializing the object  
// "jPanel4" of the CardLayout class.  
JPanel jPanel4 = new JPanel();  
  
// Initializing the object  
// "jl1" of the JLabel class.  
 
  
// Initializing the object  
// "jLabel2" of the JLabel class.  
JLabel jLabel2 = new JLabel("C2");  
  
// Initializing the object  
// "jLabel3" of the JLabel class.  
JLabel jLabel3 = new JLabel("C3");  
  
// Initializing the object  
// "jLabel4" of the JLabel class.  
JLabel jLabel4 = new JLabel("C4");  
  
// Adding JLabel "jLabel1" to the JPanel "jPanel1".  
  
// Adding JLabel "jLabel2" to the JPanel "jPanel2".  
jPanel2.add(jLabel2);  
  
// Adding JLabel "jLabel3" to the JPanel "jPanel3".  
jPanel3.add(jLabel3);  
  
// Adding JLabel "jLabel4" to the JPanel "jPanel4".  
jPanel4.add(jLabel4);  
 jPanel2.add(drawi); 
// Add the "jPanel1" on cPanel  
cPanel.add(jPanel1, "1");  
  
// Add the "jPanel2" on cPanel  
cPanel.add(jPanel2, "2");  
  
// Add the "jPanel3" on cPanel  
cPanel.add(jPanel3, "3");  
  
// Add the "jPanel4" on cPanel  
cPanel.add(jPanel4, "4");  
  
// Creating an Object of the "JPanel" class  
  
  
// adding firstButton in the ActionListener  
firstButton.addActionListener(new ActionListener()  
{  
public void actionPerformed(ActionEvent ae)  
{  

 
 
btnPanel.setVisible(true);
// using the first cObjl CardLayout  
cObjl.first(cPanel);  
  
// value of currCard is 1  
currCard = 1;  
}  
});  
  
// add lastButton in ActionListener  
lastButton.addActionListener(new ActionListener()  
{  
public void actionPerformed(ActionEvent ae)  
{  

 
 
// using the last cObjl CardLayout  
cObjl.last(cPanel);  
btnPanel.setVisible(true);  
// value of currCard is 4  
currCard = 4;  
}  
});  
  
// add nextButton in ActionListener  
nextButton.addActionListener(new ActionListener()  
{  
public void actionPerformed(ActionEvent ae)  
{  




  
if (currCard < 4)  
{  
  
// increase the value of currCard by 1  
currCard = currCard + 1;  
 btnPanel.setVisible(true); 
// show the value of currCard  
cObjl.show(cPanel, "" + (currCard));  
}  
}  
});  
  
// add previousButton in ActionListener  
previousButton.addActionListener(new ActionListener()  
{  
public void actionPerformed(ActionEvent ae)  
{  
 


if (currCard > 1)   
{  
 btnPanel.setVisible(true); 
// decrease the value  
// of currCard by 1  
currCard = currCard - 1;  
  
// show the value of currCard  
cObjl.show(cPanel, "" + (currCard));  
}  
}  
});  
getContentPane().add(btnPanel, BorderLayout.SOUTH);  
    
// using to get the content pane  
getContentPane().add(cPanel, BorderLayout.NORTH); 
btnPanel.setVisible(true);
}  
// using to get the content pane  

  
 
   
}  

Here's the class that's used to instantiate the first JPanel in cardlayout

import javax.swing.*;  
import java.awt.*;  
import java.awt.event.*; 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Mainpage extends JPanel{
  private int size = 500;
  private int titleSize = 50;
 
  public Mainpage(){  
    this.setSize(500,500);
   
    JLabel title1=new JLabel("Physics Scenarios");
    this.add(title1);   
    this.setVisible(true); 
 
    
            

  }
  @Override
  public Dimension getPreferredSize(){
        return new Dimension(500, 500);
  }
  

} 

Here's the class that instantiates the second JPanel in cardlayout, which paints a tree

import javax.swing.*;  
import java.awt.*;  
class Projectile extends JComponent{
  Tree tree1;
  public Projectile(){  
    this.setSize(500,500);
    tree1  = new Tree(100,100);
    JLabel title1=new JLabel("Physics Scenarios");
    this.add(title1);   
    this.setVisible(true); 
    
            

  }
  
  @Override
  public void paintComponent(Graphics g){
    // Basically says when paintComponent(g) is called, erase and redraw entire
    // JPanel
    super.paintComponent(g);

    // Draw background
     

    // Draw trees
    tree1.drawMe(g);
     
  }
  
    @Override
  public Dimension getPreferredSize(){
        return new Dimension(500, 500);
  }
}

Here's the subclass that contains the method that paints a tree.

// import color and graphics
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;

public class Tree extends JComponent{

    // position variables
    private int x;
    private int y;

    // constructor
    public Tree (int x, int y){
        this.x = x;  //100
        this.y = y;  //250
    }

    // drawMe(g) method
    public void drawMe(Graphics g) {

        // x = 100
        // y = 250

        // Draw tree trunk
        Color trunkBrown = new Color(128, 64, 0);
        g.setColor(trunkBrown);
        g.fillRect(x+30,y+50,40,100);   

        // Draw top green crown of tree
        Color topTreeGreen = new Color(41, 163, 41);
        g.setColor(topTreeGreen);
        g.fillOval(x,y,100,100);
    }

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source