You are on page 1of 10

UNIT 3

SWING UI ELEMENTS

1. Java Program to create a blank frame and display it.


import java.awt.event.*;
import javax.swing.*;
class text2 extends JFrame {
// frame
static JFrame f;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("NewFrame");

f.show(); // display the frame

}
}

2. Java Program to create a blank label and add text to it.


import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame {
// frame
static JFrame f;
// label to display text
static JLabel l;
// default constructor
text()
{
}

// main class
public static void main(String[] args)
{
// create a new frame to store text field and button
f = new JFrame("label");
// create a label to display text
l = new JLabel();

// add text to label


l.setText("label text");

// create a panel
JPanel p = new JPanel();

// add label to panel


p.add(l);

// add panel to frame


f.add(p);

// set the size of frame


f.setSize(300, 300);

f.show();
}
}

3. Creation of a Button and display some text when clicked on the button.
import javax.swing.*;
public class text3 {
public static void main(String[ ] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
4. Creation of textbox and displaying some text in the textbox :
import javax.swing.*;
class text4
{
public static void main(String args[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1;
t1=new JTextField("Welcome to Textfield Example.");
t1.setBounds(50,100, 200,30);
f.add(t1);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

5. Creation of Checkbox and displaying the name of the checkbox:

import javax.swing.*;
public class text5
{
text5(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new text5();
}
}
6.Creation of radio button :
import javax.swing.*;
import java.awt.event.*;

class text6 extends JFrame {

// Declaration of object of JRadioButton class.


JRadioButton jRadioButton1;

// Declaration of object of JRadioButton class.


JRadioButton jRadioButton2;

// Declaration of object of JButton class.


JButton jButton;

// Declaration of object of ButtonGroup class.


ButtonGroup G1;

// Declaration of object of JLabel class.


JLabel L1;

// Constructor of Demo class.


public text6()
{

// Setting layout as null of JFrame.


this.setLayout(null);

// Initialization of object of "JRadioButton" class.


jRadioButton1 = new JRadioButton();

// Initialization of object of "JRadioButton" class.


jRadioButton2 = new JRadioButton();

// Initialization of object of "ButtonGroup" class.


G1 = new ButtonGroup();

// Initialization of object of " JLabel" class.


L1 = new JLabel("Qualification");

// setText(...) function is used to set text of radio button.


// Setting text of "jRadioButton1".
jRadioButton1.setText("Under-Graduate");

// Setting text of "jRadioButton2".


jRadioButton2.setText("Graduate");

// Setting Bounds of "jRadioButton1".


jRadioButton1.setBounds(120, 30, 120, 50);

// Setting Bounds of "jRadioButton2".


jRadioButton2.setBounds(250, 30, 80, 50);

// Setting Bounds of JLabel "L1".


L1.setBounds(20, 30, 150, 50);

// "this" keyword in java refers to current object.


// Adding "jRadioButton1" on JFrame.
this.add(jRadioButton1);

// Adding "jRadioButton2" on JFrame.


this.add(jRadioButton2);

// Adding JLabel "L2" on JFrame.


this.add(L1);

public static void main(String args[])


{

text6 f = new text6(); // Creating object of text6 class.

// Setting Bounds of JFrame.


f.setBounds(100, 100, 400, 200);

// Setting Title of frame.


f.setTitle("RadioButtons");

// Setting Visible status of frame as true.


f.setVisible(true);

}
}
7.Creation of Combo Box and displaying the item selected in it.

// Java Program to create a simple JComboBox and add elements to it


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class text8 extends JFrame implements ItemListener {

// frame
static JFrame f;

// label
static JLabel l, l1;

// combobox
static JComboBox c1;

// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");

// create a object
text8 s = new text8();

// set layout of frame


f.setLayout(new FlowLayout());

// array of string containing cities


String s1[] = { "Vijayawada", "Mumbai", "Kakinada", "Vizag",
"New Delhi" };

// create combobox
c1 = new JComboBox(s1);

// add ItemListener
c1.addItemListener(s);

// create labels
l = new JLabel("select your city ");
l1 = new JLabel("Vijayawada selected");
// set color of text
l.setForeground(Color.red);
l1.setForeground(Color.blue);

// create a new panel


JPanel p = new JPanel();

p.add(l);

// add combobox to panel


p.add(c1);

p.add(l1);

// add panel to frame


f.add(p);

// set the size of frame


f.setSize(400, 300);

f.show();
}
public void itemStateChanged(ItemEvent e)
{
// if the state combobox is changed
if (e.getSource() == c1) {

l1.setText(c1.getSelectedItem() + " selected");


}
}
}

8.Creation of List :
// java Program to create a simple JList
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text9 extends JFrame
{

//frame
static JFrame f;
//lists
static JList b;

//main class
public static void main(String[] args)
{
//create a new frame
f = new JFrame("Example of List");

//create a object
text9 s =new text9();

//create a panel
JPanel p =new JPanel();

//create a new label


JLabel l= new JLabel("select the day of the week");

//String array to store weekdays


String
week[]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","
Sunday"};

//create list
b= new JList(week);

//set a selected index


b.setSelectedIndex(0);

//add list to panel


p.add(b);

f.add(p);

//set the size of frame


f.setSize(400,400);
f.show();
}

}
9. Creation of Scroll bar:

import javax.swing.*;
class text10
{
text10(){
JFrame f= new JFrame("Scrollbar Example");
JScrollBar s=new JScrollBar();
s.setBounds(100,100, 50,100);
f.add(s);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new text10();
}}

10. Creation of Slider:

// java Program to create a simple JSlider


import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
class text11 extends JFrame {

// frame
static JFrame f;

// slider
static JSlider b;

// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("Example of slider");

// create a object
text11 s = new text11();
// create a panel
JPanel p = new JPanel();

// create a slider
b = new JSlider();

// add slider to panel


p.add(b);

f.add(p);

// set the size of frame


f.setSize(300, 300);

f.show();
}
}

You might also like