Results 1 to 3 of 3
  1. #1
    vivek_9912 is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

    Default How to place panel into frame

    Hi Everybody !! I am new to Swing and am working on a GUI based Elevator Simulation System. I am using Frame on which I am trying to put a Panel. But the panel does not set to the size I want it to even if I use setSize() method. I am posting the code .Could anyone please help me.

    Thanks !!


    Java Code:
    package elevator; import javax.swing.*;import java.awt.*; public class Controller {        public static void main(String args[])    {              Container cpane;      JFrame frame = new JFrame("ELEVATOR");      cpane = frame.getContentPane();      Lift1Panel lp1 = new Lift1Panel();      frame.setVisible(true);      frame.setSize(1300,800);      frame.add(lp1);      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         }        }      package elevator; import javax.swing.*;import java.awt.*;  public class Lift1Panel extends JPanel {            public Lift1Panel()     {         setSize(200,200);            }         public void paintComponent(Graphics g)    {             super.paintComponent(g);             Graphics2D g2d = (Graphics2D)g;     g2d.setColor(Color.WHITE);      g2d.drawRect(10,10,100,100);           }  }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class ControllerRx {
        public static void main(String[] args) {
            JFrame frame = new JFrame("ELEVATOR");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container cpane = frame.getContentPane();
            cpane.setLayout(new GridBagLayout());
            Lift1Panel lp1 = new Lift1Panel();
            frame.add(lp1, new GridBagConstraints());
            frame.setSize(600,400);
            frame.setVisible(true);
        }
    }
    
    class Lift1Panel extends JPanel {
        public Lift1Panel() {
            setPreferredSize(new Dimension(200,200));
            setBackground(Color.pink);
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D)g;
            g2d.setColor(Color.WHITE);
            g2d.drawRect(10,10,100,100);
        }
    }

  3. #3
    vivek_9912 is offline Member
    Join Date
    Nov 2007
    Posts
    3
    Rep Power
    0

Similar Threads

  1. How can i copy a folder from one place to another..
    By rajeshgubba in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 02:21 AM
  2. Replies: 2
    Last Post: 03-03-2008, 08:05 PM
  3. Help with drag from panel
    By fernando in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2007, 10:19 PM
  4. how to place a divider of the splitpane?
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:41 PM
  5. how to place an image in an applet
    By paty in forum Java Applets
    Replies: 2
    Last Post: 08-04-2007, 05:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •