Professional Documents
Culture Documents
SWING UI ELEMENTS
}
}
// 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();
// create a panel
JPanel p = new JPanel();
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);
}
}
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.*;
}
}
7.Creation of Combo Box and displaying the item selected in it.
// 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();
// 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);
p.add(l);
p.add(l1);
f.show();
}
public void itemStateChanged(ItemEvent e)
{
// if the state combobox is changed
if (e.getSource() == c1) {
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 list
b= new JList(week);
f.add(p);
}
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();
}}
// 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();
f.add(p);
f.show();
}
}