Select Menu

Combined Post

Mag Posts

Theme images by konradlew. Powered by Blogger.

Blogger templates

Blogger news

Facebook

Advertising

Blogroll

Popular Posts

Combined Posts 2

Mag Post 2

2 Column Post

Simple Post

Simple Post 2

New Carousel

Video Posts

» »Unlabelled » Creating a Simple Swing Program in java


SLVIKI 6:50 PM 0

Here is a simple swing program that include only JFrame and JLabel. JFrame is the top level container that commonly used for Swing applications in java. And JLabel is the Swing component that creates a label. Which is a component that display information. To add these Swing components we need to import Swing to the java code.



Here is the java code for the simple swing program.



 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
// A simple Swing program

import javax.swing.*;

class SwingDemo {
 SwingDemo() {
  // Create a new JFrame container.
  JFrame jfrm = new JFrame("A Simple Swing Application");

  // Give the frame an initial size.
  jfrm.setSize(275, 100);

  // Terminate the program when the user closes the application.
  jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // Create a text-based label.
  JLabel jlab = new JLabel(" Swing defines the modern Java GUI. ");

  // Add the label to the content pane.
  jfrm.add(jlab);

  // Display the frame.
  jfrm.setVisible(true);
 }

 public static void main(String[] args) {
  // Create the frame on the event dispatching thread.
  SwingUtilities.invokeLater (new Runnable() {
   public void run() {
    new SwingDemo();
   }
  });
 }
 
}

And here is the output which we get from the above program.






If you have any questions feel free to ask.

Enjoy!

«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply