Canvas Only Shows on Resize
Hey,
I'm creating a simple program using a JFrame with a canvas (LogoCanvas()) that is used to display a logo created in another class. I'm trying to display the canvas (logo) in the 'EAST' side of a JPanel with a BorderLayout layout.
My issue is that the logo only displays itself when I resize the running JFrame window. When I simply run the program the logo is invisible.
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.*;
public class MyGUI extends JFrame
{
private JButton cdListBtn = new JButton("View CD List");
private JButton mp3ListBtn = new JButton("View MP3 List");
private JButton cartBtn = new JButton("View Cart");
private JLabel titleLbl = new JLabel("Liam's GUI");
private JPanel northPnl, southPnl;
private LogoCanvas canvas;
Font font = new Font("Tahoma", Font.BOLD, 12);
public MyGUI()
{
super("Follow a GUI Plan");
//Creates new canvas containing logo
canvas = new LogoCanvas();
//Sets fonts for three buttons and label
cdListBtn.setFont(font);
mp3ListBtn.setFont(font);
cartBtn.setFont(font);
titleLbl.setFont(new Font("Tahoma", Font.PLAIN, 20));
//Centers label so it appears in center of JPanel
titleLbl.setHorizontalAlignment(SwingConstants.CENTER);
//Creates north panel and adds title label and logo
northPnl = new JPanel();
northPnl.setOpaque(false);
northPnl.setLayout(new BorderLayout());
northPnl.add(titleLbl, BorderLayout.CENTER);
northPnl.add(canvas, BorderLayout.EAST);
//Creates south panel and adds three buttons (flow layout)
southPnl = new JPanel();
southPnl.setOpaque(false);
southPnl.setLayout(new FlowLayout());
southPnl.add(cdListBtn);
southPnl.add(mp3ListBtn);
southPnl.add(cartBtn);
//Adds north and south panels to frame
getContentPane().add("North", northPnl);
getContentPane().add("South", southPnl);
setSize(400, 300);
setLocation(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setBackground(new Color(0xCC, 0xE6, 0xFF));
setVisible(true);
}
public static void main(String[] args)
{
new MyGUI();
}
}
Any advice would be greatly appreciated. Thanks.
Re: Canvas Only Shows on Resize
Try calling pack on the JFrame prior to calling setVisible
Re: Canvas Only Shows on Resize
Quote:
Originally Posted by
doWhile
Try calling pack on the JFrame prior to calling setVisible
Adding pack(); to the MyGUI method didn't work. Adding it to the line before setVisible compressed the window size and adding it anywhere else had no effect.
Sorry for my ignorance, I'm a student and I've never had to call "pack" before.
Re: Canvas Only Shows on Resize
Why do you setOpaque(false)? What are you trying to achieve by that?
db
Re: Canvas Only Shows on Resize
Moved from New to Java
db
Re: Canvas Only Shows on Resize
Quote:
Originally Posted by
DarrylBurke
Why do you setOpaque(false)? What are you trying to achieve by that?
db
To remove the background from the north and south JPanels. I wouldn't normally, but it states to do so in the assignment brief I've been given.