JAVA Swing Codes-Www - Edulanka.lk
JAVA Swing Codes-Www - Edulanka.lk
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);
CODE NO: 2
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)
{
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);
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>