Results 1 to 7 of 7
Thread: JFrame and JPanel
- 01-15-2011, 12:52 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
JFrame and JPanel
Hello, please
I have two class NewJFrame and NenJPenel
NenJPenel contains a graph
NewJFrame contains a button
what I want is that when I press the button of NewJFrame
the graph of NewJPanle appear
I put in NenJFrame:
NewJPanel NewJPanel panel = new ();
panel.show ();
it does not work
code :
----------- NewJPanel -------------------------------------
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
public class NewJPanel extends javax.swing.JPanel {
int[] data = {21, 14, 18, 03, 86, 88, 74, 87, 54, 77,61, 55, 48, 60, 49, 36, 38, 27, 20, 18};
final int PAD = 20;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// Draw ordinate.
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
// Draw abcissa.
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
double xInc = (double)(w - 2*PAD)/(data.length-1);
double scale = (double)(h - 2*PAD)/getMax();
// Mark data points.
g2.setPaint(Color.red);
for(int i = 0; i < data.length; i++) {
double x = PAD + i*xInc;
double y = h - PAD - scale*data[i];
g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
}
}
private int getMax() {
int max = -Integer.MAX_VALUE;
for(int i = 0; i < data.length; i++) {
if(data[i] > max)
max = data[i];
}
return max;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new NewJPanel());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
public NewJPanel() {initComponents();}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
}
------------------------NewJFrame------------------------------------
public class NewJFrame0 extends javax.swing.JFrame {
public NewJFrame0() {initComponents();}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addContainerGap(317, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addContainerGap(255, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(22, 22, 22))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
/ * I want the code that displays my panel * /
NewJPanel panel=new NewJPanel();
panel.setVisible(true);
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
//setvisible and show does not work
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame0().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
thank you :(Last edited by khiat; 01-15-2011 at 01:18 PM. Reason: java
-
Where do you add your JPanel to the JFrame? Also, please edit your post so that your code uses code tags and is readable.
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 01-15-2011, 06:37 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
[java]
public class NewJFrame0 extends javax.swing.JFrame {
public NewJFrame0() {initComponents();}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addContainerGap(317, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILI NG, layout.createSequentialGroup()
.addContainerGap(255, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(22, 22, 22))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
/ * I want the code that displays my panel * /
NewJPanel panel=new NewJPanel();
panel.setVisible(true);
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
//setvisible and show does not work
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame0().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
[/java]
-
[java]
[/java]
?
Being able to follow simple instructions and use code tags is kind of like an aptitude test to see if you're intelligent enough to program. Please re-look at my post above and pass the test.
Also, I ask again, where do you add your JPanel to the JFrame? Also, you will need to read the layout manager tutorial and use a layout other than GroupLayout for this to work. You can find the tutorial here: Layout Manager TutorialsLast edited by Fubarable; 01-15-2011 at 07:00 PM.
- 01-15-2011, 07:11 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
preblems
1) i do not know how i use tags
2) it's is problem if you can help me, how integrate JPanel in JFrame le problem is solved
thanks you
-
How come since I showed explicitly you how to use it in my first post in this thread? Again, you place the tag [code] above your code block, not [java] and the tag [/code] below your code block, not [/java].
Again, you are creating a JPanel but adding it to nothing and you must add it to a top level container in order for it to be shown. You need to read the Swing tutorials to learn how Swing works and can find them here: Swing Tutorials2) it's is problem if you can help me, how integrate JPanel in JFrame le problem is solved
thanks you
- 01-15-2011, 07:32 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
Help with JPanel and JFrame
By weikang in forum AWT / SwingReplies: 3Last Post: 11-26-2010, 01:22 AM -
JPanel/JFrame
By Mayur in forum New To JavaReplies: 2Last Post: 12-20-2009, 05:07 AM -
JFrame and JPanel -Error
By britto_bicsjohn in forum AWT / SwingReplies: 2Last Post: 08-25-2009, 01:31 PM -
JFrame and two JPanel Classes
By YellowPainting in forum AWT / SwingReplies: 9Last Post: 08-25-2009, 12:49 PM -
scroll a Jpanel in a JFrame
By nidhirastogi in forum SWT / JFaceReplies: 1Last Post: 09-07-2008, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks