Está en la página 1de 25

CHAPTER 1

INTRODUCTION

1.1 Background
Be an high school student is really exciting, especially been smanda highschool
student. we have so many taks and homework in a day. we have do our homework on
time. Because of it, so many students in Smanda do their homework until 00.00. Because
of it, we choose Alarm application for our Java netbeans project. We are going to make
alarm to help people who want to wake up on time.
1.2 Problem formula
1) What is the java program?
2) What is netbeans?
3) How to make alarm with Java Netbeans?
1.3 Purpose
a) To help people wake up early
b) To fullfill our task
c) To add our knowledge about java netbeans
1.4 Benefit
a) We can learn more about How to make an application
b) We can help the other people to solve their problem
c) We finish our task
CHAPTER II
SUBJECT MATTER

2. 1 Java Program
Java is a general-purpose computer programming language that is concurrent,
class-based, object-oriented, and specifically designed to have as few implementation
dependencies as possible. It is intended to let application developers "write once, run
anywhere" (WORA), meaning that compiled Java code can run on all platforms that
support Java without the need for recompilation. Java applications are typically compiled
to bytecode that can run on any Java virtual machine (JVM) regardless of computer
architecture. As of 2016, Java is one of the most popular programming languages in use,
particularly for client-server web applications, with a reported 9 million developers.
[citation needed] Java was originally developed by James Gosling at Sun Microsystems
(which has since been acquired by Oracle Corporation) and released in 1995 as a core
component of Sun Microsystems' Java platform. The language derives much of its syntax
from C and C++, but it has fewer low-level facilities than either of them.
The original and reference implementation Java compilers, virtual machines, and
class libraries were originally released by Sun under proprietary licences. As of May
2007, in compliance with the specifications of the Java Community Process, Sun
relicensed most of its Java technologies under the GNU General Public License. Others
have also developed alternative implementations of these Sun technologies, such as the
GNU Compiler for Java (bytecode compiler), GNU Classpath (standard libraries), and
IcedTea-Web (browser plugin for applets).
The latest version is Java 8, which is the only version currently supported for free
by Oracle, although earlier versions are supported both by Oracle and other companies on
a commercial basis.
The history of java come fromJames Gosling, Mike Sheridan, and Patrick
Naughton. They initiated the Java language project in June 1991. Java was originally
designed for interactive television, but it was too advanced for the digital cable television
industry at the time. The language was initially called Oak after an oak tree that stood
outside Gosling's office. Later the project went by the name Green and was finally
renamed Java, from Java coffee. Gosling designed Java with a C/C++-style syntax that
system and application programmers would find familiar.
Sun Microsystems released the first public implementation as Java 1.0 in 1995. It
promised "Write Once, Run Anywhere" (WORA), providing no-cost run-times on
popular platforms. Fairly secure and featuring configurable security, it allowed network-
and file-access restrictions. Major web browsers soon incorporated the ability to run Java
applets within web pages, and Java quickly became popular, while mostly outside of
browsers, that wasn't the original plan. In January 2016, Oracle announced that Java
runtime environments based on JDK 9 will discontinue the browser plugin. The Java 1.0
compiler was re-written in Java by Arthur van Hoff to comply strictly with the Java 1.0
language specification. With the advent of Java 2 (released initially as J2SE 1.2 in
December 1998 – 1999), new versions had multiple configurations built for different
types of platforms. J2EE included technologies and APIs for enterprise applications
typically run in server environments, while J2ME featured APIs optimized for mobile
applications. The desktop version was renamed J2SE. In 2006, for marketing purposes,
Sun renamed new J2 versions as Java EE, Java ME, and Java SE, respectively.
In 1997, Sun Microsystems approached the ISO/IEC JTC 1 standards body and
later the Ecma International to formalize Java, but it soon withdrew from the process.
Java remains a de facto standard, controlled through the Java Community Process. At one
time, Sun made most of its Java implementations available without charge, despite their
proprietary software status. Sun generated revenue from Java through the selling of
licenses for specialized products such as the Java Enterprise System.
On November 13, 2006, Sun released much of its Java virtual machine (JVM) as
free and open-source software, (FOSS), under the terms of the GNU General Public
License (GPL). On May 8, 2007, Sun finished the process, making all of its JVM's core
code available under free software/open-source distribution terms, aside from a small
portion of code to which Sun did not hold the copyright.
Sun's vice-president Rich Green said that Sun's ideal role with regard to Java was
as an "evangelist". Following Oracle Corporation's acquisition of Sun Microsystems in
2009–10, Oracle has described itself as the "steward of Java technology with a relentless
commitment to fostering a community of participation and transparency". This did not
prevent Oracle from filing a lawsuit against Google shortly after that for using Java inside
the Android SDK (see Google section below). Java software runs on everything from
laptops to data centers, game consoles to scientific supercomputers. On April 2, 2010,
James Gosling resigned from Oracle.
There were five primary goals in the creation of the Java language:
1) It must be "simple, object-oriented, and familiar".
2) It must be "robust and secure".
3) It must be "architecture-neutral and portable".
4) It must execute with "high performance".
5) It must be "interpreted, threaded, and dynamic".
As of 2015, only Java 8 is supported ("publicly"). Major release versions of Java,
along with their release dates:
1) JDK 1.0 (January 21, 1996)
2) JDK 1.1 (February 19, 1997)
3) J2SE 1.2 (December 8, 1998)
4) J2SE 1.3 (May 8, 2000)
5) J2SE 1.4 (February 6, 2002)
6) J2SE 5.0 (September 30, 2004)
7) Java SE 6 (December 11, 2006)
8) Java SE 7 (July 28, 2011)
9) Java SE 8 (March 18, 2014)
2. 2 Netbeans
NetBeans is a software development platform written in Java. The NetBeans
Platform allows applications to be developed from a set of modular software components
called modules. Applications based on the NetBeans Platform, including the NetBeans
integrated development environment (IDE), can be extended by third party developers.
The NetBeans IDE is primarily intended for development in Java, but also
supports other languages, in particular PHP, C/C++ and HTML5.
NetBeans is cross-platform and runs on Microsoft Windows, Mac OS X, Linux,
Solaris and other platforms supporting a compatible JVM.
The NetBeans Team actively support the product and seek feature suggestions
from the wider community. Every release is preceded by a time for Community testing
and feedback.
NetBeans began in 1996 as Xelfi (word play on Delphi), a Java IDE student
project under the guidance of the Faculty of Mathematics and Physics at Charles
University in Prague. In 1997, Roman Staněk formed a company around the project and
produced commercial versions of the NetBeans IDE until it was bought by Sun
Microsystems in 1999. Sun open-sourced the NetBeans IDE in June of the following
year. Since then, the NetBeans community has continued to grow. In 2010, Sun (and thus
NetBeans) was acquired by Oracle.
The NetBeans Platform is a framework for simplifying the development of Java
Swing desktop applications. The NetBeans IDE bundle for Java SE contains what is
needed to start developing NetBeans plugins and NetBeans Platform based applications;
no additional SDK is required.
Applications can install modules dynamically. Any application can include the
Update Center module to allow users of the application to download digitally signed
upgrades and new features directly into the running application. Reinstalling an upgrade
or a new release does not force users to download the entire application again.
The platform offers reusable services common to desktop applications, allowing
developers to focus on the logic specific to their application. Among the features of the
platform are:
1) User interface management (e.g. menus and toolbars)
2) User settings management
3) Storage management (saving and loading any kind of data)
4) Window management
5) Wizard framework (supports step-by-step dialogs)
6) NetBeans Visual Library
7) Integrated development tools

2. 3 How to Make Alarm with Java Netbeans Application

2. 4If you have worked with the NetBeans IDE in the past, much of this section will look familiar,
since the initial steps are similar for most projects. Still, the following steps describe settings
that are specific to this application, so take care to follow them closely.

2. 5 Step 1: Create a New Project


2. 6
Figure 2: File -> New Project (Click to Enlarge)

2. 7To create a new project, launch the NetBeans IDE and choose New Project from the File
menu. Note that keyboard shortcuts for each command appear on the far right of each menu
item. The look and feel of the NetBeans IDE may vary across platforms, but the functionality
will remain the same.

2. 8 Step 2: Choose General -> Java Application

2. 9
Figure 3: General -> Java Application (Click to Enlarge)

2. 10 Next, select General from the Categories column, and Java Application from the
Projects column. You may notice mention of "J2SE" in the description pane; that is just the
old name for what is now known as the "Java SE" platform. Press the button labeled "Next"
to proceed.

2. 11 Step 3: Set a Project Name

2. 12
Figure 4: New Java Application Window (Click to Enlarge)

2. 13 Now enter "CelsiusConverter" as the project name. You can leave the Project
Location and Project Folder fields set to their default values, or click the Browse button to
choose an alternate location on your system.

2. 14 Also make sure to deselect the "Create Main Class" checkbox. Leaving this option
selected generates a new class as the main entry point for the application, but our main GUI
window (created in the next step) will serve that purpose, so checking this box is not
necessary.

