java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4095
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Two examples illustrating the usage of JFrame and JPanel classes E-mail
User Rating: / 41
PoorBest 

This Java tip differantiates between the functionalities of JFrame and JPanel classes with the help of two examples. A JFrame object is the physical window that you'll be working with in the Swing API. Developers may know that there is a need to add a JPanel to the JFrame's contentpane before adding components to the JFrame.

import java.awt.*;
import javax.swing.*;

public class CustomPanel extends JPanel {
   public final static int CIRCLE = 1, SQUARE = 2;
   private int shape;

   public void paintComponentGraphics g )
   {
      super.paintComponent);

      if shape == CIRCLE )
         g.fillOval50106060 );
      else if shape == SQUARE )
         g.fillRect50106060 );
   }

   public void drawint )
   {
      shape = s;
      repaint();
   }
}

// Using a customized Panel object.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CustomPanelTest extends JFrame {
   private JPanel buttonPanel;
   private CustomPanel myPanel;
   private JButton circle, square;

   public CustomPanelTest()
   {
      super"CustomPanel Test" );

      myPanel = new CustomPanel();   // instantiate canvas
      myPanel.setBackgroundColor.green );

      square = new JButton"Square" );
      square.addActionListener(
         new ActionListener() {
            public void actionPerformedActionEvent e )
            {
               myPanel.drawCustomPanel.SQUARE );
            }
         }
      );

      circle = new JButton"Circle" );
      circle.addActionListener(
         new ActionListener() {
            public void actionPerformedActionEvent e )
            {
               myPanel.drawCustomPanel.CIRCLE );
            }
         }
      );

      buttonPanel = new JPanel();
      buttonPanel.setLayoutnew GridLayout1) );
      buttonPanel.addcircle );
      buttonPanel.addsquare );

      Container c = getContentPane();
      c.addmyPanel, BorderLayout.CENTER );  
      c.addbuttonPanel, BorderLayout.SOUTH );

      setSize300150 );
      show();
   }

   public static void mainString args[] )
   {
      CustomPanelTest app = new CustomPanelTest();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosingWindowEvent e )
            {
               System.exit);
            }
         }
      );
   }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.