Write a following code in netbeans IDE 7.1 and save file as RegistrationForm.java and run it and you can able to set your look and feel in your application.
package registrationform;
/**
*
* @author NISH
*/
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class RegistrationForm extends JFrame implements ItemListener,ActionListener{
JLabel title,lookfeel;
JLabel l[]=new JLabel[7];
JTextField t[]=new JTextField[7];
JPasswordField tp;
String lbl[]=new String[7];
Container c;
JComboBox cb;
JRadioButton male,female;
JOptionPane jop;
ButtonGroup grp;
JButton breg,bcncl;
int vsp=80,hsp=100;
int spx,spy;
public RegistrationForm()
{
lbl[0]="Name :"; lbl[1]="Address :"; lbl[2]="Gender :"; lbl[3]="Email :"; lbl[4]="Phno :"; lbl[5]="Uname :"; lbl[6]="Password :";
c=this.getContentPane();
setLayout(null);
lookfeel=new JLabel("Select LookAndFill :");
lookfeel.setBounds(5, 5, 120, 20);
c.add(lookfeel);
cb=new JComboBox();
cb.setBounds(125, 5, 75, 20);
cb.addItem("Metal");
cb.addItem("Motif");
cb.addItem("Window");
c.add(cb);
title=new JLabel("Registration");
title.setFont(new Font("Times New Roman", NORMAL, 20));
title.setBounds(170, 30, 120,90);
c.add(title);
spy=30;
spx=100;
for(int i=0;i<=6;i++)
{
l[i]=new JLabel(lbl[i]);
l[i].setBounds(vsp,hsp , 80, 40);
hsp=hsp + spy;
c.add(l[i]);
}
male=new JRadioButton("Male");
female=new JRadioButton("Female");
vsp=80;
vsp=vsp+spx;
hsp=110;
for(int i=0;i<=6;i++)
{
t[i]=new JTextField("");
if(i==2)
{
male.setBounds(vsp,hsp, 60, 20);
c.add(male);
female.setBounds(vsp + 70,hsp, 100, 20);
c.add(female);
male.setSelected(true);
grp=new ButtonGroup();
grp.add(male);
grp.add(female);
}
else if(i==6)
{
tp=new JPasswordField();
tp.setBounds(vsp, hsp, 170, 20);
c.add(tp);
}
else
{
t[i].setBounds(vsp,hsp , 170, 20);
c.add(t[i]);
}
hsp=hsp + spy;
}
breg=new JButton("Submit");
breg.setBounds(110, hsp+spy, 90, 30);
c.add(breg);
bcncl=new JButton("Cancel");
bcncl.setBounds(220, hsp+spy, 90, 30);
c.add(bcncl);
cb.addItemListener(this);
breg.addActionListener(this);
bcncl.addActionListener(this);
}
public static void main(String[] args) {
RegistrationForm rg=new RegistrationForm();
rg.setTitle("Registration Form");
rg.setSize(450, 500);
rg.setResizable(false);
rg.show();
}
@Override
public void itemStateChanged(ItemEvent e) {
try {
if(cb.getSelectedItem().toString() == "Metal")
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
if(cb.getSelectedItem().toString() == "Motif")
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
if(cb.getSelectedItem().toString() == "Window")
{ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
SwingUtilities.updateComponentTreeUI(c);
}
catch(Exception ex)
{
}
}
@Override
public void actionPerformed(ActionEvent e) {
jop=new JOptionPane();
String str="";
if(e.getSource().equals(breg))
{
for(int i=0;i<=6;i++)
{
if(i==2)
{
if(male.isSelected())
{
str= str + lbl[i] + " " + "Male" + "\n";
}
else
{
str= str + lbl[i] + " " + "FeMale" + "\n";
}
}
else if(i==6)
{
str= str + lbl[i] + " " + tp.getText().toString() + "\n";
}
else
{
str= str + lbl[i] + " " + t[i].getText().toString() + "\n";
}
}
jop.showMessageDialog(c,str,null, JOptionPane.INFORMATION_MESSAGE);
}
for(int i=0;i<=6;i++)
{
t[i].setText("");
}
tp.setText("");
if(e.getSource().equals(bcncl))
{
for(int i=0;i<=6;i++)
{
t[i].setText("");
}
tp.setText("");
}
}
}Output:-
0 comments:
Post a Comment