Re: Problems with JPanel and Graphics

From: Knute Johnson (nospam_at_sagebrush.frazmtn.com)
Date: 01/22/05


Date: Sat, 22 Jan 2005 11:08:25 -0800

bunallo wrote:
> That does not help I still don't get anything blue in the bottom or to the
> left (only white if I pull the frame).

Your code is missing a few parts like the import statements and the
main(). But that aside you can't see the JPanel because the JFrame is
obscuring it. Add a setPreferredSize() to your constructor to tell the
layout manager what size to layout the JPanel. Then call pack() instead
of setSize() on your frame. You will still probably have a funny
looking frame with some white space because the JPanel is smaller than
the frame's header.

-- 
Knute Johnson
email s/nospam/knute/
import java.awt.*;
import javax.swing.*;
public class Land extends JPanel {
   public Land() {
       setPreferredSize(new Dimension(100,100));
     JFrame my_frame = new JFrame();
     my_frame.getContentPane().add(this);
     my_frame.setTitle("Field");
//    my_frame.setSize(100,100);
     my_frame.pack();
     my_frame.setVisible(true);
   }
   public void paintComponent(Graphics g) {
     g.setColor(Color.BLUE);
     g.fillRect(0,0,100,100);
     g.setColor(Color.GREEN);
     g.fillRect(5,5, 90,90);
}
     public static void main(String[] args) {
         new Land();
     }
}


Relevant Pages

  • Re: =?ISO-8859-15?Q?!DisplayPanel_einem_JFrame_hinzuf=FCgen.?=
    ... ich habe eine Klasse Frame. ... In der soll ein neuer JFrame erzeugt werden. ... Dieses Display Panel ist ein normales JPanel dem einfach nur ein JTextField ...
    (de.comp.lang.java)
  • JDialog problem: cant figure out how to get parent?
    ... I want to create a new JDialog centered over the current JFrame so I ... need to pass the parent frame to the JDialog. ... Is there any way to determine the frame parent/owner of a JPanel? ...
    (comp.lang.java.help)
  • Re: JFrame, JPanel and Applications
    ... > I would like to create a swing application that shows one window (JFrame). ... > This Frame has a JPanel with a JButton that calls a second ... > The second JPanel has another JButton that have to show third JPanel... ...
    (comp.lang.java.gui)
  • Re: JFrame, JPanel and Applications
    ... > JPanel to show in the Frame - i did this.setContentPane(new ... > The second JPanel has another JButton that have to show third ... you can put all of the panels into the JFrame at the beginning, ...
    (comp.lang.java.gui)
  • Re: Preserving a panels width:height ratio when the frame is resized
    ... >> ratio when it's parent frame is resized. ... For example, if a JFrame ... >> containing a JPanel is resized I would like the JPanel to retain ...
    (comp.lang.java.gui)