GUI
whats wrong with my code?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class prog3 extends JFrame
{
JLabel lbl,lbl1,lbl2,lbl3,lbl4,lbl5,lbl6,lbl7,lb18,lbl9,lbl10,lbl11,lbl12;
JTextField txt,txt1,txt2,txt3,txt4,txt5,txt6,txt7,txt8,txt9,txt10,txt11;
JTextArea area;
//JComboBox cb;
//JButton btn1,btn2,btn3,btn4,btn5,btn6;
SpringLayout lay;
public prog3()
{
super("Employee Details");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(350,300);
lay=new SpringLayout();
setLayout(lay);
widgets();
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new prog3().setVisible(true);
}
});
}
public void widgets()
{
lbl=new JLabel("Patient ID:");
txt=new JTextField(12);
lbl1=new JLabel("First Name:");
txt1=new JTextField(12);
lbl2=new JLabel("Last Name:");
txt2=new JTextField(12);
lbl3=new JLabel("Gender:");
txt3=new JTextField(12);
lbl4=new JLabel("Date Of Birth:");
txt4=new JTextField(12);
lbl5=new JLabel("Address:");
area=new JTextArea(10,8);
lbl6=new JLabel("Phone Number:");
txt5=new JTextField(12);
lbl7=new JLabel("Patient Occupation:");
txt6=new JTextField(12);
lbl8=new JLabel("Marital Status:");
txt7=new JTextField(12);
lbl9=new JLabel("Account Type:");
txt8=new JTextField(12);
lbl10=new JLabel("Company Name");
txt9=new JTextField(12);
lbl11=new JLabel("Age:");
txt10=new JTextField(12);
lbl12=new JLabel("Weight:");
txt11=new JTextField(12);
getContentPane().add(lbl1);
getContentPane().add(txt1);
getContentPane().add(lbl);
getContentPane().add(txt);
getContentPane().add(lbl2);
getContentPane().add(txt2);
getContentPane().add(lbl3);
getContentPane().add(txt3);
getContentPane().add(lbl4);
getContentPane().add(txt4);
getContentPane().add(lbl5);
getContentPane().add(txt5);
getContentPane().add(lbl6);
getContentPane().add(txt5);
getContentPane().add(lbl7);
getContentPane().add(txt6);
getContentPane().add(lbl8);
getContentPane().add(txt7);
getContentPane().add(lbl9);
getContentPane().add(txt8);
getContentPane().add(lbl10);
getContentPane().add(txt9);
getContentPane().add(lbl11);
getContentPane().add(txt10);
getContentPane().add(lbl12);
getContentPane().add(txt11);
//adjust the label to 5,5
lay.putConstraint(SpringLayout.WEST,lbl,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl,5,SpringLayout.NORTH,getContentPane());
//adjust the textfield to +5,5
lay.putConstraint(SpringLayout.WEST,txt,5,SpringLayout.EAST,lbl);
lay.putConstraint(SpringLayout.NORTH,txt,5,SpringLayout.NORTH,getContentPane());
//lbl1
lay.putConstraint(SpringLayout.WEST,lbl1,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl1,5,SpringLayout.SOUTH,lbl);
//txt1
lay.putConstraint(SpringLayout.WEST,txt1,5,SpringLayout.EAST,lbl1);
lay.putConstraint(SpringLayout.NORTH,txt1,5,SpringLayout.SOUTH,txt);
//lbl2
lay.putConstraint(SpringLayout.WEST,lbl2,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl2,5,SpringLayout.SOUTH,lb12);
//txt2
lay.putConstraint(SpringLayout.WEST,txt2,5,SpringLayout.EAST,lbl2);
lay.putConstraint(SpringLayout.NORTH,txt2,5,SpringLayout.SOUTH,txt);
//lbl3
lay.putConstraint(SpringLayout.WEST,lbl3,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl3,5,SpringLayout.SOUTH,lbl3);
//txt3
lay.putConstraint(SpringLayout.WEST,txt3,5,SpringLayout.EAST,lbl3);
lay.putConstraint(SpringLayout.NORTH,txt3,5,SpringLayout.SOUTH,txt);
//lbl4
lay.putConstraint(SpringLayout.WEST,lbl4,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl4,5,SpringLayout.SOUTH,lbl4);
//txt4
lay.putConstraint(SpringLayout.WEST,txt4,5,SpringLayout.EAST,lbl4);
lay.putConstraint(SpringLayout.NORTH,txt4,5,SpringLayout.SOUTH,txt);
//lbl5
lay.putConstraint(SpringLayout.WEST,lbl5,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl5,5,SpringLayout.SOUTH,lbl5);
//area
lay.putConstraint(SpringLayout.WEST,area(10,8),5,SpringLayout.EAST,lbl5);
lay.putConstraint(SpringLayout.NORTH,area(10,8),5,SpringLayout.SOUTH,area);
//lbl6
lay.putConstraint(SpringLayout.WEST,lbl6,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl6,5,SpringLayout.SOUTH,lbl6);
//txt5
lay.putConstraint(SpringLayout.WEST,txt5,5,SpringLayout.EAST,lbl6);
lay.putConstraint(SpringLayout.NORTH,txt5,5,SpringLayout.SOUTH,txt);
//lbl7
lay.putConstraint(SpringLayout.WEST,lbl7,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl7,5,SpringLayout.SOUTH,lbl7);
//txt6
lay.putConstraint(SpringLayout.WEST,txt6,5,SpringLayout.EAST,lbl7);
lay.putConstraint(SpringLayout.NORTH,txt6,5,SpringLayout.SOUTH,txt);
//lbl8
lay.putConstraint(SpringLayout.WEST,lbl8,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl8,5,SpringLayout.SOUTH,lbl8);
//txt7
lay.putConstraint(SpringLayout.WEST,txt7,5,SpringLayout.EAST,lbl8);
lay.putConstraint(SpringLayout.NORTH,txt7,5,SpringLayout.SOUTH,txt);
//lbl9
lay.putConstraint(SpringLayout.WEST,lbl9,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl9,5,SpringLayout.SOUTH,lbl9);
//txt8
lay.putConstraint(SpringLayout.WEST,txt8,5,SpringLayout.EAST,lbl9);
lay.putConstraint(SpringLayout.NORTH,txt8,5,SpringLayout.SOUTH,txt);
//lbl10
lay.putConstraint(SpringLayout.WEST,lbl10,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl10,5,SpringLayout.SOUTH,lbl10);
//txt9
lay.putConstraint(SpringLayout.WEST,txt9,5,SpringLayout.EAST,lbl10);
lay.putConstraint(SpringLayout.NORTH,txt9,5,SpringLayout.SOUTH,txt);
//lbl11
lay.putConstraint(SpringLayout.WEST,lbl11,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl11,5,SpringLayout.SOUTH,lbl11);
//txt10
lay.putConstraint(SpringLayout.WEST,txt10,5,SpringLayout.EAST,lbl1);
lay.putConstraint(SpringLayout.NORTH,txt10,5,SpringLayout.SOUTH,txt);
//lbl12
lay.putConstraint(SpringLayout.WEST,lbl12,5,SpringLayout.WEST,getContentPane());
lay.putConstraint(SpringLayout.NORTH,lbl12,5,SpringLayout.SOUTH,lbl12);
//txt11
lay.putConstraint(SpringLayout.WEST,txt11,5,SpringLayout.EAST,lbl12);
lay.putConstraint(SpringLayout.NORTH,txt11,5,SpringLayout.SOUTH,txt);
}
}
View Answers
September 24, 2010 at 2:45 PM
Hi Friend,
We have modified your code, try this:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class prog3 extends JFrame{
JLabel lbl,lbl1,lbl2,lbl3,lbl4,lbl5,lbl6,lbl7,lbl8,lbl9,lbl10,lbl11,lbl12;
JTextField txt,txt1,txt2,txt3,txt4,txt5,txt6,txt7,txt8,txt9,txt10,txt11;
JTextArea area;
SpringLayout lay;
public prog3(){
super("Employee Details");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(350,500);
setVisible(true);
widgets();
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new prog3().setVisible(true);
}
});
}
public void widgets(){
lbl=new JLabel("Patient ID:");
txt=new JTextField(12);
lbl1=new JLabel("First Name:");
txt1=new JTextField(12);
lbl2=new JLabel("Last Name:");
txt2=new JTextField(12);
lbl3=new JLabel("Gender:");
txt3=new JTextField(12);
lbl4=new JLabel("Date Of Birth:");
txt4=new JTextField(12);
lbl5=new JLabel("Address:");
area=new JTextArea(4,12);
lbl6=new JLabel("Phone Number:");
txt5=new JTextField(12);
lbl7=new JLabel("Patient Occupation:");
txt6=new JTextField(12);
lbl8=new JLabel("Marital Status:");
txt7=new JTextField(12);
lbl9=new JLabel("Account Type:");
txt8=new JTextField(12);
lbl10=new JLabel("Company Name");
txt9=new JTextField(12);
lbl11=new JLabel("Age:");
txt10=new JTextField(12);
lbl12=new JLabel("Weight:");
txt11=new JTextField(12);
add(lbl);
add(txt);
add(lbl1);
add(txt1);
add(lbl2);
add(txt2);
add(lbl3);
add(txt3);
add(lbl4);
add(txt4);
add(lbl5);
add(area);
add(lbl6);
add(txt5);
add(lbl7);
add(txt6);
add(lbl8);
add(txt7);
add(lbl9);
add(txt8);
add(lbl10);
add(txt9);
add(lbl11);
add(txt10);
add(lbl12);
add(txt11);
lay=new SpringLayout();
September 24, 2010 at 2:47 PM
continue..
lay.putConstraint(SpringLayout.WEST,lbl,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl,10,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt,10,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl1,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl1,40,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt1,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt1,40,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl2,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl2,70,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt2,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt2,70,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl3,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl3,100,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt3,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt3,100,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl4,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl4,130,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt4,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt4,130,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl5,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl5,160,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,area,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,area,160,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl6,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl6,230,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt5,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt5,230,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl7,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl7,260,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt6,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt6,260,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl8,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl8,290,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt7,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt7,290,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl9,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl9,320,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt8,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt8,320,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl10,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl10,350,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt9,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt9,350,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl11,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl11,380,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt10,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt10,380,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,lbl12,10,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,lbl12,410,SpringLayout.NORTH,this);
lay.putConstraint(SpringLayout.WEST,txt11,150,SpringLayout.WEST,this);
lay.putConstraint(SpringLayout.NORTH,txt11,410,SpringLayout.NORTH,this);
setLayout(lay);
}
}
Thanks