2. 15 Click the "Finish" button.


2. 16
Figure 5: The Resulting Main Window (Click to Enlarge)

2. 17 When the IDE finishes loading, you will see a screen similar to the above. All panes
will be empty except for the Projects pane in the upper left hand corner, which shows the
newly created CelsiusConverter project.

2. 18 Step 4: Add a JFrame Form

2. 19
Figure 6: New JFrame Form (Click to Enlarge)
2. 20 Right-click the CelsiusConverter project name and choose New -> JFrame Form
(JFrame is the Swing class responsible for the main frame for your application.) You will
learn how to designate this class as the application's entry point later in this tutorial, after the
application is complete.

2. 21 Step 5: Name the GUI Class

2. 22
Figure 7: New JFrame Form (Click to Enlarge)

2. 23 Next, type CelsiusConverterGUI as the class name, and celsiusconverter as


the package name. The remainder of the fields should automatically fill themselves in, as
shown above in Figure 7. Click the Finish button when you are done.

2. 24
Figure 8: The Design View (Click to Enlarge)
2. 25 When the IDE finishes loading, the right pane will display a design-time, graphical
view of the CelsiusConverterGUI. It is on this screen that you will visually drag, drop, and
manipulate the various Swing components to construct the application's GUI.

2. 26 NetBeans IDE Basics


2. 27 You do not need to learn every feature of the NetBeans IDE before exploring its GUI
creation capabilities. In fact, the only features that you really need to understand are
the Palette, the Design Area, the Property Editor, and theInspector.

2. 28 The Palette
2. 29 The Palette contains all of the components offered by Swing. You can probably
already deduce what many of these components do, even if this is your first exposure to
them (JLabel is a text label, JList is a drop-down list, etc.)

2. 30
Figure 9: The Palette (Click to Enlarge)

2. 31 From this list, the CelsiusConverterGUI will use only JLabel (a basic text
label), JTextField (for the user to enter the temperature), and JButton (to convert the
temperature from Celsius to Fahrenheit.) Future additions to this lesson may expand to cover
all of the components shown above, but for now we will keep things simple by focusing on
just these three.

2. 32 The Design Area


2. 33 The Design Area is where you will visually arrange your components after dragging
them from the Palette. It has two views: source view, and design view. Design view is the
default, as shown below in Figure 10. You can toggle between views at any time by clicking
their respective tabs at the top of the window.
2. 34
Figure 10: The Design Area, Design View (Click to Enlarge)

2. 35 The figure above shows a single JFrame object, which is represented by the large
blue shaded rectangle. Commonly expected behavior (such as quitting when the user clicks
the "close" button) is auto-generated by the IDE and appears in the source view within
uneditable blue sections of code called guarded blocks.

2. 36
Figure 11: Generated Source Code (Click to Enlarge)
2. 37 You may be surprised at the amount of code that has already been generated. Taking
a look at the source view reveals that the IDE has created a private method
named initComponents, which initializes the various components of the GUI. It tells the
application to "exit on close", performs some layout-specific tasks, then packs the (soon to
be added) components together on screen.

2. 38 To learn how to code these components by hand, see: How to Make Frames (Main
Windows) and Laying Out Components Within a Container.

2. 39 The Property Editor


2. 40 The Property Editor does what its name implies: it allows you to edit the properties of
any given component. The Property Editor is intuitive to use; in it you will see a series of
rows — one row per property — that you can click and edit without ever touching the source
code. The following screenshot shows the Property Editor for our newly
addedJFrame object:

2. 41
Figure 12: The Property Editor (Click to Enlarge)
2. 42 After you've complete this lesson, feel free to return to the Property Editor to
experiment with the various components that we have used.

