[Go to site: main page, start]

0% found this document useful (0 votes)
9 views9 pages

Java GUI Programming with NetBeans

Uploaded by

BelayAbeneh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Java GUI Programming with NetBeans

Uploaded by

BelayAbeneh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Chapter One

Graphical User Interface and Java Database


connectivity
Creating Graphical User Interface (GUI) using NetBeans IDE
This chapter provides an introduction to Graphical User Interface (GUI) programming in java
with Swing components (Textfield, buttons, Labels, check box, etc) using NetBeans IDE. The
NetBeans IDE is a free, open-source, cross-platform integrated development environment with
built-in support for Java programming language.

The goal of this lesson is to introduce the Swing API (Application program Interface) by
designing a simple application that accept values from one textfield and display in second
textfield. Its GUI will be basic, focusing on only a subset of the available Swing components.
We will use the NetBeans IDE GUI builder, which makes user interface creation a simple matter
of drag and drop. Its automatic code generation feature simplifies the GUI development process,
letting you focus on the application logic instead the underlying infrastructure.

Because this lesson is a step-by-step checklist of specific actions to take, we recommend that you
run the NetBeans IDE and perform each step as you read along. This will be the quickest and
easiest way to begin programming with Swing.

Add Jframeform in your project and drag and drop four textfields, four labels and two buttons.
Let the name of four textfields are txt1, txt2, txt3 and txt4. Let also the caption of the labels are
String Input , Number Input ,Value 1 and Value 2. Let also the name of buttons are btndisplay
and btnclear. Make the caption of the two buttons as Display and Clear as shown below.

To give caption/label right click on the button or label or text field and select Edit Text.

To give name right click on textfield or button or label and select Change Variable Name.

1
Data validations for txt1 and txt2 textfield

Let txt1 must accept only letters and txt2 accept only numbers (0-9). Use the following codes to
do these validations.

//import [Link]; at the top of the class

//Letter validation,Right click on txt1,then point to Events then point to Key finally select KeyPessed

private void txt1KeyPressed([Link] evt) {

if( ([Link]() >= 'a' && [Link]() <= 'z')||([Link]() >= 'A' && [Link]() <=
'Z'))

[Link](true);
else {
[Link](false);
[Link](null," please enter only letter");
[Link](true);
}
}
//Number validation
private void txt2KeyPressed([Link] evt) {
if ([Link]() >= '0' && [Link]() <= '9')
[Link](true);

else {
[Link](false);
[Link](null," please enter only numeric digits(0-9)");
[Link](true);
}
}

2
//Code for btndisplay button
// Right click on btndisplay and point to Events then point to Action and then Actionperformed
private void btndisplayActionPerformed([Link] evt)
{
String s1=[Link]();
[Link](s1);
String s2=[Link]();
[Link](s2);
}

private void btnclearActionPerformed([Link] evt)


{
[Link](“ ”);
[Link](“ “);
[Link](“ “);
[Link](“ “);
}

Second Example: GUI Calculator

Design the following form and write for +,-,*,/,%,= and clear buttons action performed event.
Write also action performed event code for 0,1,2,3,4,5,6,7,8,9 and .(dot) buttons.

3
The operators +,-,*,/,% shoud be set at operator textfield and 1,2,3,4,5,6,7,8,9,0 and . should be
set at Num1 or Num2 textfield.Num1 textfield should be filled first and then the operator
textfield and finally the Num2 textfield.

//code for each button


private void btn1ActionPerformed([Link] evt) {
String str;
if([Link]().equals(""))
{ str = [Link]() + [Link]();
[Link](str);
} else
{ str= [Link]() + [Link]();
[Link](str); } }
private void btn2ActionPerformed([Link] evt) {
String str;
if([Link]().equals(""))
{ str = [Link]() + [Link]();
[Link](str);
} else
{ str= [Link]() + [Link]();
[Link](str); } }
private void btn3ActionPerformed([Link] evt) {
String str;
if([Link]().equals(""))
{ str = [Link]() + [Link]();

4
[Link](str);
} else
{ str= [Link]() + [Link]();
[Link](str); } }
private void btn4ActionPerformed([Link] evt) {
String str;
if([Link]().equals(""))
{ str = [Link]() + [Link]();
[Link](str);
} else
{ str= [Link]() + [Link]();
[Link](str); } }
private void btn5ActionPerformed([Link] evt) {
String str;
if([Link]().equals(""))
{ str = [Link]() + [Link]();
[Link](str);
} else
{ str= [Link]() + [Link]();
[Link](str); } }
private void btn6ActionPerformed([Link] evt) {
String str;
if([Link]().equals(""))
{ str = [Link]() + [Link]();
[Link](str);
} else
{ str= [Link]() + [Link]();
[Link](str); } }
private void btn7ActionPerformed([Link] evt) {
String str;
if([Link]().equals(""))
{ str = [Link]() + [Link]();
[Link](str);
} else
{ str= [Link]() + [Link]();
[Link](str); } }
private void btn8ActionPerformed([Link] evt) {
String str;
if([Link]().equals(""))
{ str = [Link]() + [Link]();
[Link](str);
} else
{ str= [Link]() + [Link]();
[Link](str); } }
private void btn9ActionPerformed([Link] evt) {
String str;

5
if([Link]().equals(""))
{ str = [Link]() + [Link]();
[Link](str);
} else
{ str= [Link]() + [Link]();
[Link](str); } }
private void btn0ActionPerformed([Link] evt) {
String str;
if([Link]().equals(""))
{ str = [Link]() + [Link]();
[Link](str);
} else
{ str= [Link]() + [Link]();
[Link](str); } }

private void btnaddActionPerformed([Link] evt) {

if([Link]().equals(""))[Link](null," Enter value in the first


textfield");

else{

[Link]("+");

[Link](true); } }

private void btnsubtActionPerformed([Link] evt) {

if([Link]().equals(""))[Link](null," Enter value in the first


textfield");

else{

[Link]("-");

[Link](true); } }

private void btnmultActionPerformed([Link] evt) {

6
if([Link]().equals(""))[Link](null," Enter value in the
first textfield");

else{

[Link]("*");

[Link](true); } }

private void btndivActionPerformed([Link] evt) {

if([Link]().equals(""))[Link](null," Enter value in the first


textfield");

else{

[Link]("/");

[Link](true); } }

private void btnmodActionPerformed([Link] evt) {

if([Link]().equals(""))[Link](null," Enter value in the first


textfield");

else{

[Link]("%");

[Link](true); }

private void btnequalActionPerformed([Link] evt) {


float val1=[Link]([Link]()); //to convert string to float
float val2= [Link]([Link]());
float sum=val1+val2;
float diff=val1-val2;
float prod=val1*val2;
float div=val1/val2;
float mod=val1%val2;
String oper=[Link]();
if([Link]("+"))
{ String sumres=[Link](sum); //to convert float to string

7
[Link](sumres);
}
else if([Link]("-"))
{ String diffres=[Link](diff);
[Link](diffres); }
else if([Link]("*"))
{ String prodres=[Link](prod);
[Link](prodres);
}
else if([Link]("/"))
{ String divres=[Link](div);
[Link](divres);
}
else if([Link]("%"))
{ String modres=[Link](mod);
[Link](modres);
}
}

private void btnclearActionPerformed([Link] evt) {


[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link](true);
}

private void btndotActionPerformed([Link] evt) {


String str;
if([Link]().equals(""))
{
str = [Link]() + [Link]();
[Link](str);
[Link](false);
}
else {
str = [Link]() + [Link]();
[Link](str);
[Link](false); }

8
9

You might also like