I tried to tell more in detail from start Java Netbeans to a simple calculator program completion. Make sure the Netbeans IDE it is installed properl
Create a New Application Project
Open Netbeans IDE program is already installed on the PC.
Click File -> New Project
Select a category Java, and Java Application project
Then click Next>
The name of our project now is also the name sesaui desire KalkulatorSederhana.bisa friend of all.
Tip: To project name I suggest not to use spaces.
Save the project site in a secure and convenient folder. hehe
Uncheck the Create Main Class. Because at the time of making the program we'll use JFrame class as the main class.
click Finish
The new project has been created.
Preparation Java application interface design
After creating a new project, the next is to design a calculator display that we will create.
Right-click on the package <default package> -> New -> JFrame Form
Fill in the name of the class with TampilanKalkulator. There should be no spaces in the name of the class.
Then click Finish.
To make it easier to make the design looks, I saranin use Absolute Layout Layout Manager. Previously, make sure Window Navigator is activated (Window -> Navigating -> Navigator).
The way to change the layout manager, click the Design tab in the document and look at the window TampilanKalkulator.java Navigator (left of screen) -> Right click on the [JFrame] -> Set Layout -> Absolute Layout
Designing an application view
Now is the time to design the look of calculator application. Simply drag and drop from the window palette (Window -> Palette) in the screen right into our TampilanKalkulator.java Design, slide the upper left lower right length.
To change the text and the name of the variable component of the way:
Right-click on the component to be in modif -> Edit Text (to change the text that appears) or Change Variable Name (To change the name of the variable component)
In order to comply with kodingan that I've created, change the Variable Name of each component corresponding variable names list below. * Be careful, kava is a great means of small letters CaseSensitive affect kodingan.
Write java code
When finished making the design interface, now satanya fill his kodingan Java.
First of all is to define the type of data
Click the Source tab in the document TampilanKalkulator.java
Write the definition of variables that we need right under
public class extends TampilanKalkulator javax.swing.JFrame {
String of numbers;
double the amount, number1, angka2;
int select;
To fill kodingan, by default stay Double Click on the desired component then we directly use Event actionPerformed (long right-click on the desired component -> Events -> Action -> actionPerformed). This means that if a user Mass clicking the button will run the java code that we wrote in the actionPerformed object.
These are codes that are to be written
number + = "x";
display.setText (number);
x according to the numbers that are shown in the display (JTextField)
mean the code above is if the button is pressed, the number will be certain to add one digit number to a number of variables. Then change the text to display the content of a variable number.
The comma (,)
number + = ".";
display.setText (number);
Adding symbols comma / point
The C (Clear)
display.setText ("");
number1 = 0.0;
angka2 = 0.0;
number = 0.0;
number = "";
Ensure all components and variables zero / null.
Button (+)
number1 = Double.parseDouble (number);
display.setText ("+");
number = "";
select = 1;
Variable number1 constitute the numbers that will be operated with variable angka2. Because we declare a variable number1 Double data type, then the variable number of type String data must be first converted to a Double.
Change the text on the display becomes the symbol +.
Variable numbers emptied back to accommodate String numbers will be assigned to the variable angka2.
Select = 1 is an integer that serves for the selection of action in case the switch on if the key is equal to (=) is pressed.
For a number of other operating buttons to adjust as a code on the button (+)
Button (-)
number1 = Double.parseDouble (number);
display.setText ("-");
number = "";
select = 2;
Button (x)
number1 = Double.parseDouble (number);
display.setText ("*");
number = "";
select = 3;
Button (/)
number1 = Double.parseDouble (number);
display.setText ("/");
number = "";
select = 4;
Button (=)
switch (select) {
case 1:
angka2 = Double.parseDouble (number);
number = number1 + angka2;
number = Double.toString (number);
display.setText (number);
break;
case 2:
angka2 = Double.parseDouble (number);
number = number1 - angka2;
number = Double.toString (number);
display.setText (number);
break;
case 3:
angka2 = Double.parseDouble (number);
number = number1 * angka2;
number = Double.toString (number);
display.setText (number);
break;
case 4:
angka2 = Double.parseDouble (number);
number = number1 / angka2;
number = Double.toString (number);
display.setText (number);
break;
default:
break;
}
Before surgery performed numbers, angka2 variables must be converted first to a Double data type.
Variable number of operations and angka2 number1 number that was converted to a Double data type.
Variable numbers convert the variable amount of data type double to a String that can be displayed on the display.Completion Display
After kodingan finished, need to add some rules that we have set in the interface so that the program more regularly and avoid errors caused by the user program.
Property organize JFrame
Right-click in the area (container) desingn JFramenya -> Properties.
Fill JFrame title.
Uncheck resizable. It is intended that at the time in the Run program user can not change the length or width of the window.
Property organize JTextField
Right click on the JTextField component inside a JFrame container -> Properties
Uncheck editable. So that the user can not enter the characters directly from the keyboard. Avoid unwanted characters into the display.
Horizontal Alignment change to Right. So that the position of the figure to the right flat as calculators in general.
running Applications
Finally, it is run Java applications.
Click Run -> Run Project or Run File.
keyword:
simple calculator, simple interest calculator, simple mortgage calculator, simple loan calculator, simple savings calculator, simple tax calculator, online calculator, calculator online, scientific calculator, calculater, free online calculator, free calculator, online scientific calculator, caculator

No comments:
Post a Comment