Phát triển ứng dụng mã nguồn mở - Bài 4: Core Java
Nội dung • Tổng quan về Java • So sánh Java và C++ • Đặc trưng của Java • Java platform • Nguyên lý hướng đối tượng của Java
Bạn đang xem trước 20 trang tài liệu Phát triển ứng dụng mã nguồn mở - Bài 4: Core Java, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Đoàn Thiện Ngân Bài 4 - 1/34
Bài 4 :
Core Java
GV: ĐOÀN THIỆN NGÂN
Đoàn Thiện Ngân Bài 4 - 2/34
Nội dung
• Tổng quan về Java
• So sánh Java và C++
• Đặc trưng của Java
• Java platform
• Nguyên lý hướng đối tượng
của Java
Đoàn Thiện Ngân Bài 4 - 3/34
Tài liệu tham khảo
1. JAVA™ FOR PROGRAMMERS, 2ND
EDITION, DEITEL® DEVELOPER SERIES
2. NetBeans IDE 7 Cookbook, 2011, Packt
Publishing
3. Java Tutorial,
4. Netbeans Tutorial,
Đoàn Thiện Ngân Bài 4 - 4/34
Tổng quan về Java
• Java có cấu trúc tương tự C++
• Java đơn giản hơn
• Java là một ngôn ngữ lập trình rất mạnh
• Java là một công nghệ mới xây dựng ứng
dụng trên nhiều nền tảng khác nhau
/java/index-jsp-135888.html
Đoàn Thiện Ngân Bài 4 - 5/34
Java và C++
C++ Java
Object-Oriented Optional Obligatory
Variables
Globals Function
Optional doesn’t exist
Multi-Platform Soure Code
(sometime)
Soure Code
bytecode
Memory
Administration
Manual Automatic
(Gababe
Collector)
Multi-Thread No Yes
Multi-Inheritance Yes No
Đoàn Thiện Ngân Bài 4 - 6/34
Đặc trưng của Java
• Hướng đối tượng
• Đơn giản
• Phân tán
• Thông dịch trung gian (bytecode)
• An toàn - Bảo mật
• Cấu trúc độc lập
• Dễ thích ứng
• Kiểu dữ liệu mạnh
• Xử lý đa luồng (multi-thread)
Đoàn Thiện Ngân Bài 4 - 7/34
Đặc trưng của Java
• Đặc trưng cơ bản của Java từ C và C++
• Java được dùng nhiều trong ứng dụng:
– di động
– desktop
– web
• JVM (Java Virtual Machine) là máy ảo Java
để chạy chương trình Java dạng bytecode
(JRE - Java Runtime Environment)
• JDK (Java Development Kit) là bộ công cụ
để biên dịch (bytecode), dò lỗi và thực thi
chương trình Java
Đoàn Thiện Ngân Bài 4 - 8/34
Đoàn Thiện Ngân Bài 4 - 9/34
• HelloWorldApp.java
• javac HelloWorldApp.java
• HelloWorldApp.class
• java HelloWorldApp
Đoàn Thiện Ngân Bài 4 - 10/34
What Can Java Technology Do?
The general-purpose, high-level Java
programming language is a powerful
software platform. Every full
implementation of the Java platform gives
you the following features.
• Development Tools: The development
tools provide everything you'll need for
compiling, running, monitoring,
debugging, and documenting your
applications. As a new developer, the
main tools you'll be using are the javac
compiler, the java launcher, and the
javadoc documentation tool.
Đoàn Thiện Ngân Bài 4 - 11/34
What Can Java Technology Do?
• Application Programming Interface (API):
The API provides the core functionality of
the Java programming language. It offers
a wide array of useful classes ready for
use in your own applications. It spans
everything from basic objects, to
networking and security, to XML
generation and database access, and
more. The core API is very large; to get an
overview of what it contains, consult the
Java SE Development Kit (JDKTM)
documentation.
Đoàn Thiện Ngân Bài 4 - 12/34
What Can Java Technology Do?
• Deployment Technologies: The JDK
software provides standard mechanisms
such as the Java Web Start software and
Java Plug-In software for deploying your
applications to end users.
• User Interface Toolkits: The Swing and
Java 2D toolkits make it possible to create
sophisticated Graphical User Interfaces
(GUIs).
Đoàn Thiện Ngân Bài 4 - 13/34
What Can Java Technology Do?
• Integration Libraries: Integration libraries
such as the Java IDL API, JDBCTM API,
Java Naming and Directory InterfaceTM
("J.N.D.I.") API, Java RMI, and Java
Remote Method Invocation over Internet
Inter-ORB Protocol Technology (Java
RMI-IIOP Technology) enable database
access and manipulation of remote
objects.
Đoàn Thiện Ngân Bài 4 - 14/34
How Will Java Technology Change My Life?
We can't promise you fame, fortune, or even a job
if you learn the Java programming language. Still,
it is likely to make your programs better and
requires less effort than other languages. We
believe that Java will help you:
• Get started quickly: Although the Java
programming language is a powerful object-
oriented language, it's easy to learn,
especially for programmers already familiar
with C or C++.
• Write less code: Comparisons of program
metrics (class counts, method counts, and so
on) suggest that a program written in the Java
programming language can be four times
smaller than the same program written in C++.
Đoàn Thiện Ngân Bài 4 - 15/34
How Will Java Technology Change My Life?
• Write better code: automatic garbage
collection helps you avoid memory leaks. Its
object orientation, its JavaBeansTM
component architecture, and its wide-
ranging, easily extendible API let you reuse
existing, tested code and introduce fewer
bugs.
• Develop programs more quickly: is simpler
than C++, and as such, your development
time could be up to twice as fast when
writing in it. Your programs will also require
fewer lines of code.
Đoàn Thiện Ngân Bài 4 - 16/34
How Will Java Technology Change My Life?
• Avoid platform dependencies: You can
keep your program portable by avoiding
the use of libraries written in other
languages.
• Write once, run anywhere: Because
applications written in the Java
programming language are compiled into
machine-independent bytecodes, they run
consistently on any Java platform.
Đoàn Thiện Ngân Bài 4 - 17/34
How Will Java Technology Change My Life?
• Distribute software more easily: With Java
Web Start software, users will be able to
launch your applications with a single
click of the mouse. An automatic version
check at startup ensures that users are
always up to date with the latest version
of your software. If an update is available,
the Java Web Start software will
automatically update their installation.
Đoàn Thiện Ngân Bài 4 - 18/34
JAVA Editors
• Netbeans
• Eclipse
• JBuilder
• JCreator
• JDeveloper
• KDeveloper
•
Đoàn Thiện Ngân Bài 4 - 19/34
The "Hello World!" Application
• JDK: Java Development Kit
avase/downloads/index.html
• Java Compiler: javac
• Java Runtime Machine:
– java
– javaw
• JRE: Java Runtime Machine
– jre
– java, javaw
Đoàn Thiện Ngân Bài 4 - 20/34
The "Hello World!" Application
• NetBeans IDE
• HelloWorldApp.java
public class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!\n"); }
}
• Biên dịch: javac HelloWorldApp.java
• Chạy thử: java HelloWorldApp
• Netbeans: Build (F11, Shift+F11); Run (F6)
• Chú ý: Tên class trùng tên tập tin java
Đoàn Thiện Ngân Bài 4 - 21/34
JAR File
• Chương trình soạn thảo project trong
Java có chức năng đóng gói thành một
tập tin có extension là JAR.
• Netbeans tạo file *.jar đóng gói chương
trình trong thư mục dist của project
• Chạy chương trình với file jar
java ─jar *.jar
Ví dụ:
java ─jar HelloWorldApp.jar
Đoàn Thiện Ngân Bài 4 - 22/34
What is Swing?
• To create a Java program with a graphical
user interface (GUI), you'll want to learn
about Swing.
• The Swing toolkit includes a rich set of
components for building GUIs and adding
interactivity to Java applications. Swing
includes all the components you would
expect from a modern toolkit: table controls,
list controls, tree controls, buttons, and
labels,
• Swing is far from a simple component
toolkit, however. It includes rich undo
support, a highly customizable text package,
integrated internationalization and
accessibility support.
Đoàn Thiện Ngân Bài 4 - 23/34
What is Swing?
• Swing supports numerous look and feels,
including the ability to create your own look
and feel.
• Swing wouldn't be a component toolkit without
the basic user interface primitives such as drag
and drop, event handling, customizable
painting, and window management.
• Swing is part of the Java Foundation Classes
(JFC). The JFC also include other features
important to a GUI program, such as the ability
to add rich graphics functionality and the
ability to create a program that can work in
different languages and by users with different
input devices.
Đoàn Thiện Ngân Bài 4 - 24/34
Swing and JFC
• Swing GUI Components: The Swing toolkit includes
a rich array of components: buttons, check boxes,
tables, text, There are file browsers and dialogs to
suit most needs, and if not, customization is
possible. If none of Swing's provided components
are exactly what you need, you can leverage the
basic Swing component functionality to create your
own.
• Java 2D API: To make your application stand out;
convey information visually; or add figures, images,
or animation to your GUI, you'll want to use the Java
2D API. Because Swing is built on the 2D package,
it's trivial to make use of 2D within Swing
components. Adding images, drop shadows,
compositing — it's easy with Java 2D.
Đoàn Thiện Ngân Bài 4 - 25/34
Swing and JFC
• Pluggable Look-and-Feel: Support Any
program that uses Swing components has a
choice of look and feel. The classes shipped
by Oracle provide a look and feel that
matches that of the platform. The Synth
package allows you to create your own look
and feel. The GTK+ look and feel makes
hundreds of existing look and feels available
to Swing programs.
• A program can specify the look and feel of
the platform it is running on, or it can
specify to always use the Java look and feel,
and without recompiling, it will just work. Or,
you can ignore the issue and let the UI
manager sort it out.
Đoàn Thiện Ngân Bài 4 - 26/34
Swing and JFC
• Data Transfer : via cut, copy, paste, and drag
and drop, is essential to almost any
application. Support for data transfer is built
into Swing and works between Swing
components within an application, between
Java applications, and between Java and
native applications.
• Internationalization: This feature allows
developers to build applications that can
interact with users worldwide in their own
languages and cultural conventions.
Applications can be created that accept
input in languages that use thousands of
different characters, such as Japanese, or
Korean, ...
Đoàn Thiện Ngân Bài 4 - 27/34
Swing and JFC
• Swing's layout managers make it easy to
honor a particular orientation required by
the UI. For example, the UI will appear
right to left in a locale where the text
flows right to left. This support is
automatic: You need only code the UI
once and then it will work for left to right
and right to left, as well as honor the
appropriate size of components that
change as you localize the text.
Đoàn Thiện Ngân Bài 4 - 28/34
Java Swing Application
• Cộng 2 số
Đoàn Thiện Ngân Bài 4 - 29/34
Swing Application
• New Project/Java/Java Application.
• Addition in the Project Name/path.
• Deselect the Create Main Class checkbox
• New File/ JFrame Forms/ AdditionUI as the
class name.
• Package
• Jlabel, JTextField, JButton, Jlist, JTextArea,
JComboBox, JRadioBox, JCheckBox,
JTable, JTree, JMenu,
• Set AdditionUI as the main class before
running.
Đoàn Thiện Ngân Bài 4 - 30/34
Đoàn Thiện Ngân Bài 4 - 31/34
Đoàn Thiện Ngân Bài 4 - 32/34
ActionPerformed Event
float firstnum =
Float.parseFloat(txtNumber1.getText());
float secondnum =
Float.parseFloat(txtNumber2.getText());
float result = firstnum + secondnum;
String str = String.valueOf(firstnum);
str += " + ";
str += String.valueOf(secondnum);
str += " = ";
str += String.valueOf(result);
lblResult.setText(str);
txtNumber1.setText("");
txtNumber2.setText("");
Đoàn Thiện Ngân Bài 4 - 33/34
Máy tính
• MiniCalc
• Chức năng tính toán
đơn giản (cộng, trừ,
nhân và chia) với các
số thập phân (tương
tự Calc trong MS
Windows)
• Sqrt (căn bậc 2)
• Bắt lỗi tính toán khi
nhập sai
Đoàn Thiện Ngân Bài 4 - 34/34
???
• Java và NetBeans
– Java? NetBeans?
– Cài đặt JDK, NetBeans
– Xem cấu trúc tổ chức một project trong
Netbeans
– Biên dịch (build) và chạy thử
– Xem cấu trúc tổ chức một project trong
Netbeans sau khi biên dịch
– Gói chương trình *.jar
– Swing Application
– Viết chương trình máy tính giao diện như
trang trước.