from bluej to netbeans - laerer.rhs.dklaerer.rhs.dk/psl/rhs/kvu-materiale/material/netbeans/from...

48
From BlueJ to NetBeans

Upload: duongthuan

Post on 01-Apr-2018

260 views

Category:

Documents


2 download

TRANSCRIPT

From BlueJ to NetBeans

Why change…?

BlueJ is great for introduction to programming

◦ Simple interface, few options

◦ Graphical interface to classes and objects

◦ Some tehnical details are hidden

Why change…?

NetBeans is great for professional programming

◦ Much more functionality

◦ Better editor, debugger,…

◦ Integration to database systems

◦ A full-blown, stand-alone system

…but also more complex!

What is NetBeans?

NetBeans is an open-source project

Originates from SUN Microsystems

www.netbeans.org

Can be downloaded for free from the above website

We use NetBeans 7.1 SE (+ Java JDK)

Version 7.1.x is also OK

What is NetBeans?

Click here…

What is NetBeans?

Click here…

We use ”Java SE”

What is NetBeans?

NOTE!!!

The rest of the presentation has been made using NetBeans 6.9…

However, the GUI is very similar to 7.1, so you should be able to find the same features easily in 7.1

There might be small differences here and there, though…

What is NetBeans?

How do I…

…create a new project?

…add a new class?

…edit a class definition?

…write text to the screen?

…get input from the user?

…run a project?

Create a new project - BlueJ

Choose Project | New Project…

Create a new project - BlueJ

Enter project name, press ”Create”

Create a new project - NetBeans

Choose File | New Project…

Create a new project - NetBeans

In Categories, choose ”Java”

In Projects, choose ”Java Application”

Create a new project - NetBeans

Enter project name and location

Set name of Main class!!

Create a new project - NetBeans

NOTE!!

All Java programs must contain a method with this signature:

public static void main(String[] args)

This method is placed in the ”main” class

This class is traditionally named Main, but is doesn’t have to!

Per default, NetBeans creates a ”main” class with the project name…

Create a new project - NetBeans

Example

◦ You create a project named SchoolAdm

◦ NetBeans will suggest to create a main class called schooladm.SchoolAdm

◦ You can keep this, or change it to schoolAdm.Main

◦ Do not just change it to Main; your project will not work!!

Create a new project - NetBeans

Create a new project - NetBeans

As mentioned, all Java programs must contain one method with this signature:

public static void main(String[] args)

This was hidden in BlueJ!

When the program starts, the code in the main(…) method is executed, no mat-ter which class that contain this method!

Create a new project - NetBeans

Note the yellow folder-like icons called ”Source Packages” and ”Libraries”

A ”package” is just a collection of classes, usually related classes

A NetBeans project has per default a package under ”Source Packages”, with the same name as the project

Our class definitions will be in that package – for now…

Add a new class- BlueJ

Press ”New Class…”

Enter class name

Add a new class- BlueJ

Add a new class- NetBeans

Highlight the package with the same name as the project (here bankaccount)!

Choose New | Java Class

Add a new class- NetBeans

Enter class name

Add a new class- NetBeans

Edit a class definition - BlueJ

Double-click on the class icon

Edit a class definition - BlueJ

Edit a class definition - NetBeans

Double-click on the class icon

Edit a class definition - NetBeans

Note the file panes in NetBeans

Edit a class definition - NetBeans

Why do red lines start to appear in the code, as soon as I start typing…?

The NetBeans editor continuously makes an analysis of the code, even before it is compiled

The editor highlights errors in the code by a waved red line, even before typing has been completed

Helpful…? Annoying…?

Edit a class definition - NetBeans

Yes, I know, but I am still typing!!

Edit a class definition - NetBeans

See hints by hovering mouse over (!)

Edit a class definition - NetBeans

Why do lists with method names pop up when I type…?

The NetBeans editor supports ”auto-completion”, i.e. it tries to predict what you will type next

Typically when calling a method with ”.”

Helpful, when you get used to it…

Also try hitting Ctrl + Space, after having typed e.g. part of a class name….

Edit a class definition - NetBeans

Available methods on the specific object/class

Documentation for each method

Writing text to the screen - BlueJ

Writing text to the screen - BlueJ

Writing text to the screen - NetBeans

Writing text to the screen - NetBeans

Need this code for actually executing the method

Writing text to the screen - NetBeans

Output written to the ”Output window”

Get input from the user - BlueJ

Get input from the user - BlueJ

Enter parameter value directly

Get input from the user - NetBeans

Not quite as simple to get input from a user in NetBeans

Two options

◦ Use the Scanner class

◦ Use a input dialog class

See chapter 3.6 in Big Java for details

Get input from the user - NetBeans

import java.util.Scanner;

Scanner in = new Scanner(System.in);

String name = in.nextLine();

int balance = in.nextInt();

double area = in.nextDouble();

Get input from the user - NetBeans

import javax.swing.JOptionPane;

String value =

JOptionPane.showInputDialog(”Limit”);

int limit = Int.parseInt(value);

Running a project - BlueJ

We cannot as such ”run” a project in BlueJ – what should run..?

We might create a ”system” class, that has a ”run”-like method

◦ Create a System object

◦ Right-click the object

◦ Call the run method

Running a project - NetBeans

Choose Run | Run Main Project

Running a project - NetBeans

…or just click the green triangle (or press F6)

Running a project - NetBeans

Running a project always executes the main method, no matter which class that actually contains the method!

A project is automatically compiled when you save it

No reason to run a project in order to fix syntax errors

Other NetBeans features

A GUI Builder – build a GUI by drag-drop of controls. Code is auto-generated

A much more powerful debugger

Test features

More customisable

Easy integration to database systems

We will talk about additional features later on…