CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: swings

  1. #1
    Join Date
    Jul 2011
    Posts
    29

    swings

    hi..
    i am creating an interface of my projct using jframe form
    i created a frame
    on that frame i added 2 panels ...1 upper jpanel covering most of central area
    and one lower jpanel
    on the upper panel i want to add a rectangle...
    but the rectangle i am adding is not being added on the jpanel rather it is being added on the frame below the jpanel...when i double click on the screen or jpanel...the frame maximises and then it is visible on the upper jpanel...pls help..i want to add that rectangle on jpanel

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: swings

    What component are you using for the rectangle? The Rectangle class has no GUI ability.

    Can you post a small simple program that executes and demonstrates your problem.
    Please wrap the code in code tags: [code] and [/code ] (without space)
    Norm

  3. #3
    Join Date
    Jul 2011
    Posts
    29

    Re: swings

    [code]package drawings;

    import java.awt.Graphics;
    import java.awt.Panel;

    /**
    *
    * @author Administrator
    */
    public class mod1 extends javax.swing.JFrame implements Runnable {

    /** Creates new form mod1 */
    public mod1() {
    initComponents();

    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jPanel2 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    try {
    jPanel1 =(javax.swing.JPanel)java.beans.Beans.instantiate(getClass().getClassLoader(), "drawings.mod1_jPanel1");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (java.io.IOException e) {
    e.printStackTrace();
    }

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(255, 153, 153));
    setForeground(new java.awt.Color(255, 51, 51));

    jPanel2.setBackground(new java.awt.Color(102, 255, 51));

    jButton1.setText("jButton1");

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(35, 35, 35)
    .addComponent(jButton1)
    .addContainerGap(871, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(41, 41, 41)
    .addComponent(jButton1)
    .addContainerGap(98, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 979, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 445, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(299, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>


    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    public javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration
    /*
    public void paint(Graphics g)
    {
    g.drawRect(100, 100, 70, 80);

    }
    *
    */
    public void paint(Graphics g)
    {
    g.drawRect(200, 200, 70, 80);


    }
    public void run() {

    }

    }




    package drawings;

    import javax.swing.JPanel;

    /**
    *
    * @author Administrator
    */
    public class mod2 {

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new mod1().setVisible(true);



    }
    });


    }
    }

    [\code]

  4. #4
    Join Date
    Jul 2011
    Posts
    29

    Re: swings

    i want this rectangle to be placed on a jpanel ...and this jpanel is on a frame

  5. #5
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: swings

    Please edit your posted code and correct the ending code tag. It should be [/code]

    Where is the rectangle you talk about?

    This is not a simple program to demo your problem. It is missing a class when I try to execute it.
    Last edited by Norm; July 30th, 2011 at 03:16 PM.
    Norm

  6. #6
    Join Date
    Jul 2011
    Posts
    29

    Re: swings

    Code:
    package drawings;
    
    import java.awt.Graphics;
    import java.awt.Panel;
    
    /**
    *
    * @author Administrator
    */
    public class mod1 extends javax.swing.JFrame implements Runnable {
    
    /** Creates new form mod1 */
    public mod1() {
    initComponents();
    
    }
    
    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
    
    jPanel2 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    try {
    jPanel1 =(javax.swing.JPanel)java.beans.Beans.instantiate(getClass().getClassLoader(), "drawings.mod1_jPanel1");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (java.io.IOException e) {
    e.printStackTrace();
    }
    
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(255, 153, 153));
    setForeground(new java.awt.Color(255, 51, 51));
    
    jPanel2.setBackground(new java.awt.Color(102, 255, 51));
    
    jButton1.setText("jButton1");
    
    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(35, 35, 35)
    .addComponent(jButton1)
    .addContainerGap(871, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(41, 41, 41)
    .addComponent(jButton1)
    .addContainerGap(98, Short.MAX_VALUE))
    );
    
    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 979, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 445, Short.MAX_VALUE)
    );
    
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(299, Short.MAX_VALUE))
    );
    
    pack();
    }// </editor-fold>
    
    
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    public javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration
    /*
    public void paint(Graphics g)
    {
    g.drawRect(100, 100, 70, 80);
    
    }
    *
    */
    public void paint(Graphics g)
    {
    g.drawRect(200, 200, 70, 80);
    
    
    }
    public void run() {
    
    }
    
    }
    
    
    
    
    package drawings;
    
    import javax.swing.JPanel;
    
    /**
    *
    * @author Administrator
    */
    public class mod2 {
    
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    
    public void run() {
    new mod1().setVisible(true);
    
    
    
    }
    });
    
    
    }
    }

  7. #7
    Join Date
    Jul 2011
    Posts
    29

    Re: swings

    please see the method public void paint()
    the rectangle code is in that

  8. #8
    Join Date
    Jul 2011
    Posts
    29

    Re: swings

    it has two clases .... create two classes as different files....please run the two classes seperately...one class has the main method..run the main class...

  9. #9
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: swings

    Please edit your code and restore its indentation. All the code you posted is aligned to the left.
    It is very hard to read that kind of code.

    Can you post a small simple complete program that executes and demonstrates your problem.
    Your posted code is generated by an IDE. It is not human generated. And it is missing classes.
    Norm

  10. #10
    Join Date
    Jul 2011
    Posts
    29

    Re: swings

    i mentioned above that the code is generated using jframe form..
    just tell me this that i want to add rectangle over a jpanel ...and this jpanel is over a frame window..how can i do that...i am using void paint method but the rectangle is being added on the frame below jpanel

  11. #11
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: swings

    Can you post a small simple complete program that executes and demonstrates your problem.
    Norm

  12. #12
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: swings

    Or change your code to this:
    Code:
    /*  Remove this code
    try {
    jPanel1 =(javax.swing.JPanel)java.beans.Beans.instantiate(getClass().getClassLoader(), "drawings.mod1_jPanel1");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (java.io.IOException e) {
    e.printStackTrace();
    }
    */
    jPanel1 = new JPanel();   // Use this
    Norm

  13. #13
    Join Date
    Jul 2011
    Posts
    29

    Re: swings

    ok..let me see

  14. #14
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: swings

    One problem I see is that your paint method is in the JFrame and not in a JPanel.
    Norm

  15. #15
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: swings

    Quote Originally Posted by archit View Post
    i mentioned above that the code is generated using jframe form..
    just tell me this that i want to add rectangle over a jpanel ...and this jpanel is over a frame window..how can i do that...i am using void paint method but the rectangle is being added on the frame below jpanel
    Please learn how to post correctly formatted code on the forum.

    If you want to draw a rectangle on a panel, put your drawing code in the panel's paintComponent(..) method.

    There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies...
    C.A.R. Hoare
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured