First Steps in JAVA Programing
First Steps in JAVA Programing
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.
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).
19The result is going to display when the user press the button labeled
20Calculate.
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.
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.
115
121Next, the lines 11-18 creates our GUIs elements/objects, and we have
122to add them to our window in lines 19-25.
137
138