INF160Lec06 NetBeans

advertisement
INF160 IS Development Environments
AUBG, COS dept
Lecture 06
Title:
Dev Env: NetBeans
(Extract from Syllabus)
Reference: www.netbeans.org
1
Lecture Contents:
 NetBeans – introduction
 NetBeans – functionality


NetBeans IDE Java Quick Start Tutorial
(http://netbeans.org/kb/docs/java/quickstart.html)
Introduction to GUI Building
(http://netbeans.org/kb/docs/java/guifunctionality.html)
 NetBeans – configuration
2
NetBeans – Introduction
 NetBeans refers to both


NetBeans Platform
NetBeans IDE
 NetBeans Platform is a reusable framework for
simplifying the development of Java Swing
desktop applications.
 NetBeans IDE is an open-source integrated
development environment that supports
development of all Java application types.
3
NetBeans – introduction
 Currently installed NetBeans IDE version
 Develop desktop, mobile and web applications
with Java, PHP, C/C++ and more.
 Runs on Windows, Linux, Mac OS X and Solaris.
4
NetBeans – functionality
NetBeans IDE Java Quick Start
 Setting up the project







To Create an IDE project
Adding Code to the Generated Source File
Compiling and running a project
Building and deploying the application (.jar file)
Create javadoc
Packaging and Distributing Java Desktop Applications
Tips for NetBeans users
5
Setting Up the Project
.
6
To create an IDE project:
 1. Start NetBeans IDE.
 2. In the IDE, choose File > New Project…
(Ctrl+Shift+N), as shown in the figure below.
7
To create an IDE project:
 3. In the New Project wizard, expand the Java
category and select Java Application as shown
in the figure below. Then click Next.
8
To create
an IDE
project:
 4. In the Name and Location page of the wizard, do
the following (as shown in the figure above):




In the Project Name field, type <PrjName> on your choice.
Leave the Use Dedicated Folder for Storing Libraries
checkbox unselected.
In the Create Main Class field, type
<PackageName>.<ClassName> on your choice (if it is empty).
9
Leave the Set as Main Project checkbox selected.
To create an IDE project:
 5. Click Finish.
The project is created and opened in the IDE. You should see the
following components:




The Projects window, which contains a tree view of the components of
the project, including source files, libraries that your code depends on,
and so on.
The Source Editor window with a file called <ClassName>.java open.
The Navigator window, which you can use to quickly navigate between
elements within the selected class.
The Tasks window, which lists compilation errors as well other tasks that
10
are marked with keywords.
Adding Code to the Generated
Source File
 Because you have left the Create Main Class checkbox selected in the New
Project wizard, the IDE has created a skeleton main class for you. You can
add the "Hello World!" message to the skeleton code by replacing the line:
// TODO code application logic here
with the line:
System.out.println("Hello World!");
 Save the change by choosing File > Save.
 The file should look something like the following code sample.
11
Adding Code to the Generated Source File
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package helloworldapp;
/**
*
* @author <your name>
*/
public class HelloWorldApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
12
Compiling and running a project
 Because of the IDE's Compile on Save feature, you do
not have to manually compile your project in order to
run it in the IDE. When you save a Java source file,
the IDE automatically compiles it. (how to switch this
option off, see Project > Properties > Compiling).
 To run the program:



Choose Run > Run Main Project
(F6).
The next figure shows what you should now see.
Congratulations! Your program works.
13
Compiling and running a project
14
Compiling and running a project
 If there are compilation errors, they are marked with
red glyphs in the left and right margins of the Source
Editor.


The glyphs in the left margin indicate errors for the
corresponding lines.
The glyphs in the right margin show all of the areas of the
file that have errors, including errors in lines that are not
visible.
 You can mouse over an error mark to get a description
of the error.
 You can click a glyph in the right margin to jump to the
line with the error.
15
Compiling and running a project
 Here are more options to process the project:
 To test the program:



Choose Run > Test Project(<prj name>)
(Alt+F6)
Try the command to know what you should now see.
Open the Output window (if it is closed) and have a
look at the report generated in it.
16
Compiling and running a project
 Here are more options to process the project:
 To build only (without run) the program:



Choose Run > Build Main Project
(F11)
Try the command to know what you should now see.
Open the Output window (if it is closed) and have a
look at the report generated in it.
17
Compiling and running a project
 Here are more options to process the project:
 To duplicate the recent command given:



Choose Run > Repeat Build/Run:<prj name>(jar)
Try the command to know what you should now see.
Open the Output window (if it is closed) and have a
look at the report generated in it.
18
Compiling and running a project
 Here are more options to process the project:
 To establish project settings:
Click right mouse btn on Project name, select Properties
OR
 Choose Run > Set project configuration >

• <default setting>
• Customize…


Try the command to know what you should now see.
Project Properties screen gets opened – you are free to
manipulate/establish/modify current project parameters.
19
Compiling and running a project
 Here are more options to process the project:
 To establish Main project settings:


Click right mouse btn Project name, select Set as Main project
OR
Choose Run > Set Main project >
• None
• Select among all projects for the current NetBeans session


Try the command to know what you should now see.
List of existing projects gets displayed and you are to
select/set the main project for the current session.
20
Compiling and running a project
 Here are more options to process the project:
 To create archive (.jar) file for the program:




Choose Run > Clean and Build Main Project (Shift+F11)
Try the command to know what you should now see.
Open the Output window (if it is closed) and have a
look at the report generated in it.
For details see next slide.
21
Building and deploying the application (.jar file)
 Once you have written and test run your application, you can use
the Clean and Build command to build your application for
deployment. When you use the Clean and Build command, the
IDE runs a build script that performs the following tasks:


Deletes any previously compiled files and other build outputs.
Recompiles the application and builds a JAR file containing the compiled
files.
 To build your application:

Choose Run > Clean and Build Main Project (Shift+F11)
 You can view the build outputs by opening the Files window and
expanding the <PrjName> node. The compiled bytecode file
<PrjName>.class is within the build/classes/<ClassName>
subnode. A deployable JAR file that contains the
<PrjName>.class is within the dist node.
22
Building and deploying the application (.jar file)
.
.
23
Create javadoc
 To be discussed in separate lecture on SW
Documentation.
24
Packaging and Distributing Java Desktop Applications
One question that a lot of beginning programmers have is: "Now that I've
created my application in the IDE, how do I get it to work from the
command line outside of the IDE." Similarly, someone might ask, "How do I
distribute this application to other users without having to give them the
whole IDE as well?"
The answers to these questions are relatively simple, but not necessarily
obvious. This document addresses those questions by taking you through the
basics of using the IDE to prepare your applications for distribution and
deployment. In addition, this document provides information that you might
need to configure your system (or which you might need to pass on to the
users of your application). We will show a few different approaches for
deploying an application, so that users can access the application by:
Double-clicking the application's Java Archive (JAR) file.
Calling the application from the command line.
Calling the application from a script file.
Using Java Web Start
25
Tips for NetBeans users
 The Compile on Save feature can be turned off in the
Project Properties window. Right-click your project,
select Properties. In the Properties window, choose
the Compiling tab. The Compile on Save checkbox is
right at the top.
 Note that in the Project Properties window you can
configure numerous settings for your project: project
libraries, packaging, building, running, etc.
26
NetBeans – configuration
.
27
Exercises/Tasks
 Write a Java program to test the options for
the main menu Run command:
 To display 5 times the string
“Good luck, dear INF160 student!!”
 To enter two numeric integer/real values and
To display their sum and product.
28
Exercises/Tasks
for(int i=1; i<=5; i++)
{
System.out.print("Good luck, dear");
System.out.println(" INF160 student!!");
}
29
Exercises/Tasks
import java.util.Scanner;
…
Scanner cin = new Scanner(System.in);
System.out.println("\n Enter two real numeric values:");
double inp1, inp2, result1;
inp1 = cin.nextDouble(); inp2 = cin.nextDouble();
result1 = inp1 + inp2;
System.out.println(" result of addition is = " + result1);
30
Exercises/Tasks
import java.util.Scanner;
public class INF160Proba3 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
for(int i=1; i<=5; i++)
System.out.println("Good luck, dear INF160 student!!");
Scanner cin = new Scanner(System.in);
System.out.println("\n Enter two real numeric values:");
double inp1, inp2, result1;
inp1 = cin.nextDouble();
inp2 = cin.nextDouble();
result1 = inp1 + inp2;
System.out.println(" result of addition is = " + result1);
} // end of main
} // end of class
31
Exercises/Tasks
 Write a Java program to test the command-line
