Ryan's District Boards
September 03, 2010, 06:23:32 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Other sites in our network:
You are not allowed to view links.
Register or Login and You are not allowed to view links.
Register or Login (new) << free mp3 download   and many more sites
 
   Home   Help Search Calendar Shop Login Register  
Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: [Java tutorial] A simple if else..  (Read 5371 times)
0 Members and 1 Guest are viewing this topic.
automatic_poster
Newbie
*

Reputation: 1
Offline Offline

Gender: Female
Posts: 13
8099.33 RD$

View Inventory
Send Money to automatic_poster
Ryan's District


View Profile WWW
« on: May 19, 2006, 10:42:13 PM »

 
If else? What are "if elses" and why do you use it?

Example:

When coding a java application you often use awt or swing components like a Button, Label etc
Assume you have two buttons in your GUI and want to know which one you have pressed on. With the use of the ActionListener you can define what the button does after you clicked it.

I'm now going to show you some code so you see how it works, let's write a simple java application shall we?

First we need to declare two buttons for the if else later on the guide:
Code

GeSHi (java):
Private JButton b1,b2;
Created by GeSHI 1.0.7.5


After this we must add  an actionlister for each button:

Code

GeSHi (java):
b1.addActionListener(this);
b1.addActionListener(this));
Created by GeSHI 1.0.7.5


We have 2 JButtons now. So how do we tell the computer on which button we clicked?
Well.. this is the part we use the "if else" statement.

Let's say you clicked on b1, and you want a message that tells you that you clicked the button, same goes for b2. We do this with the following code:

Code

GeSHi (java):
public actionPerformed(ActionEvent e) {
if (e.getSource() ==b1) {
//do b1
System.out.println("Button1");
}
if (e.getSource() == b2) {
//do b2
System.out.println("Button2");
}
else {
//do something else
}
Created by GeSHI 1.0.7.5


I Assume the code shouldn't be too hard to understand. "If" you have a question, just post it here, i can code you a working if else application if you want so you can test it our yourself.


Credit : by You are not allowed to view links.
Register or Login
www.happyserver.be
Logged

All my posts are from You are not allowed to view links.
Register or Login
www.happyserver.be
Ryan's District Boards
« on: May 19, 2006, 10:42:13 PM »

 Logged
MojoFighter
Newbie
*

Reputation: 0
Offline Offline

Posts: 1
2.60 RD$

View Inventory
Send Money to MojoFighter

View Profile
« Reply #1 on: March 27, 2010, 06:14:23 AM »

oh no what happened to happyserver
Logged

You are not allowed to view links.
Register or Login
party supplies
Ryan's District Boards
   

 Logged
Pages: [1]   Go Up
  Send this topic  |  Print  

 
Jump to:  

.
Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.061 seconds with 28 queries.