0% found this document useful (0 votes)
2 views4 pages

Build Every Java Program in Gui

The document compares four methods of user input and output in Java: Console, JOptionPane, Swing, and JavaFX. Each method is illustrated with complete code examples that demonstrate how to ask for a user's name and display a welcome message. The document also includes a side-by-side feature comparison and emphasizes that the underlying logic remains consistent across all methods, with only the user interface differing.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Build Every Java Program in Gui

The document compares four methods of user input and output in Java: Console, JOptionPane, Swing, and JavaFX. Each method is illustrated with complete code examples that demonstrate how to ask for a user's name and display a welcome message. The document also includes a side-by-side feature comparison and emphasizes that the underlying logic remains consistent across all methods, with only the user interface differing.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Simple Input/Output Comparison

Suppose we want to ask the user for their name and display a welcome message.

1. Console
Input
Scanner input = new Scanner([Link]);
String name = [Link]();

Output
[Link]("Welcome " + name);

Complete Program
import [Link];

public class Main {


public static void main(String[] args) {

Scanner input = new Scanner([Link]);

[Link]("Enter Name: ");


String name = [Link]();

[Link]("Welcome " + name);


}
}

2. JOptionPane
Input
String name =
[Link]("Enter Name");

Output
[Link](
null,
"Welcome " + name);

Complete Program
import [Link];

public class Main {

public static void main(String[] args) {

String name =
[Link]("Enter Name");

[Link](
null,
"Welcome " + name);
}
}

3. Swing
Input Component
JTextField txtName = new JTextField();

Output Component
JLabel lblResult = new JLabel();

Complete Program
import [Link].*;

public class Main {

public static void main(String[] args) {

JFrame frame = new JFrame("Swing Example");

JTextField txtName = new JTextField();

JButton btn = new JButton("Show");

JLabel lblResult = new JLabel();

[Link](50, 30, 150, 30);


[Link](50, 70, 150, 30);
[Link](50, 110, 200, 30);

[Link](e -> {
String name = [Link]();
[Link]("Welcome " + name);
});

[Link](txtName);
[Link](btn);
[Link](lblResult);

[Link](300, 250);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}

4. JavaFX
Input Component
TextField txtName = new TextField();

Output Component
Label lblResult = new Label();

Complete Program
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];

public class Main extends Application {

@Override
public void start(Stage stage) {

TextField txtName = new TextField();

Button btn = new Button("Show");

Label lblResult = new Label();

[Link](e -> {
String name = [Link]();
[Link]("Welcome " + name);
});

VBox root = new VBox(10);

[Link]().addAll(
txtName,
btn,
lblResult
);

Scene scene =
new Scene(root, 300, 200);

[Link](scene);
[Link]("JavaFX Example");
[Link]();
}

public static void main(String[] args) {


launch();
}
}

Side-by-Side Comparison
Feature Console JOptionPane Swing JavaFX
Input Scanner showInputDialog() JTextField TextField
Output println() showMessageDialog() JLabel Label
Button ❌ ❌ JButton Button
Window Terminal Popup JFrame Stage
GUI ❌ Basic Yes Modern
Difficulty Easy Easy Medium Medium

Same Logic in All Four


Get Name

Store in Variable

Display Welcome Message

Only the input/output mechanism changes:

Console
Scanner → println

JOptionPane
showInputDialog → showMessageDialog

Swing
JTextField → JLabel

JavaFX
TextField → Label

This is the natural learning progression:

Console

JOptionPane

Swing

JavaFX

The business logic (variables, loops, conditions, methods, OOP) stays almost identical; only
the user interface changes.

You might also like