You are on page 1of 6

1 First steps in JAVA programming (with Eclipse IDE)

3In the lines that follows I will try to write a few basics in programming.
4Iwant them to be useful to any beginner, be it a student or a human
5profile graduate.

6Firstof all, I want to describe what a programming language is. There


7are many definitions for that on the internet, but I think the best one is
8on Wikipedia: A set of definite expressions/rules/forms available
9techniques instructions for a computer.

10

11The programmer writes the source code, then it compiles the code
12(transforming it from a language appropriate to human to a language
13that computer can understands).

14I will explain in what follows a simple program in Java programming


15language.

16For example, a programmer writes the source code for displaying a


17window. This contains 2 editable fields witch can be filled with numeric
rd
18values. A 3 field will display to the user the result of the addition.

19The result is going to display when the user press the button labeled
20Calculate.

21To make these happen we fill need an IDE (a software development


22environment or Integrated Development Environment). The IDE is also a
23program to write and compile other programs.

24To illustrate, I will present the IDE Eclipse:


25
26

27That shows an environment in which programs are written. Next, I will


28explain every key portion of this picture, and we are going to write the
29program for the actions early mentioned.

30

31
32
33 1) The button for creating new projects
34 2) Package explorer the place where we can view workgroup projects
35 3) The selected project
36 4) By pressing this, we bring the source of the program demo in first place.
37 5) Name of the current program/class
38 6) Name of the starting function where the program starts
39 7) The parenthesis are used as follows :
40 a. { is used to mark the beginning of a function/block, in our case it marks the
41 beginning of the main function
42 b. } is used to mark the end of a function/block, in our case it marks the end of
43 the main function(where our program ends)
44 8) The tab where we see the program errors
45 9) The tab where we see the compiling result/spellchecking result
46

47Good, now I explained each key part of IDE, lets start writing a program
48and explain it.
49import java.awt.*;
50import java.awt.event.*;
51import javax.swing.*;
52
53public class GUI{
54
55 public static void main(String[] args) {
56
57 JFrame frame = new JFrame("Test Window");
58 frame.setLayout(new FlowLayout());
59 JLabel label1 = new JLabel("Enter first number:");
60 JLabel label2 = new JLabel("Enter second number:");
61 JLabel labelr = new JLabel("Result:");
62
63 JTextField textf1 = new JTextField("",5);
64 JTextField textf2 = new JTextField("",5);
65 JTextField textfr = new JTextField("",5);
66 JButton button = new JButton("Compute");
67 frame.add(label1);
68 frame.add(textf1);
69 frame.add(label2);
70 frame.add(textf2);
71 frame.add(labelr);;
72 frame.add(textfr);
73 frame.add(button);
74 button.addActionListener(new ActionListener(){
75 public void actionPerformed(ActionEvent e){
76
77 int number1=0,number2=0;
78 boolean error = false;
79
80 try{
81 number1 = Integer.parseInt(textf1.getText());
82 number2 = Integer.parseInt(textf2.getText());
83 }
84 catch(Exception ex){
85 textfr.setText("error");
86 error = true;
87 }
88 if (!error)
89 textfr.setText(number1+number2+" ");
90
91 }
92 });
93 frame.setVisible(true);
94 frame.setSize(600, 200);
95 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
96 }
97
98}
99The program has 50 lines of code. Lines 1-3 adds some libraries for
100classes/functions we use later.

101java.awt and java.swing packages contains the functions we need for


102displaying GUI elements like windows, buttons fields or text etc.Line 2
103contains the import of java.awt.event package, which is used to handle
104events.

105Line 5 contains the class declaration. A class in java is a template for a


106real object. It contains data and actions (fields and methods)

107For example we can have the class car with fields/data carName,
108carType, carColor and actions carAccelerate and carStop.

109A class models a real life object. In our case, we dont have methods
110and fields, because all is done in the method main. But GUI class is a
111class.

112Ihave to mention that white-spaces and tabs are by default ignored by


113the compiler and the instructions are separated by ;. So lines 4 and 6
114are ingnored by the compiler.

115

116Line9 (JFrame frame = new JFrame("Test Window");) creates a frame object


117from class(template JFrame). We can find class JFrame in the java.swing
118package from line 3. Now we have a window where we can display
119anything we want. Lines 10 sets the layout of the window to default
120FlowLayout. I want to emphasize that the FlowLayout is also an object.

121Next, the lines 11-18 creates our GUIs elements/objects, and we have
122to add them to our window in lines 19-25.

123Good, now we have a window with 3 JFields(java fields) objects and a


124button, but they do nothing. We need a way to listen or be notified by
125JButton(java button) presses.
126And we will use the addActionListener method from class JButton witch
127adds actions to be executed when a key press happens. These are the
128lines 26-44. When we enter 2 numbers in our fields and press button
rd
129calculate we can see the result of number1 + number2 in the 3 field
130 textfr(It is an object of class JTextField).
131if (!error) textfr.setText(number1+number2+" ");
132
133So line 41 do the magic with the function actionPerformed. When an
134action is performed, all the actionPerform function body is executed,
135also the line 41.

136Lets see the execution of our program

137
138

139Aswe explained, we have a window, 3 JTextFields with JLabels and a


140button. The result is as expected, because 53 = 31+22 .