-
July 30th, 2011, 01:55 PM
#1
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
-
July 30th, 2011, 02:53 PM
#2
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
-
July 30th, 2011, 03:02 PM
#3
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]
-
July 30th, 2011, 03:09 PM
#4
Re: swings
i want this rectangle to be placed on a jpanel ...and this jpanel is on a frame
-
July 30th, 2011, 03:11 PM
#5
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
-
July 30th, 2011, 03:14 PM
#6
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);
}
});
}
}
-
July 30th, 2011, 03:15 PM
#7
Re: swings
please see the method public void paint()
the rectangle code is in that
-
July 30th, 2011, 03:18 PM
#8
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...
-
July 30th, 2011, 03:20 PM
#9
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
-
July 30th, 2011, 03:24 PM
#10
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
-
July 30th, 2011, 03:26 PM
#11
Re: swings
Can you post a small simple complete program that executes and demonstrates your problem.
Norm
-
July 30th, 2011, 03:27 PM
#12
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
-
July 30th, 2011, 03:31 PM
#13
-
July 30th, 2011, 03:34 PM
#14
Re: swings
One problem I see is that your paint method is in the JFrame and not in a JPanel.
Norm
-
July 30th, 2011, 03:37 PM
#15
Re: swings
Originally Posted by archit
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 [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|