简体   繁体   中英

JButtons in JPanel don't display properly until repainted through resize or action

I've had this problem for a while now, searched many forums and sites (including this one), and still did not find an answer to my question.

This is my problem: I am building a visual calendar. I have a parent panel with multiple panels in it. I repaint the parent panel, and make the 3 overlaying opaque(false). The paint of the parent panel is not showing until I resize the frame (or use the buttons that overlay one of the 3, but those are left out in this example because it makes the code longer)

Anyway, here is the code, I simplified it to the problem part:

public class Calendar extends JPanel{

  public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setSize(1600,150);
    frame.add(new Calendar());
    frame.setVisible(true);
  }

  public Calendar(){
    setLayout(new GridBagLayout());
    GridBagConstraints cc = new GridBagConstraints();
    cc.weightx = 1;
    cc.weighty = 1;
    cc.gridx = 0;
    cc.fill = GridBagConstraints.BOTH;

    //Initiate Panels
    JPanel yearpanel = new JPanel();
    JPanel monthpanel = new JPanel();
    JPanel daypanel = new JPanel();

    yearpanel.setLayout(new GridBagLayout());
    monthpanel.setLayout(new GridBagLayout());
    daypanel.setLayout(new GridBagLayout());

    // Set sizes
    int width = (int) this.getPreferredSize().getWidth();
    int height = (int) (this.getPreferredSize().getHeight() / 3);
    yearpanel.setSize(width,height);
    daypanel.setSize(width,height);
    monthpanel.setSize(width,height);

    //make transparent
    yearpanel.setOpaque(false);
    monthpanel.setOpaque(false);
    daypanel.setOpaque(false);
  }        

  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Image image = Toolkit.getDefaultToolkit().getImage("Images/CalendarBackground.jpg");
    g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
  }
}

I have no idea why it does that + I could not find an answer online, only people with the same problem whose question got abandoned :/

Can anyone help me?

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Image image = Toolkit.getDefaultToolkit().getImage("Images/CalendarBackground.jpg");
    g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
}
  1. The image should not be loaded in the paint method. Instead it should be declared as a class attribute and preloaded.
  2. The Toolkit method is asynchronous, so it is even more important to use the component as the ImageObserver .

    g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);


Here is a working SSCCE that is something like what you seem to be attempting.

check the return value of Graphics.drawImage() . I bet it's returning false, which means the image you're painting isn't fully loaded and scaled yet. Try loading the image somewhere other than your paintComponent method, like your Calendar constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM