Home Internet Java GUI Source Code Examples Know How To Create GUI With Examples

Java GUI Source Code Examples Know How To Create GUI With Examples

0
java gui

Swing in java & java gui becomes part of Java foundation class which is lightweight and platform independent. It is utilized for creating window based applications. It includes elements such as button, scroll bar and text field and so on.

Java GUI Source Code Examples Know How To Create GUI With Examples

In this article, you can know about java gui here are the details below;

Putting together all these elements makes a graphical user interface. In this content, we will go into the ideas associated with the procedure of building applications utilizing swing in Java. Following are the concepts talked about in this article:

What is Swing In Java?

Swing in Java is a light-weight GUI toolkit which has a wide range of widgets for constructing enhanced window based app. It is a member of the JFC( Java Structure Classes). It is build on best of the AWT API and completely written in java. It is platform autonomous unlike AWT and has light-weight components.
It ends up being much easier to construct applications given that we currently have GUI components like button, checkbox and so on. This is valuable since we do not need to begin with the scratch. Also check another post like discord javascript error.

Container Class

Any class which has other parts in it is called as a container class. For constructing GUI applications a minimum of one container class is necessary.
Following are the 3 kinds of container classes:

1. Panel– It is utilized to organize parts on to a window
2. Frame– A totally functioning window with icons and titles
3. Dialog– It resembles a turn up window however not fully practical like the frame

Difference In Between AWT and Swing

AWTSWING
– Platform Dependent – Platform Independent
– Does not follow MVC – Follows MVC
– Lesser Components – More effective parts
– Does not support pluggable look – Supports pluggable feel and look
– Heavyweight – Lightweight.

Java Swing Class Hierarchy.

Explanation: All the elements in swing like JButton, JComboBox, JList, JLabel are inherited from the JComponent class which can be contributed to the container classes. Boxes are the windows like frame & dialog boxes. Fundamental swing components are the foundation of any gui application. Methods like setLayout bypass the default design in each container. Containers such as JFrame and JDialog can just add a component to itself. Following are a couple of parts with examples to comprehend how we can use them. Also check scala vs java.

JButton Class.

It is used to build a labelled button. Utilizing the ActionListener it will lead to some action when the switch is pushed. It inherits the AbstractButton class & is platform independent.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
import javax.swing.*;
public class example{
public static void main(String args[]) {
JFrame a = new JFrame("example");
JButton b = new JButton("click me");
b.setBounds(40,90,85,20);
a.add(b);
a.setSize(300,300);
a.setLayout(null);
a.setVisible(true);
}
}

Output:.

JTextField Class.

It inherits the JTextComponent room & it is utilized to enable editing of single line text.
Example:.

1
2
3
4
5
6
7
8
9
10
11
12
import javax.swing.*;
public class example{
public static void main(String args[]) {
JFrame a = new JFrame("example");
JTextField b = new JTextField("techolac");
b.setBounds(50,100,200,30);
a.add(b);
a.setSize(300,300);
a.setLayout(null);
a.setVisible(true);
}
}

Output:.

JScrollBar Class.

It is used to include scroll bar, both horizontal and vertical.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import javax.swing.*;
class example{
example(){
JFrame a = new JFrame("example");
JScrollBar b = new JScrollBar();
b.setBounds(90,90,40,90);
a.add(b);
a.setSize(300,300);
a.setLayout(null);
a.setVisible(true);
}
public static void main(String args[]){
new example();
}
}

Output:.

JPanel Class.

It acquires the JComponent class and supplies space for an application which can connect any other part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.awt.*;
import javax.swing.*;
public class Example{
Example(){
JFrame a = new JFrame("example");
JPanel p = new JPanel();
p.setBounds(40,70,200,200);
JButton b = new JButton("click me");
b.setBounds(60,50,80,40);
p.add(b);
a.add(p);
a.setSize(400,400);
a.setLayout(null);
a.setVisible(true);
}
public static void main(String args[])
{
new Example();
}
}

Output:.

JMenu Class.