2. 43 The Inspector
2. 44 The last component of the NetBeans IDE that we will use in this lesson is the
Inspector:
2. 45
Figure 13: The Inspector
2. 46 The Inspector provides a graphical representation of your application's components.
We will use the Inspector only once, to change a few variable names to something other than
their defaults.

2. 47 Creating the CelsiusConverter GUI


2. 48 This section explains how to use the NetBeans IDE to create the CelsiusConverter
GUI. As you drag each component from the Palette to the Design Area, the IDE will auto-
generate the appropriate source code.

2. 49 Step 1: Set the Title


2. 50 First, set the title of the application's JFrame to "Celsius Converter", as previously
shown in Figure 1.

2. 51 To do so, single-click the JFrame in the Inspector:

2. 52
Figure 14: Selecting the JFrame
2. 53 Then, set its title with the Property Editor:
2. 54
Figure 15: Setting the Title
2. 55 You can set the title by either double-clicking the title property and entering the new
text directly, or by clicking the button and entering the title in the provided field.

2. 56 Step 2: Add a JTextField


2. 57 Next, drag a JTextField from the Palette to the upper left corner of the Design
Area. As you approach the upper left corner, the GUI builder will will provide visual cues
(dashed lines) that suggest the appropriate spacing. Using these cues as a guide, drop
a JTextField into the upper left hand corner of the window as shown below:
2. 58
Figure 16: Adding the first JTextField (Click to Enlarge)
2. 59 You may be tempted to erase the default text "JTextField1", but just leave it in place
for now; we will replace it later in this lesson as we make the final adjustments to each
component.

2. 60 To learn how to code this component by hand, see How to Use Text Fields.

2. 61 Step 3: Add a JLabel


2. 62 Next, drag a JLabel onto the Design Area. Place it to the right of the JTextField,
again watching for visual cues that suggest an appropriate amount of spacing. Make sure
that text base for this component is aligned with that of theJTextField; the visual cues
provided by the IDE should make this easy to determine.
2. 63
Figure 17: Adding the JLabel (Click to Enlarge)

2. 64 To learn how to code this component by hand, see How to Use Labels.

2. 65 Step 4: Add a JButton


2. 66 Next, drag a JButton from the Palette and position it to the left and underneath
the JTextField. Again, the visual cues will help guide it into place.
2. 67
Figure 18: Adding the JButton (Click to Enlarge)

2. 68 You may be tempted to manually adjust the width of the JButton and JTextField,
but just leave them as they are for now. You will learn how to correctly adjust these
components later in this lesson under Adjusting the GUI Components.

2. 69 To learn how to code this component by hand, see How to Use Buttons.

2. 70 Step 5: Add a Second JLabel


2. 71
Figure 19: Adding a second JLabel (Click to Enlarge)

2. 72 Finally, add a second JLabel, repeating the process in step 2. Place this second
label to the right of the JButton, as shown above in Figure 19.

2. 73 Adjusting the GUI Components


2. 74 With the GUI components now in place, it is time to make the final adjustments.
There are a few different ways to do this; the order suggested here is just one possible
approach.

2. 75 Step 1: Set the Component Text


2. 76 First, double-click the JTextField and JButton to remove or replace the default
text that was inserted by the IDE. When you erase the text from the JTextField, it will
shrink in size as shown below in Figure 20. Change the text of the JButton from "JButton1"
to "Convert." Also make sure to change the top JLabel text to "Celsius" and the bottom to
"Fahrenheit."
2. 77
Figure 20: Setting the Component Text (Click to Enlarge)

2. 78 Step 2: Set the Component Size


2. 79 Next, shift-click the JTextField and JButton components; this will highlight each
showing that they are selected. Right-click (control-click for mac users) Same Size -> Same
Width. The components will now be the same width, as shown below in Figure 21. When you
perform this step, make sure that JFrame itself is not also selected. If it is, the Same Size
menu will not be active.
2. 80
Figure 21: Setting the JTextField and JButton sizes (Click to Enlarge)

2. 81 Step 3: Remove Extra Space


