Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Java Notes

Example - Second Window

The main program of this example is basically identical to that in Example - First Window, but here we define a subclass of JFrame (a window) that constructs a JPanel to put the one component on. In this case the component is a button that does nothing.
  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 /**
 * Program:  basic_windows/Second.java - Customizes a subclass of JFrame  //Note 1
 * @version 2004-10-25
 * @author Michael Maus
 */

import java.awt.*;                                         //Note 2
import javax.swing.*;


/////////////////////////////////////////////////////////// class Second
class Second {
    //====================================================== method main
    public static void main(String[] args) {
        JFrame window = new SecondGUI();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Note 3
        window.setVisible(true);          // make window visible
    }
}


////////////////////////////////////////////////////// class SecondGUI
class SecondGUI extends JFrame {                         //Note 4
    //====================================================== constructor
    public SecondGUI() {                                 //Note 5
        //... Create and initialize the components
        JButton helloButton;                             //Note 6
        helloButton = new JButton("Hello Earthlings.");  //Note 7

        //... Create a panel to put the components on.   //Note 8
        JPanel content = new JPanel();                   //Note 9
        content.setLayout(new FlowLayout());             //Note 10
        content.add(helloButton);                        //Note 11
        this.setContentPane(content);                    //Note 12
                                                         //Note 13
        this.setTitle("Second Window");
        this.pack();       // finalize the layout        //Note 14
    }
}

Notes



Note 1: This style of comment is used by many programs because the <code>javadoc</code> program can read the produce HTML documentation.

Note 2: GUI programs will import classes from these packages, among others. The "*" at the end of an import specification imports all classes in that package.

Note 3: Causes a click on the close box to stop the program (the default is to make the window invisible, but the program keeps running). This is OK for amlll programs, but its also common to add a listener to ask the user whether files should be saved first.

Note 4: This class extends JFrame (the Java class for a window), which means that it can do everything a JFrame can, and more. This is an example of inheritance.

Note 5: The constructor tells how to build an object of this class. It is typically declared <i>public</i>. You can tell a constructor from a method because it has no return type and is the same name as the class.

Note 6: This declares a local variable for a button. See JButton for more on buttons.

Note 7: "new" creates an object, which in this case is a button with the appropriate text. It doesn't do anything yet

Note 8: There are two styles for working with the content pane. (1) Create a JPanel and set the content pane to this panel (what we do here), and (2) Get the predefined content pane and use it. Both styles are common.

Note 9: Create a new JPanel to use as the content pane.

Note 10: Java has a number of ways of arranging components on the content pane. This uses FlowLayout (left to right, top to bottom). There are other layouts (see Layouts).

Note 11: Each component must be "added" to the container (eg, JPanel). The layout controls where it goes.

Note 12: The content pane of a window is where all components are placed. Here we set the content pane to the JPanel we just created.

Note 13: this is a reference to the current object, ie, the JFrame we're creating. "this" is implicitly added by the compiler to instance methods or variables so it's not required here, but it's sometimes written to make things clear. These methods are not defined in this class, but they are defined in one of the parent classes.

Note 14: pack is called to do the final arranging of the components in the layout.

Next

See Example - Generic Calc for an example of components that really do something.

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.