Вы находитесь на странице: 1из 7

Java Programming Lecture 1

Java was created in 1991 by James Gosling as part of a corporate research project at Sun Microsystems. This project, code-named Green, began in response to predictions that microprocessors the chips that make computers work would have a profound impact on intelligent consumer electronic devices. The Java programming language was based on C++, a widely used programming language that provides capabilities for object-oriented programming. The marketplace for intelligent consumer electronic devices did not develop as quickly as Sun had anticipated; as a result, the Green project suffered some initial difficulties. By sheer good fortune, the World Wide Web exploded in popularity in 1993, and sun saw an immediate potential for using Java to create dynamic content (animated and interactive content) for Web pages. Sun introduced a mechanism for building Java programs called applets that could execute in Web pages that were viewed in Web browsers. Sun formally announced Java at a conference in May 1995. Ordinarily, an event like this would not have generated much attention. However, Java generated immediate excitement in the business community because of the phenomenal interest in the Web. Developers now use Java to create dynamic Web pages, to build large-scale enterprise applications, to enhance the functionality of Web servers (computers that provide the content that is distributed to your Web browser when you browse Web sites), to provide applications for consumer devices (such as cell phones, pagers and PDAs) and for many other purposes. Java is no longer a language used simply to make Web pages come alive. Java has become the preferred language for meeting many organizations programming needs. There are several reasons why so many programmers prefer Java to other languages. First, Java is fully object-oriented. Objects are reusable software components that model items in the real world. Object-oriented programs are often easier to understand, correct, and modify than programs developed with previous techniques. Second, Java programs consist of pieces called classes, which are used to define objects. Classes include methods, which perform tasks and return information when they complete those tasks. You can create each piece you need to form a Java program. However, most Java programmers take advantage of the rich collection of existing classes in the Java class library, which is also known as the Java API (Application Programming Interface). Thus, there are really two pieces to learning the Java world. The first is the Java language itself, so that you can program your own classes and methods; the second is the Java class library which provides an extensive set of reusable classes. Many programmers favor using Java because it is platform independent, meaning that Java applications can be created and run on a variety of computer platforms (that is, different types of computers running different operating systems such as Windows and Linux). This concept of write once, run anywhere allows for a truly portable experience- one application can be given to people on several computer platforms , regardless of the computer platform on which the application was created. Java achieves its portability and good performance with a clever combination of compilation and interpretation of Java programs.

Page 1

