0% found this document useful (0 votes)
101 views9 pages

Java Swing GUI Components Examples

The document discusses different Swing components in Java including JFrame, JButton, JLabel, JTextField, JTextArea, JPasswordField, JCheckBox, JRadioButton, and JComboBox. Code examples are provided for creating and using each component.

Uploaded by

Regie Sudoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views9 pages

Java Swing GUI Components Examples

The document discusses different Swing components in Java including JFrame, JButton, JLabel, JTextField, JTextArea, JPasswordField, JCheckBox, JRadioButton, and JComboBox. Code examples are provided for creating and using each component.

Uploaded by

Regie Sudoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JFrame

import [Link].*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//creating instance of JButton


[Link](130,100,100, 40);//x axis, y axis, width, height

[Link](b);//adding button in JFrame

[Link](400,500);//400 width and 500 height


[Link](null);//using no layout managers
[Link](true);//making the frame visible
}
}

Output
JButton
import [Link].*;
public class ButtonExample{
ButtonExample(){
JFrame f=new JFrame("Button Example");
JButton b=new JButton(new ImageIcon("D:\\[Link]"));
[Link](100,100,100, 40);
[Link](b);
[Link](300,400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ButtonExample();
}
}
Output
JLabel
import [Link].*;
class LabelExample
{
public static void main(String args[])
{
JFrame f= new JFrame("Label Example");
JLabel l1,l2;
l1=new JLabel("First Label.");
[Link](50,50, 100,30);
l2=new JLabel("Second Label.");
[Link](50,100, 100,30);
[Link](l1); [Link](l2);
[Link](300,300);
[Link](null);
[Link](true);
}
}

Output
JTextField

import [Link].*;
class TextFieldExample
{
public static void main(String args[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField("Welcome to Computer Programming 2.");
[Link](50,100, 200,30);
t2=new JTextField("SWING Example");
[Link](50,150, 200,30);
[Link](t1); [Link](t2);
[Link](400,400);
[Link](null);
[Link](true);
}
}
Output

Welcome to Computer Programming 2

SWING Example
JTextArea
import [Link].*;
public class TextAreaExample
{
TextAreaExample(){
JFrame f= new JFrame();
JTextArea area=new JTextArea("Welcome to Computer Programming 2");
[Link](10,30, 200,200);
[Link](area);
[Link](300,300);
[Link](null);
[Link](true);
}
public static void main(String args[])
{
new TextAreaExample();
}}

Output

Welcome to Computer Programming 2


JPasswordField
import [Link].*;
public class PasswordFieldExample {
public static void main(String[] args) {
JFrame f=new JFrame("Password Field Example");
JPasswordField value = new JPasswordField();
JLabel l1=new JLabel("Password:");
[Link](20,100, 80,30);
[Link](100,100,100,30);
[Link](value); [Link](l1);
[Link](300,300);
[Link](null);
[Link](true);
}
}
Output
JCheckBox
import [Link].*;
public class CheckBoxExample
{
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
[Link](100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
[Link](100,150, 50,50);
[Link](checkBox1);
[Link](checkBox2);
[Link](400,400);
[Link](null);
[Link](true);
}
public static void main(String args[])
{
new CheckBoxExample();
}}
Output
JRadioButton
import [Link].*;
public class RadioButtonExample {
JFrame f;
RadioButtonExample(){
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
[Link](75,50,100,30);
[Link](75,100,100,30);
ButtonGroup bg=new ButtonGroup();
[Link](r1);[Link](r2);
[Link](r1);[Link](r2);
[Link](300,300);
[Link](null);
[Link](true);
}
public static void main(String[] args) {
new RadioButtonExample();
}
}
Output

JComboBox

import [Link].*;
public class ComboBoxExample {
JFrame f;
ComboBoxExample(){
f=new JFrame("ComboBox Example");
String country[]={"India","Aus","U.S.A","England","Newzealand"};
JComboBox cb=new JComboBox(country);
[Link](50, 50,90,20);
[Link](cb);
[Link](null);
[Link](400,500);
[Link](true);
}
public static void main(String[] args) {
new ComboBoxExample();
}
}
Output

You might also like