2. 82 Finally, grab the lower right-hand corner of the JFrame and adjust its size to
eliminate any extra whitespace. Note that if you eliminate all of the extra space (as shown
below) the title (which only appears at runtime) may not show completely. The end-user is
free to resize the application as desired, but you may want to leave some extra space on the
right side to make sure that everything fits correctly. Experiment, and use the screenshot of
the finished GUI (Figure 1) as a guideline.
2. 83
Figure 22: The Completed GUI (Click to Enlarge)
2. 84
The GUI portion of this application is now complete. If the NetBeans IDE has done its job
right, you should feel that constructing this GUI was a simple, if not trivial, task. But take a
minute to click on the Source tab; you might be surprised at the amount of code that has
been generated.

2. 85
Figure 23: The Generated Source Code (Click to Enlarge)

2. 86 Figure 23 above shows a partial listing of the generated source code. To see the
code in its entirety, click on the source tab and scroll up and down as necessary. You can
expand or collapse certain blocks of code (such as method bodies) by clicking the + or -
symbol on the left-hand side of the source editor.
2. 87 Adding the Application Logic
2. 88 So far, you have successfully created the GUI for the CelsiusConverter application. It
is now time to add in the application logic. The following code is all that is necessary to
convert a temperature from Celsius to Fahrenheit:

2. 89 //Parse degrees Celsius as a double and convert to Fahrenheit.

2. 90 int tempFahr = (int)


((Double.parseDouble(tempTextField.getText()))

2. 91 \* 1.8 + 32);

2. 92 fahrenheitLabel.setText(tempFahr + " Fahrenheit");

2. 93 Completing this application is mostly a matter of pasting the above code into the
correct location within the source code. There are, however, a couple of minor actions that
you must also take before this will actually work. They are: change the default variable
names, register the event listeners, and add the temperature conversion code.

2. 94 Step 1: Change the Default Variable Names


2. 95 Figure 24 below shows the default variable names as they currently appear within
the Inspector. For each component, the variable name appears first, followed by the object's
type in square brackets. For example,jTextField1 [JTextField] means that
"jTextField1" is the variable name and "JTextField" is its type.

2. 96
Figure 24: Default Variable Names
2. 97 The default names are not very relevant in the context of this application, so it makes
sense to change them from their defaults to something more meaningful. Right-click each
variable name and choose "Change variable name." When you are finished, the variable
names should appear as follows:
2. 98
Figure 25: Changed Variable Names
2. 99 The new variable names are "tempTextField", "celsiusLabel", "convertButton", and
"fahrenheitLabel." Each change that you make in the Inspector will automatically propagate
its way into the source code. You can rest assured that compilation will not fail due to typos
or mistakes of that nature — mistakes that are common when editing by hand.

2. 100 Step 2: Register the Event Listeners


2. 101 When an end-user interacts with a Swing GUI component (such as clicking the
Convert button), that component will generate a special kind of object (called an event
object) and will broadcast it to any other objects that have previously registered themselves
as listeners. If we were coding this application by hand, there would be a few additional steps
to register an event listener on the Convert button. The NetBeans IDE, however, makes this
process extremely simple:
2. 102
Figure 26: Registering an Event Listener (Click to Enlarge)

2. 103 In the Design Area, click on the Convert button to select it. Make sure that only the
Convert button is selected (if the JFrame itself is also selected, this step will not work.) Right-
click the Convert button and choose Events -> Action -> ActionPerformed. This will generate
the required event-handling code, leaving you with empty method bodies in which to add
your own functionality:

2. 104
Figure 27: Empty Listener Method Body (Click to Enlarge)

2. 105 There are many different event types to represent the various kinds of actions an
end-user can take (clicking the mouse triggers one type of event, typing at the keyboard
triggers another, moving the mouse yet another, and so on.) Our application is only
concerned with the ActionEvent; for more information about event handling, see Writing
Event Listeners.

2. 106 Step 3: Add the Temperature Conversion Code


2. 107 The final step is to simply paste the temperature conversion code into the empty
method body:

2. 108
Figure 28: Complete Listener Method Body (Click to Enlarge)
2. 109 With the conversion code now in place, the application is complete. All that remains
is to go ahead and actually run the program.

2. 110 Step 4: Run the Application


2. 111 Running the application is simply a matter of choosing Run -> Run Main Project. The
first time you run this application, you will be prompted with a dialog asking to
set CelsiusConverterGUI as the main class for this project. Click the OK button, and
when the program finishes compiling, you should see the application running in its own
window. You can run this program at any time using this same technique.

2. 112 Congratulations! You have completed your first Swing application!

1.

También podría gustarte