Editing Java programs with the BlueJ IDE

advertisement
Editing Java programs with the
BlueJ IDE
Working environments to develop (= write)
programs
• There are 2 ways to develop (write) computer programs:
1. Using an editor (e.g. gedit) and a compiler (such
as javac) separately.
•
You have seen this method in the last webnote --http://192.168.1.3/~cheung/teaching/web/170/Syll
abus/02/BlueJ/java.html
2. Using an editor and a compiler in an integrated
manner
Working environments to develop (= write)
programs (cont.)
• In the second way, you will need to install a special
application called an Integrated Development Environment
(IDE)
Java IDEs
• There are a number of Integrated Development
Environment IDE) available for Java
• Java IDEs:
• Eclipse -- Eclipse is highly extensible and
customizable, but hard to learn (freely
available)
• NetBeans -- created by Sun MircoSystem
(original designer of the Java programming
language) (freely available)
• JBuilder -- top commercial Java IDE; very
costly...
• BlueJ -- easy to learn (freely available)
Java IDEs (cont.)
• In this webnote, you will learn to edit Java programs with
BlueJ
• In the next webnote, you will learn how to:
• compile the Java program with BlueJ
• run the (compiled) Java program with BlueJ
• You will learn how to program in the Java programming
language later in this course
Java IDEs (cont.)
• BlueJ is freely available and it can be obtained from this
website: http://www.bluej.org/
Preparation
• Before you can use BlueJ, you must:
• Login to a computer in the MathCS lab
• Open a terminal window
• Change the current (working) directory to your
cs170 directory This directory is used to store CS
170 labs and homework.
Information about this BlueJ tutorial
• The tutorial is described from the perspective of the user
cheung
(Because it was developed by Professor Cheung)
• The directory used to hold the project is
/home/cheung/cs170
For clarity, I have delete all files and folders from my
cs170 directory.
Information about this BlueJ tutorial
(cont.)
• We will write a simple Java program and store the program in a
project directory called "TestProj".
The "TestProj" will be contained inside the /home/cheung/cs170
directory.
• In other words, the absolute path of the project directory is:
• /home/cheung/cs170/TestProj
Information about this BlueJ tutorial
(cont.)
• Here is the Simple Java program that you will enter into BlueJ:
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello Class");
System.out.println("How is everyone doing so far
?");
}
}
You don't need to understand this program right now; it will be
explained later in the course.
Topics covered in this (short) tutorial
• Things you need to learn to get started with BlueJ
• Run the BlueJ application
• Create a new project in BlueJ
• Create a new program file
• Insert text into the file
• Delete text from the file
• Goto a certain line in the file
• Search for a pattern in the file
• Search and replace for a pattern with another pattern in the file
• Undo a change
• Save your work
• Quit without saving (because you made a mess)...
Starting the BlueJ IDE application
• Enter the following command in a terminal window:
UNIX prompt>> bluej &
• This will run BlueJ as a detached process
Starting the BlueJ IDE application (cont.)
• You will first see an announcement window:
Starting the BlueJ IDE application (cont.)
• When it's ready, you will see the main window:
Create a new project
• BlueJ requires that each project be stored in a different
directory
When you create a new project, BlueJ will also create a
new directory for you.
Create a new project (cont.)
• How to create a new project:
• Left click on the Project tab
• Then left click on the New Project tab:
Create a new project (cont.)
• A new window will pop up:
Create a new project (cont.)
• Enter the name of the new project directory
(/home/cheung/cs170/TestProj) and click on the
Create button:
Create a new project (cont.)
• When BlueJ has successful created an new project,
it will show the following window:
Create a new program file
• Suppose we want to create a file that contains the
following Java program (given above):
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello Class");
System.out.println("How is everyone doing so far
?");
}
}
Create a new program file (cont.)
Notice that the program is called class Hello
This will be important in the creation procedure.
Create a new program file (cont.)
• How to create a Java program file:
• Left click on the New Class button:
Create a new program file (cont.)
• A new window will pop up:
Create a new program file (cont.)
• Type in the name of the "class" (which is Hello)
and click OK:
• A new window will pop up:
Create a new program file (cont.)
• Final result:
You can see the new file Hello in the TestProj area.
Create a new program file (cont.)
• To see that BlueJ has created a file, we list the content of the
TestProj directory from a terminal window:
The icon named Hello in BlueJ represents the program file
Hello.java inside the TestProj directory.
Open a program file for editing
• If you want to edit a program file, do the following:
• Right click on the file icon
• Then left click on the Open Editor button:
Open a program file for editing
• If you want to edit a program file, do the following:
• A new window will pop up:
Open a program file for editing
The new window contains the content of the file Hello.java
(To verify, use "cat Hello.java" in a terminal window)
BlueJ has already inserted a few things in the file
Hello.java to help you start writing a Java program
Deleting text from a file
• How to delete text from a file:
• Highlight the text in BlueJ that you want to delete:
Deleting text from a file (cont.)
• Press the backspace key You can also press the delete key
or control-X
• Result:
Inserting text into a file
• Use the scroll bar on the right to find the location in the
file where you want to insert text.
• Left click at the insert location
• Then type in the new text.
• Example:
Insert text by copy and paste
• You can insert text from another window into the document in
BlueJ by using the copy and paste facility:
1. Highlight any text in a window (e.g., from a
webpage) The highlighted text is automatically
copied in UNIX
2. (On a Windows-based PC, you need to type
control-C to copy)
3. Now click in the BlueJ window at the position
where you want to insert the highlighted text
4. Type control-V (for paste)
Replacing some text
• How to replace text:
•
Delete the text
•
Insert new text
Undo a change
• When you make a edit mistake, you can undo the last change
with the undo-command:
•
control-Z
Undo a change (cont.)
• Undo earlier changes:
•
You can undo earlier changes by pressing
control-Z multiple time
•
The maximum number of changes can be
undo is 25
Undo an undo
• Suppose you have undone a change that was in fact correct
• You can undo an undo operation using:
•
control-Y (this is called a Redo operation)
Goto a certain line in the file
• A feature that is very useful when you write computer
programs is:
•
Goto a certain line in a file
That is because compilers (an application that translates a
program written in a high level language into machine
code) always report an error along with the location (as a
line number) in the file.
Goto a certain line in the file (cont.)
• How to go to line number n in a file:
1. Left click on the Tools tab
2. Then left click on the Go to Line tab
Goto a certain line in the file (cont.)
Example:
After this, a window will pop up and you can enter the
desired line number
Goto a certain line in the file (cont.)
• Keyboard shortcut:
•
The keyboard shortcut for the Go to Line function
is control-L
Search for a text pattern
• Finding the next occurrence of a pattern in a file:
1. Left click on the Find tab The lower portion of the
BlueJ window will change to the Find menu
Example:
Search for a text pattern (cont.)
Enter the search text pattern and click Next:
The text highlighted in yellow is the next matching pattern
All other matching patterned are highlighted in blue
Search for a text pattern (cont.)
Left click on the Next button to find the subsequent
matching pattern
• Search forward:
• Left click on the Prev button to search forward
Search and Replace
• Finding the next occurrence of a text pattern in a file and
replace it with some other pattern:
• Left click on the Replace tab
The lower portion of the BlueJ window will
change to the Replace menu
Search and Replace (cont.)
Example:
Search and Replace (cont.)
2. Enter the replacement pattern in the Replace field:
Search and Replace (cont.)
3. Click on the Once button to replace the current
occurrence (in yellow):
Search and Replace (cont.)
You can replace the next occurrence by clicking on
Once another time.
Click on All to replace every occurrence
Search and Replace (cont.)
• Hint:
•
If you do not want to replace the current occurrence
and want to continue the Search and Replace
operation, then do the following:
1. Click on the text immediately after the current
occurrence
2. Click Next (to find the next occurrence)
3. Continue with replace if desire
Search and Replace (cont.)
Example:
Search and Replace (cont.)
Click on the text immediately after the current occurrence
Search and Replace (cont.)
Click Next
Continue with the Replace operation if so desired.
Saving the changes
• Auto saving:
•
You do not need to save your work.
•
When you quit (close) the BlueJ window, it saves
your works automatically
Saving the changes (cont.)
• Save your work explicitly:
•
You can choose to save your work explicit by
clicking of Class and then Save:
Quit without saving your work...
• You do not have this option in BlueJ
Exit BlueJ
• Before you exit BlueJ, I would recommend that you save
all your changes explicitly
You have learned saving your work above !!!
Exit BlueJ (cont.)
• Exiting BlueJ:
•
To exit BlueJ, click Project in the BlueJ's main
window
•
and select Quit:
Download