0% found this document useful (0 votes)
155 views5 pages

JAVA Swing Codes-Www - Edulanka.lk

The document contains 3 code samples demonstrating the use of Java Swing components to create simple graphical user interfaces (GUIs). Code 1 creates labels and text fields to display and edit name, address, and phone number information. Code 2 creates labels and text fields for selecting and displaying different colors. Code 3 is similar and creates a color selection GUI. The codes are presented on the EduLanka online education website along with contact information.

Uploaded by

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

JAVA Swing Codes-Www - Edulanka.lk

The document contains 3 code samples demonstrating the use of Java Swing components to create simple graphical user interfaces (GUIs). Code 1 creates labels and text fields to display and edit name, address, and phone number information. Code 2 creates labels and text fields for selecting and displaying different colors. Code 3 is similar and creates a color selection GUI. The codes are presented on the EduLanka online education website along with contact information.

Uploaded by

priyantha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

www.edulanka.

com - EduLanka Sample Java Codes - ONLINE JAVA

JAVA Swings Sample Codes


CODE NO: 1

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class aaa extends JApplet implements ActionListener
{
JLabel lblName,lblAddress,lblTelno;
JTextField txtName,txtAddress,txtTelno;
JButton btnDisplay,btnClear;
public void init()
{
lblName=new JLabel("Name");
lblAddress=new JLabel("Address");
lblTelno=new JLabel("Tel -No");
txtName=new JTextField();
txtAddress=new JTextField();
txtTelno=new JTextField();
btnDisplay=new JButton("Display");
btnClear=new JButton("Clear");
setLayout(null);
lblName.setBounds(30,30,100,30);
lblAddress.setBounds(30,70,100,30);
lblTelno.setBounds(30,100,100,30);

EduLanka Online Education ---------- 1 --------------

2007 2008 All rights received.

www.edulanka.com - EduLanka Sample Java Codes - ONLINE JAVA


txtName.setBounds(150,30,200,30);
txtAddress.setBounds(150,70,200,30);
txtTelno.setBounds(150,100,100,30);
btnDisplay.setBounds(50,175,120,30);
btnClear.setBounds(220,175,120,30);
add(lblName);
add(txtName);
add(lblAddress);
add(txtAddress);
add(lblTelno);
add(txtTelno);
add(btnDisplay);
add(btnClear);
btnDisplay.addActionListener(this);
btnClear.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnDisplay)
{
txtName.setText("My Name");
txtAddress.setText("Edulanka Alawathugoda");
txtTelno.setText("000000000");
}
else
{
txtName.setText("");
txtAddress.setText("");
txtTelno.setText("");
}
}
}//end class
//<applet code="aaa.class" width=450 height=250></applet>

CODE NO: 2

EduLanka Online Education ---------- 2 --------------

2007 2008 All rights received.

www.edulanka.com - EduLanka Sample Java Codes - ONLINE JAVA

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ex extends JApplet implements ActionListener
{
JLabel lblSc,lblRed,lblGreen,lblBlue,lblYellow,lblChoice;
JTextField txtChoice,txtOutput;
JButton btnShow;
public void paint(Graphics a)
{a.drawRect(20,20,350,250);
}
public void init()
{
lblSc=new JLabel("Selecting Colors");
lblRed=new JLabel("1. Red");
lblGreen=new JLabel("2. Green");
lblBlue=new JLabel("3. Blue");
lblYellow=new JLabel("4. Yellow");
lblChoice=new JLabel("Choice");
txtChoice=new JTextField();
txtOutput=new JTextField();
btnShow=new JButton("Show");
setLayout(null);
lblSc.setBounds(30,30,200,20);
lblRed.setBounds(50,50,60,20);
lblGreen.setBounds(50,80,60,20);
lblBlue.setBounds(50,110,60,20);
lblYellow.setBounds(50,140,60,20);
lblChoice.setBounds(30,160,60,30);
txtChoice.setBounds(90,160,30,30);
txtOutput.setBounds(30,200,300,30);
btnShow.setBounds(180,160,100,30);
add(lblSc);
add(lblRed);
add(lblGreen);
add(lblBlue);
add(lblYellow);
add(lblChoice);
add(txtChoice);
add(txtOutput);
add(btnShow);
btnShow.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnShow)
{

EduLanka Online Education ---------- 3 --------------

2007 2008 All rights received.

www.edulanka.com - EduLanka Sample Java Codes - ONLINE JAVA


int Choice=Integer.parseInt(txtChoice.getText());
{
}
{
}
{
}
{

if(Choice==1)
txtOutput.setBackground(Color.red);
txtOutput.setText("You have selected RED color");
if(Choice==2)
txtOutput.setBackground(Color.green);
txtOutput.setText("You have selected GREEN color");
if(Choice==3)
txtOutput.setBackground(Color.blue);
txtOutput.setText("You have selected BLUE color");
if(Choice==4)
txtOutput.setBackground(Color.yellow);
txtOutput.setText("You have selected YELLOW color");

}
}
}
}
//<applet code="ex.class" width=450 height=250></applet>
.

CODE NO: 3

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class selcol extends JApplet implements ActionListener
{
JLabel lblSc,lblRed,lblGreen,lblBlue,lblYellow,lblChoice;
JTextField txtChoice,txtOutput;
JButton btnShow;
public void init()
{
lblSc=new JLabel("Selecting Colors");
lblRed=new JLabel("1. Red");
lblGreen=new JLabel("2. Green");
lblBlue=new JLabel("3. Blue");
lblYellow=new JLabel("4. Yellow");
lblChoice=new JLabel("Choice");
txtChoice=new JTextField();
txtOutput=new JTextField();
btnShow=new JButton("Show");
setLayout(null);
lblSc.setBounds(30,30,200,20);

EduLanka Online Education ---------- 4 --------------

2007 2008 All rights received.

www.edulanka.com - EduLanka Sample Java Codes - ONLINE JAVA


lblRed.setBounds(50,50,60,20);
lblGreen.setBounds(50,80,60,20);
lblBlue.setBounds(50,110,60,20);
lblYellow.setBounds(50,140,60,20);
lblChoice.setBounds(30,160,60,30);
txtChoice.setBounds(90,160,30,30);
txtOutput.setBounds(30,200,300,30);
btnShow.setBounds(180,160,100,30);
add(lblSc);
add(lblRed);
add(lblGreen);
add(lblBlue);
add(lblYellow);
add(lblChoice);
add(txtChoice);
add(txtOutput);
add(btnShow);
btnShow.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnShow)
{
int Choice=Integer.parseInt(txtChoice.getText());
{
}
{
}
{
}
{

if(Choice==1)
txtOutput.setBackground(Color.red);
txtOutput.setText("You have selected RED color");
if(Choice==2)
txtOutput.setBackground(Color.green);
txtOutput.setText("You have selected GREEN color");
if(Choice==3)
txtOutput.setBackground(Color.blue);
txtOutput.setText("You have selected BLUE color");
if(Choice==4)
txtOutput.setBackground(Color.yellow);
txtOutput.setText("You have selected YELLOW color");

}
}
}
}
//<applet code="selcol.class" width=450 height=250></applet>

EduLanka.com On line Education school of Sri Lanka.


www.edulanka.com
[email protected]

EduLanka Online Education ---------- 5 --------------

2007 2008 All rights received.

You might also like