Bài4 : Core Java
GV: ĐOÀN THIỆN NGÂN
ĐoànThi ệnNgân Bài4 -1/34
Nộidung
• 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ànThi ệnNgân Bài4 -2/34
Tàili ệuthamkh ảo
1.JAVA™ FOR PROGRAMMERS, 2 ND
EDITION, DEITEL® DEVELOPER SERIES
2.NetBeansIDE 7 Cookbook, 2011, Packt
Publishing 3.Java Tutorial,
http://docs.oracle.com/javase/tutorial/
4.NetbeansTutorial,
http://netbeans.org/kb/trails/matisse.html
ĐoànThi ệnNgân Bài4 -3/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
http://www.oracle.com/technetwork /java/index-jsp-135888.html
ĐoànThi ệnNgân Bài4 -4/34
Java và C++ C++
Java
Object-Oriented
Optional
Obligatory
Optional
doesn’t exist
Variables Globals Function Multi-Platform
Soure Code (sometime)
Soure Code bytecode
Manual
Memory Administration
Automatic (Gababe Collector) Yes
Multi-Thread
No
Yes
Multi-Inheritance
ĐoànThi ệnNgân
No Bài4 -5/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ànThi ệnNgân Bài4 -6/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ànThi ệnNgân Bài4 -7/34
ĐoànThi ệnNgân Bài4 -8/34
javac HelloWorldApp.java
• HelloWorldApp.java • • HelloWorldApp.class java HelloWorldApp •
ĐoànThi ệnNgân Bài4 -9/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ànThi ệnNgân
Bài4 -10/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ànThi ệnNgân Bài4 -11/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ànThi ệnNgân Bài4 -12/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ànThi ệnNgân Bài4 -13/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ànThi ệnNgân Bài4 -14/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ànThi ệnNgân Bài4 -15/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ànThi ệnNgân Bài4 -16/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ànThi ệnNgân Bài4 -17/34
JAVA Editors
• Netbeans • Eclipse • JBuilder • JCreator • JDeveloper • KDeveloper • …
ĐoànThi ệnNgân Bài4 -18/34
The "Hello World!" Application • JDK: Java Development Kit
http://www.oracle.com/technetwork/java/j avase/downloads/index.html
• Java Compiler: javac • Java Runtime Machine:
– java – javaw
• JRE: Java Runtime Machine
– jre – java, javaw
ĐoànThi ệnNgân Bài4 -19/34
java HelloWorldApp
The "Hello World!" Application • NetBeansIDE • HelloWorldApp.java public class HelloWorldApp{ public static void main(String[] args) { System.out.println("Hello World!\n"); } } • Biên dịch: javacHelloWorldApp.java • Chạyth ử: • Netbeans: Build (F11, Shift+F11); Run (F6) • Chúý: Tênclass trùngtên t ậptin java
ĐoànThi ệnNgân Bài4 -20/34
JAR File
• Chươngtrìnhso ạnth ảoproject trong
Java cóch ức năng đónggóithành m ột tậptin cóextension làJAR.
• Netbeans tạofile *.jar đónggóich ương
trìnhtrongth ư mụcdist c ủaproject
• Chạych ươngtrình v ớifile jar
java ─jar *.jar
Ví dụ:
java ─jar HelloWorldApp.jar
ĐoànThi ệnNgân Bài4 -21/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ànThi ệnNgân Bài4 -22/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ànThi ệnNgân Bài4 -23/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ànThi ệnNgân Bài4 -24/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ànThi ệnNgân Bài4 -25/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ànThi ệnNgân Bài4 -26/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ànThi ệnNgân Bài4 -27/34
Java Swing Application
• Cộng 2 số
ĐoànThi ệnNgân Bài4 -28/34
Swing Application
• New Project/Java/Java Application. • Additionin the Project Name/path…. • Deselect the Create Main Class checkbox • New File/ JFrameForms/ AdditionUIas the
class name.
• Package • Jlabel, JTextField, JButton, Jlist, JTextArea,
JComboBox, JRadioBox, JCheckBox, JTable, JTree, JMenu, …
• Set AdditionUI as the main class before
running.
ĐoànThi ệnNgân Bài4 -29/34
ĐoànThi ệnNgân Bài4 -30/34
ĐoànThi ệnNgân Bài4 -31/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ànThi ệnNgân Bài4 -32/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ànThi ệnNgân Bài4 -33/34
???
• Java vàNetBeans – Java? NetBeans? – Cài đặtJDK, NetBeans – Xem cấutrúc t ổ chức mộtproject trong
Netbeans
– Biên dịch(build) vàch ạyth ử – Xem cấutrúc t ổ chức mộtproject trong
Netbeanssaukhibiên d ịch
– Góich ươngtrình*.jar – Swing Application – Viếtch ươngtrìnhmáytínhgiaodi
ệnnh ư
trangtr ước.
ĐoànThi ệnNgân Bài4 -34/34