It acquires the JMenuItem class, and is a take down menu part which is shown from the menu bar.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import javax.swing.*;
class Example{
JMenu menu;
JMenuItem a1,a2;
Example()
{
JFrame a = new JFrame("Example");
menu = new JMenu("options");
JMenuBar m1 = new JMenuBar();
a1 = new JMenuItem("example");
a2 = new JMenuItem("example1");
menu.add(a1);
menu.add(a2);
m1.add(menu);
a.setJMenuBar(m1);
a.setSize(400,400);
a.setLayout(null);
a.setVisible(true);
}
public static void main(String args[])
{
new Example();
}
}

Output:.

JList Class.

It acquires JComponent class, the item of JList class represents a list of text products.
Programs & Frameworks Training.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import javax.swing.*;
public class Example
{
Example(){
JFrame a  = new JFrame("example");
DefaultListModel<String> l = new DefaultListModel< >();
l.addElement("first item");
l.addElement("second item");
JList<String> b = new JList< >(l);
b.setBounds(100,100,75,75);
a.add(b);
a.setSize(400,400);
a.setVisible(true);
a.setLayout(null);
}
public static void main(String args[])
{
new Example();
}
}

Output:.

JLabel Class.

It is used for putting text in a container. It likewise acquires JComponent class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import javax.swing.*;
public class Example{
public static void main(String args[])
{
JFrame a = new JFrame("example");
JLabel b1;
b1 = new JLabel("techolac");
b1.setBounds(40,40,90,20);
a.add(b1);
a.setSize(400,400);
a.setLayout(null);
a.setVisible(true);
}
}

Output:.

JComboBox Class.

It acquires the JComponent class and is used to reveal appear menu of options.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import javax.swing.*;
public class Example{
JFrame a;
Example(){
a = new JFrame("example");
string courses[] = { "core java","advance java", "java servlet"};
JComboBox c = new JComboBox(courses);
c.setBounds(40,40,90,20);
a.add(c);
a.setSize(400,400);
a.setLayout(null);
a.setVisible(true);
}
public static void main(String args[])
{
new Example();
}

Output:.

Layout Supervisor.

To arrange the parts inside a container we use the design supervisor. Following are numerous layout managers:.
1. Border design.
2. Flow layout.
3. GridBag design.

Border Design.

The default layout supervisor for every JFrame is BorderLayout. It places components in upto five locations which is top, bottom, left, ideal and center.

Flow Layout.

FlowLayout simply lays the elements in a row one after the other, it is the default layout supervisor for every JPanel.

GridBag Layout.

GridBagLayout positions the components in a grid which permits the components to span more than one cell.

Example: Chat Frame

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import javax.swing.*;
import java.awt.*;
class Example {
    public static void main(String args[]) {
      
        JFrame frame = new JFrame("Chat Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        JMenuBar ob = new JMenuBar();
        JMenu ob1 = new JMenu("FILE");
        JMenu ob2 = new JMenu("Help");
        ob.add(ob1);
        ob.add(ob2);
        JMenuItem m11 = new JMenuItem("Open");
        JMenuItem m22 = new JMenuItem("Save as");
        ob1.add(m11);
        ob1.add(m22);
        
        JPanel panel = new JPanel(); // the panel is not visible in output
        JLabel label = new JLabel("Enter Text");
        JTextField tf = new JTextField(10); // accepts upto 10 characters
        JButton send = new JButton("Send");
        JButton reset = new JButton("Reset");
        panel.add(label); // Components Added using Flow Layout
        panel.add(label); // Components Added using Flow Layout
        panel.add(tf);
        panel.add(send);
        panel.add(reset);
        JTextArea ta = new JTextArea();
        frame.getContentPane().add(BorderLayout.SOUTH, panel);
        frame.getContentPane().add(BorderLayout.NORTH, tf);
        frame.getContentPane().add(BorderLayout.CENTER, ta);
        frame.setVisible(true);
    }
}

 

This is an easy example for creating a GUI utilizing swing in Java. In this article we have actually discussed swing in Java and hierarchy of Java swing classes. With all the elements which features swing in Java, it ends up being much easier to develop enhanced GUI applications. Java programming dialect is a structured programming language and with the growing need it ends up being exceptionally essential to master all the principles in Java shows. Also check best chrome extensions.

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version