Tuesday, September 8, 2009

Graphical User Interface In Java part 1 from 3






Frankly, graphics-based applications are always more interesting than the application of command-line based. And the good news, building graphical applications in Java, it is not as difficult as imagined

Java is a programming language first released by Sun Microsystems in 1995. Java philosophy of "Write Once, Run Everywhere" make Java become one of the popular programming languages today. According to the official website of Java, Java technology currently used in 4.5 billion devices (more than 800 million PCs, 1.5 billion gadgets, 2.2 billion smart cards) and still did not include use in printers, web-cam, games, car navigation systems, medical devices, etc

know JavaIf you've heard of Java, but not so deeply in the program, chances are you will be confused with terms such as Java 2 SDK, JDK, Java Applets, Java Script, Java Runtime Engine (JRE), Java SE 6, etc.All of the Java technology, but if you want to learn to develop Java applications on a PC, the Java SE (Standard Edition) is an appropriate option. Whereas if you just want to run Java applications on a PC, you need a Java Runtime Engine (JRE)

Java SE name used interchangeably with the Java Development Kit (JDK) or Java Software Development Kit (SDK), Java 2 Standard Edition (J2SE). The latest version of Java SE is the version released on May 28, 2009, the Java SE 6 Update 14 that claim to support Windows Vista SP2, Windows 7, Internet Explorer 8 and Windows Server 2008 SP2. You can download it for free at the Java site, http / /: java.sun.com / javase / downloads / index.jsp

Java SE Development Kit (JDK) has the form of files Basic Tools:

• Compiler name javac

• Launcher or interpreter to run Java applications, called java
• Java debugger called jdb

• Viewers applet called appletviewer

• Disassembler for file.class named javap

• Generators for API documentation is called javadoc

• manager JAR files (Java Archive) named jar

When you first learn to create applications using the Java compiler javac, and java launcher will be your best friend. There are three major lines of program groups can be created with the Java language (Standard Edition), namely: applet, a Java application is run through the browser by calling the HTML tags - the same as if we are to display an image using HTML tags.Java applications (usually called "application" only) Shared longer be: Application GUI (graphical user interface). I.e. applications that use graphics display. These applications can be run on the local computer, just like we run other application programs

a. Application command line, It is limited to applications that display text, which is run and displayed in the command-prompt

b. Package (or library Java) Package itself not to run, he only provides a set of Java classes that are considered useful and the public to be used again

Making Java ProgramsStep - step in making Java applications are as follows:

1. Making the program text (source-code) with the Java language syntax. You can type in source-code in any text editor and save it with java extension.

2. To compile the text into a Java application program

3. Running Java applications with interpreters or the applet, the applet viewer if the shape

For example, we will try to make the first program in Java:

1. public class Hello {

2. public static void main (String [] args) {

3. / / Display message

4. system.out.printIn ( "Hello, world!");

5. }

6. } Make sure the file name is Hello.java

Description:

1. The first line: make a class named "Hello". The entire framework in Java is always in the class

2. The name of the class must always be the same as the file namea.

a)In the first line, is the Hello class name and file name are Hello.javab.

b) Not allowed to use a different name, eg file name replaced with Halo.java. This will cause an error during compilation

3. On the second line written method main (). This method has an array of String parameters. This is a String array argument of Java programs.

a. A Java application program must have the main part, a method called main ()

b. Method main () function as a starting point of the program. That is, when the Java program is run, method main () always run the first time

4. In the third row, there are comments

a. Comments will not be compiled and run

b. Compiler will ignore the comment lines

c. Comment line begins with a slash character uses two "//". (comments by early signs "/ /" should only be on one line)

5. The fourth line is the command to display the message

a. The message of the string "Hello, world!" Displayed by the method called printIn ()

b. PrintIn method () is part of the variable named out

c. Meanwhile, out is part of the class named Systemd. To call the dot operator (".") is used to System.out.printIn ()

The process for compiling Java programs are:

1. Once finished creating the Java file, the next step is to compile a file using javac.exe (Being in the directory \ bin \)

2. Running the compiler command in the command-prompt

> Javac Hello.java

3. Hello.class which generated files are output files from the compilation results

Hello.class files can be directly run the program using java.exe.

> java Hello

A good start in learning Java. However, it is difficult to be able to develop an application that has the interesting interface simply by relying on command-line display as above.

That's why we need a user interface is graphical, and Java has provided.

Beginning with A Window

Graphics-based application begins with making a Window. In the visual programming language (such as Visual Basic), this window is called the Form, Java name with a Frame. After making our frames can put all the necessary components, such as Menu, Button, Checkbox, Text Field, or whatever you want to put in there.

step in making the frame in Java is:

1. Creating objects by using JFrame class (a class provided by Java for creating graphical user interface)

JFrame frame = new JFrame ();
2. Adjust the size of the frame. If size is not specified, the default size used is 0.0.
frame.setSize (400.400);
3. Displaying frameframe.
setVisible (true);
we try to compile into a complete program
-------------------------------------------------------------------------------------------------
because we define the class name GUI1, then the appropriate file name is GUI1.java.
Description:
1. The first line is the command:
import javax.swing .*;
This command shows that we need a swing library (package) is located in the javax. Without doing import, we will not be able to make the frame, because the frame class defined in the javax.swing package.
2. The third line defines the class name is used, namely GUI1. Storing files must be done by using the name GUI1.java
3. In the fourth line write method main () which is the main program.
4. The next lines are to make an object called a frame, determining size, and displays.
To be able to see the results, we need to compile it using javac
> Javac GUI1.javaI
if successful, we run the compilation by using Java.
> Java GUI1
Display that appears is an empty frame with a size of 400x400. Note, this frame does not appear in the command-prompt (which used to run the application), but emerged as a new window, while the command-prompt still active.
Problems arise when we closed the frame with the click icon close. Command-prompt does not return to a state ready to accept commands, but still seemed GUI1 running applications (although the frame has been closed)
For that we need to determine the action performed by the application close icon is clicked. We add a command that is placed on line 9
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
If we do not determine DefaultCloseOperation, so when users click the icon close, just hide the application frame. But really, the applications remain in memory, which is why the command-prompt as if it were still running the application (do not return to the prompt and ready to receive another command) In this condition, we may be forced to close the command-prompt or by pressing Ctrl + C in the command-prompt.
Little is still annoying in this frame is that this frame does not have a title on the title bar. We can add a title with one of two ways:
First at the time of the object frame:
JFrame frame = new JFrame ( "TitleFrame ");
Or can be made by calling setTitle method (which can be placed after the making of objects)
frame.setTitle ( "Title Frame");
Ideally we first determine the title of the frame, and if in the middle of the application is required to change the title, we can call this method.
Just a note, form the frame to display a Java application will greatly depend on the platform that is used when running the application. If you use the operating system, Linux or Mac, then with the same program, will produce a different frame models.
Then, the next question is how to add components (such as button, label, combo box) on this frame? We will discuss the Graphical User Interface in Java Part 2

No comments:

Post a Comment