Application in Java
Creating a complete Java application involves writing Java code that accomplishes a specific task or provides a service. Java applications can vary widely in complexity, from simple command-line programs to sophisticated graphical user interfaces (GUIs) and web applications. Below, I’ll outline how to create a simple console-based Java application and a basic Java Swing GUI application as examples.
1. Simple Console-Based Java Application:
Here’s an example of a basic console-based Java application that calculates the sum of two numbers entered by the user:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
double sum = num1 + num2;
System.out.println("The sum is: " + sum);
scanner.close();
}
}
In this example:
- We import the
java.util.Scanner
class to read input from the user. - We create a
Scanner
object to read user input from the console. - We prompt the user for two numbers, read them, calculate their sum, and then display the result.
To run this program, save it in a file with the name Calculator.java
, compile it using the javac
command, and execute it using the java
command.
2. Basic Java Swing GUI Application:
Creating a simple graphical user interface (GUI) application in Java using Swing involves more code and complexity. Below is a minimal example of a Java Swing GUI application that displays a “Hello, Swing!” message in a window:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class HelloWorldSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello, Swing!");
JLabel label = new JLabel("Hello, Swing!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
}
In this example:
- We import classes from the
javax.swing
package to create the GUI components. - We create a
JFrame
to represent the application window. - We create a
JLabel
to display the “Hello, Swing!” message. - We set the default close operation to exit the application when the window is closed.
- We add the
JLabel
to the content pane of theJFrame
, and then we pack and display the window.
To run this program, save it in a file with the name HelloWorldSwing.java
, compile it using the javac
command, and execute it using the java
command.
Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Java Training here – Java Blogs
You can check out our Best in Class Java Training details here – Java Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks