Example 1

 

import java.awt.event.*;

import javax.swing.*;

public class SwingExample{

SwingExample(){

JFrame jframe=new JFrame("This App contains almost all Elements of Swing");

jframe.setSize(600, 500);

jframe.setLocationRelativeTo(null);

jframe.setLayout(null);

jframe.setVisible(true);

//Name

JLabel lblName=new JLabel("Name: ");

lblName.setBounds(30, 12, 150, 10);

jframe.add(lblName);

JTextField txtName=new JTextField();

txtName.setBounds(100, 10, 150, 20);

jframe.add(txtName);

 

//Address

JLabel lblAddress=new JLabel("Address: ");

lblAddress.setBounds(300, 12, 150, 10);

jframe.add(lblAddress);

JTextField txtAddress=new JTextField();

txtAddress.setBounds(370, 10, 150, 20);

jframe.add(txtAddress);

 

//Class

JLabel lblClass=new JLabel("Class: ");

lblClass.setBounds(30, 60, 150, 10);

jframe.add(lblClass);

JComboBox<String> cmbClass=new JComboBox<String>();

cmbClass.addItem("Select Class");

cmbClass.addItem("BCA");

cmbClass.addItem("BBA");

cmbClass.addItem("MCA");

cmbClass.addItem("MBA");

 

/*String[] clas= {"BCA","BBA"};

JComboBox cmbClass=new JComboBox(clas);*/

cmbClass.setBounds(100, 55, 150, 20);

//cmb

Class.setEditable(true);

jframe.add(cmbClass);

//Gender

JLabel lblSex=new JLabel("Gender: ");

lblSex.setBounds(300,60,80,10);

jframe.add(lblSex);

 

ButtonGroup group1=new ButtonGroup();

JRadioButton chkMale=new JRadioButton("Male",true);

chkMale.setBounds(360, 55, 70, 20);

jframe.add(chkMale);

group1.add(chkMale);

 

JRadioButton chkFemale=new JRadioButton("Female");

chkFemale.setBounds(430, 55, 70, 20);

jframe.add(chkFemale);

group1.add(chkFemale);

 

//Shift

JLabel lblShift=new JLabel("Shift: ");

lblShift.setBounds(30,100,80,10);

jframe.add(lblShift);

 

String[] shift= {"Morning","Day","Evening"};

JList list1=new JList(shift);

list1.setBounds(100, 100, 100, 60);

jframe.add(list1);

 

//Remarks

JLabel lblRemarks=new JLabel("Remarks: ");

lblRemarks.setBounds(300,100,80,10);

jframe.add(lblRemarks);

JTextArea txtArea=new JTextArea();

txtArea.setBounds(400, 100, 150, 100);

jframe.add(txtArea);

 

//JTable

String data[][]={ {"101","Amit","670000"},

{"102","Jai","780000"},

{"101","Sachin","700000"}};

String column[]={"ID","NAME","SALARY"};

JTable jt=new JTable(data,column);

JScrollPane sp=new JScrollPane(jt);

sp.setBounds(30,200,250,80);

jframe.add(sp);

 

JButton click=new JButton("Submit");

click.setBounds(200,300,100,30);

jframe.add(click);

 

//creating menu

JMenuBar mb=new JMenuBar(); mb.setBounds(30, 350, 200, 20);

JMenu menu1=new JMenu("File");

JMenuItem item1,item3; item1=new JMenuItem("New");

JMenu item2=new JMenu("Open");

JMenuItem i1,i2;

i1=new JMenuItem("Open Project");

i2=new JMenuItem("Open File");

item2.add(i1);

item2.add(i2);

item3=new JMenuItem("Close");

menu1.add(item1);

menu1.add(item2);

menu1.add(item3);

mb.add(menu1);

 

JMenu menu2=new JMenu("Edit");

JMenuItem it1,it2;

it1=new JMenuItem("Copy");

it2=new JMenuItem("Paste");

menu2.add(it1);

menu2.add(it2);

mb.add(menu2);

jframe.add(mb);

click.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

 

//get textfield

String name=txtName.getText();

 

//get combo

String clas=cmbClass.getSelectedItem().toString();

//get radio + checkbox (same process)

String gender="";

if(chkMale.isSelected())

gender=chkMale.getText();

else

gender=chkFemale.getText();

 

//getList

String shift=list1.getSelectedValuesList().toString();

 

//get textarea

String remarks=txtArea.getText(); JOptionPane.showMessageDialog(null, "Remarks: "+remarks);

}

});

 

//can use action listener also

cmbClass.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED) JOptionPane.showMessageDialog(null, cmbClass.getSelectedItem().toString());

}

}

});

 

// listener on menu item

//can use item listener also

item1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) { JOptionPane.showMessageDialog(null, item1.getText().toString()+" Clicked!");

}

});

}

public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() {

public void run() {

new SwingExample();

}

});

}

}

Example 2

                             

import javax.swing.*;

import java.awt.event.*;

public class SwingDemo {

SwingDemo(){

JFrame jframe=new JFrame("This is a simple JFrame App");

jframe.setSize(400, 300);

jframe.setLocationRelativeTo(null);

jframe.getContentPane().setLayout(null);

jframe.setVisible(true);

JLabel lbl1=new JLabel("First Number:");

lbl1.setBounds(20, 10, 100, 10);

jframe.add(lbl1);

 

JTextField txt1=new JTextField();

txt1.setBounds(120, 10, 120, 20);

jframe.add(txt1);

 

JLabel lbl2=new JLabel("Second Number:");

lbl2.setBounds(20, 50, 100, 10);

jframe.add(lbl2);

 

JTextField txt2=new JTextField(); txt2.setBounds(120, 50, 120, 20); jframe.add(txt2);

 

JLabel lbl3=new JLabel("Result: ");

//lbl3.setText("Result: ");

 

lbl3.setBounds(20,80,100,30);

jframe.add(lbl3);

 

JButton btn=new JButton("Calculate");

btn.setBounds(100, 120, 100, 30);

jframe.add(btn);

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

String first1=txt1.getText().toString();

String second1=txt2.getText().toString();

int a,b,c;

a=Integer.parseInt(first1);

b=Integer.parseInt(second1);

c=a+b;

lbl3.setText("Result: "+c);

 

//JOptionPane.showMessageDialog(null, "Addition= "+c);

}

});

}

 

public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() {

public void run() {

new SwingDemo();

}

});

 

}

}