PROGRAM EXAMPLE 1
Instructions: 1. Open Notepad. 2. Type the program shown below. 3. Save using Welcome.java as filename. 4. Go to command prompt. 5. Use cd command to go to your working directory. (Example: cd java\samples\Welcome) 6. Compile by typing javac Welcome.java then press Enter key. 7. Run program by typing java Welcome. import java.awt.*; import javax.swing.*; public class Welcome extends JFrame { private JLabel textJLabel; // JLabel that displays text private JLabel pictureJLabel; // JLabel that displays an image // no-argument constructor public Welcome() { createUserInterface(); } // create and position GUI components; register event handlers private void createUserInterface() { // get content pane and set layout to null Container contentPane = getContentPane(); contentPane.setBackground( Color.YELLOW ); contentPane.setLayout( null ); // set up textJLabel textJLabel = new JLabel(); textJLabel.setText( "Welcome to Java Programming!" ); textJLabel.setLocation( 35, 0 ); textJLabel.setSize( 550, 88 ); textJLabel.setFont( new Font( "SansSerif", Font.PLAIN, 36 ) ); textJLabel.setHorizontalAlignment( JLabel.CENTER ); contentPane.add( textJLabel ); // set up pictureJLabel pictureJLabel = new JLabel(); pictureJLabel.setIcon( new ImageIcon( "bug.png" ) ); pictureJLabel.setBounds( 54, 120, 500, 250 ); pictureJLabel.setHorizontalAlignment( JLabel.CENTER ); contentPane.add( pictureJLabel ); Page 2

// set properties of application's window setTitle( "Welcome" ); // set JFrame's title bar string setSize( 608, 413 ); // set width and height of JFrame setVisible( true ); // display JFrame on screen } // end method createUserInterface // main method public static void main( String[] args ) { Welcome application = new Welcome(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main } // end class Welcome

/************************************************************************** * (C) Copyright 1992-2004 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * **************************************************************************/

Output:

Page 3

KEY TERMS
o o o o o o o o o o o o o o o o o o o o o o o o o o o o background property Property that specifies the background color of a content pane or component. book-title capitalization A style that capitalizes the first letter of each significant word in the text (for example, Calculate the Total). bounds property The property that specifies both the location and size of a component. case sensitive Distinguishes between uppercase and lowercase letters in code .class file The type of file that is executed by the Java runtime Environment (JRE). A .class file is created by compiling the applications .java file. compiling The process that converts a source code file (.java) into a .class file. content pane The portion of a JFrame that contains the GUI components. Debugging The process of locating and removing errors in an application. dir command Command typed in a Command Prompt window to list the directory contents. font property - The property that specifies the font name, style and font size points of any displayed text in a JFrame or one of its components. horizontalAlignment property The property that specifies how text is aligned within a JLabel. Icon property The property that specifies the file name of the image displayed in a JLabel. javac command The command that compiles a source code (.java) into a .class file. .java file The type of file which programmers write the java code for an application. JLabel The component that displays text or an image that the user cannot modify. Location property The property that specifies where a components upper-left corner appears on the JFrame. Logic error An error that does not prevent your application from compiling successfully, but does cause your application to produce erroneous results. Pixel A point on your computer screen. Pixel is short for picture element. RGB value The amount of red, green and blue needed to create a color. Semicolon (;) The character used to indicate the end of a Java statement. Size property Property that specifies the width and height, in pixels, of a component. Source code file A file with a .java extension. These files are editable by the programmer, but are not executable. Statement Code that instructs the computer to perform a task. Every statement ends with a semicolon (;) character. Most applications consist of many statements. String A sequence of characters within double quotes. String literal A sequence of characters within double quotes. Syntax error An error that occurs when code violates the grammatical rules of a programming language. Text property The property that specifies the text displayed by a JLabel. Title bar The area at the top of a JFrame where its title appears.

Page 4

SKILLS SUMMARY
Creating GUIs Quickly and Efficiently Use predefined graphical user interface components like JLabels. Setting and Positioning Components Use setSize and setLocation to set the size and location properties or setBounds to set the bounds property, which sizes and positions components. Setting the Dimensions of a JFrame or JLabel by Using Property size Use setSize to set the width and height in pixels of the JFrame or JLabel Setting the Content Panes Background Color Use setBackground to set the content panes background property. Some predefined colors include Color.RED, Color.GREEN and Color.BLUE. Setting a JLabels font Property Use setFont to set the JLabels font property, which changes the font of the displayed text. You can specify the font name (for example, SansSerif, Times, Courier, etc.), the font style (for instance, Font.PLAIN) and the font size in points. Aligning Text in a JLabel Use setHorizontalAlignment to set the JLabels horizontalAlignment property. Possible value for the horizontalalignment property are JLabel.LEFT (the default), JLabel.CENTER and JLabel.RIGHT. Adding an Image to the JFrame Use a JLabel to display the image. Use setIcon to set the JLabels icon property to display the image.

GUI DESIGN GUIDELINES


Overall Design Use colors in your applications, but not to the point of distracting the user. JFrames Choose short, descriptive JFrame titles. JFrame titles should use book-title capitalization, in which the first letter of each significant word is capitalized and the title does not end with any punctuation. JLabels Use JLabels to display text that users cannot change. Ensure that all JLabel components are large enough to display their text. Use JLabels with images to enhance GUIs with graphics that users cannot change. Ensure that all JLabel components are large enough to display their images.

Page 5

JAVA LIBRARY REFERENCE ----------------------------------------------------------------------------------------------------------------------------------------JFrame - This component enables a Java application to appear in its own window. All other components in an application are displayed within the applications window. Methods setBackground Sets the background color of the JFrames content pane (or of other components). For example, if the content pane of the JFrame is called contentPane, then the statement contentPane.setBackground(Color.YELLOW); sets the background color of the content pane to yellow. setSize Sets the size (in pixels) of the JFrame. setTitle Sets the text displayed in the title bar of the JFrame. ----------------------------------------------------------------------------------------------------------------------------------------JLabel - This component displays text or an image that the user cannot modify. Methods setBounds Specifies the size and location in the JLabel. setFont Specifies the font name, style and size of the text displayed in the JLabel. setHorizontalAlignment Determines how the text is aligned in the JLabel. setIcon Specifies the file name and path of the image in the JLabel. setSize Specifies the height and width (in pixels) in the JLabel. setText Specifies the text displayed in the JLabel. ----------------------------------------------------------------------------------------------------------------------------------------

PRE-DEFINED COLORS
Constant Color.BLACK Color.BLUE Color.CYAN Color.DARK_GRAY Color.GRAY Color.GREEN Color.LIGHT_GRAY Color.MAGENTA Color.ORANGE Color.PINK Color.RED Color.WHITE Color.YELLOW 0, 0, 0 0, 0, 255 0, 255, 255 64, 64, 64 128, 128, 128 0, 255, 0 192, 192, 192 255, 0, 255 255, 200, 0 255, 175, 175 255, 0, 0 255, 255, 255 255, 255, 0 RGB value

Reference: Simply Java Programming An Application-Driven Tutorial Approach By Deitel, Deitel, Listfield, Yaeger, & Zhang Page 6

Page 7

Вам также может понравиться