Java Swing
wahidurrahman (50) in utopian-io • 6 days ago
What Will We Learn from this Tutorial ?
In this Tutorial, We are learning basic knowledge of Java Swing .
Java Swing : Java Swing is the part of a Java foundation classes that should be used in window based application. It Provide's platform Independent and It has Light -weight components.
We Will Learn from this tutorial about the basic of Swing
Also Learn Java Swing API like as JButton, JTextfield, JTextArea,JRadioButton,JCheckBox e.t.c
Requirements of Java Swings
There are some requirements in java Swing. That's are given below :
Basic Concept of Object Oriented Programming ( OOP)
Knowledge about AWT ( Abstract Window Toolkit) packages.
Difficulty
There is no difficulty in this tutorial. It is Primary level and easy to learn.
Basic
Tutorial Contents Of Java Swings
We already know that Java Swing Component is a platform Independent. It Support's Pluggable looks and powerful components.That's basically follows MVC (Model View Controller) pattern. In this tutorial , we mainly focused the Java Swing Basic knowledge.
Let's discuss about the Swing Components to the given below :
Java JButton: The JButton class is used to create a labeled button that has platform independent implementation. It Inherits from Abstract Button class.
Here , we can see a example of JButton :
import javax.swing.*;
public class button {
public static void main(String[] args) {
JFrame f=new JFrame("Button");
JButton b=new JButton("Click this");
b.setBounds(50,100,100,40);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Output: After running this program , we can get the following output -
1.PNG
1a.PNG
Java JLabel : In Java Jlabel class is a component for placing a text in the container.It Inherits form JComponent Class.
Let's see an example of JLabel :
import javax.swing.*;
class label{
public static void main(String args[])
{
JFrame f= new JFrame("Label ");
JLabel l1,l2;
l1=new JLabel("Hello Beginners!!!");
l1.setBounds(50,50, 100,30);
l2=new JLabel("Welcome to Our new tutorial.");
l2.setBounds(50,100, 300,30);
f.add(l1); f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
Output : After the running program , we can get the following result -
2.PNG
2a.PNG
Java JTextField: In Java JTextField class is a component that allows editing the single line of elements.
Here, We can see an example of JTextField-
import javax.swing.*;
class textfield{
public static void main(String args[])
{
JFrame f= new JFrame("TextField");
JTextField t1,t2;
t1=new JTextField("Welcome to Our New Tutorial.");
t1.setBounds(50,100, 300,30);
t2=new JTextField("Java Swing Tutorial");
t2.setBounds(50,150, 300,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Output : After Running this program, we can see the following Output -
3.PNG
3a.PNG
Java JPasswordField: In JPasswordField Class is a Component for password . It allows the editing of a single line of text. It Inherits from JTextField Class.
Here, we can see an example of JPasswordField -
import javax.swing.*;
public class password {
public static void main(String[] args) {
JFrame f=new JFrame("Password Field ");
JPasswordField value = new JPasswordField();
JLabel l1=new JLabel("Password:");
l1.setBounds(20,100, 80,30);
value.setBounds(100,100,100,30);
f.add(value); f.add(l1);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
Output : After running the program , we can get the following output -
4.PNG
4a.PNG
Java JCheckBox : In Java JCheckBox Class is used to create a JCheckBox. It Inherit's from JToggleButton class.
Here, We can see an example of JCheckBox -
import javax.swing.*;
public class checkbox{
checkbox(){
JFrame f= new JFrame("CheckBox");
JCheckBox checkBox1 = new JCheckBox("A",true);
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("B");
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 checkbox();
}
}
OutPut : After running this program , we can see the following result-
5.PNG
5a.PNG
Java JRadioButton : In Java JRadioButton Class is used to create a radio button. It is used to choose one option from multiple options.
Let's see an example of JRadioButton-
import javax.swing.*;
public class radiobutton {
JFrame f;
radiobutton(){
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
r1.setBounds(50,50,100,30);
r2.setBounds(50,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new radiobutton();
}
}
Output: After running the program, we can get the following results-
6.PNG
6a.PNG
Posted on Utopian.io - Rewarding Open Source Contributors
Congratulations @shahriyar! You have received a personal award!
1 Year on Steemit
Click on the badge to view your Board of Honor.
Congratulations @shahriyar! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!