arguments option
 How to specify arguments

Right-click your project, select Properties. In the Properties
window, choose the Run tab. Enter arguments
 How to access arguments
public static void main(String[] args) {
for (int i=0; i<args.length; i++)
System.out.println(args[i]);
}
32
Exercises/Tasks
 How to access arguments, second more reliable version
public static void main(String[] args) {
System.out.println("\n\n Command line arguments processed");
if (args.length >0)
{
for(int i=0; i<args.length; i++)
System.out.println(args[i]);
}
else
System.out.println(" No cmd line arguments specified");
}
33
Exercises/Tasks
 Write a Java program to test the command-line
arguments option
 Run from within NetBeans
 Run as a separate OS command
java –jar Application.rar
1111 2222 333
34
Exercises/Tasks
 Write a Java program to test the main menu
Refactor command.
 Rename variables



inp1
into
input1
inp2
into
input2
result1
into
res1
35
Exercises/Tasks




Write a Java program to test the
GCD problem
Fibonacci problem
Factoriel problem
36
NetBeans – functionality
Introduction to GUI Building







Contents
Exercise 1: Creating a Project
Exercise 2: Building the Front End
Exercise 3: Adding Functionality
Exercise 4: Running the Program
How Event Handling Works
37
Exercise 1: Creating a Project
c
38
Exercise 2: Building the Front End
c
39
Exercise 3: Adding Functionality
c
40
Exercise 4: Running the Program
c
41
How Event Handling Works
c
42
Exercises/Tasks
 Try to run ProgFrameAndLayout.java program
43
Thank You
For
Your Attention!
44
Download