Results 1 to 7 of 7
  1. #1
    khiat is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default 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 02:18 PM. Reason: java

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  3. #3
    khiat is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    [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]

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    [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 Tutorials
    Last edited by Fubarable; 01-15-2011 at 08:00 PM.

  5. #5
    khiat is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by khiat View Post
    preblems
    1) i do not know how i use tags
    How come since I showed explicitly you how to use it in my first post in this thread? Again, you place the tag [cod&#101;] above your code block, not [java] and the tag [/cod&#101;] below your code block, not [/java].

    2) it's is problem if you can help me, how integrate JPanel in JFrame le problem is solved
    thanks you
    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 Tutorials

  7. #7
    khiat is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

Similar Threads

  1. Help with JPanel and JFrame
    By weikang in forum AWT / Swing
    Replies: 3
    Last Post: 11-26-2010, 02:22 AM
  2. JPanel/JFrame
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 12-20-2009, 06:07 AM
  3. JFrame and JPanel -Error
    By britto_bicsjohn in forum AWT / Swing
    Replies: 2
    Last Post: 08-25-2009, 02:31 PM
  4. JFrame and two JPanel Classes
    By YellowPainting in forum AWT / Swing
    Replies: 9
    Last Post: 08-25-2009, 01:49 PM
  5. scroll a Jpanel in a JFrame
    By nidhirastogi in forum SWT / JFace
    Replies: 1
    Last Post: 09-07-2008, 04:42 AM

Tags for this Thread

Posting Permissions

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