Developing graphical user interface using java // Internet Programming (IT)

Опубликовано: 24 Октябрь 2024
на канале: Global Exploration Knowledge Hub 2.0
10
3

Developing Graphical User Interfaces (GUIs) in Java

Java provides several libraries and frameworks for creating graphical user interfaces (GUIs), the most common being Swing and JavaFX. Both libraries allow developers to build interactive applications with windows, buttons, text fields, and other components.

---

1. Introduction to Java Swing

*Swing* is a part of the Java Foundation Classes (JFC) and provides a rich set of GUI components. It is built on top of AWT (Abstract Window Toolkit) and is platform-independent.

#### Key Features of Swing:
Lightweight components.
Pluggable Look-and-Feel: You can change the appearance of your application.
Built-in support for events.

#### Basic Swing Components
**JFrame**: The main window.
**JPanel**: A container that can hold other components.
**JButton**: A clickable button.
**JLabel**: Displays text or images.
**JTextField**: A single-line text input.
**JTextArea**: A multi-line text input.

#### Example: Simple Swing Application

```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleSwingApp {
public static void main(String[] args) {
// Create the frame
JFrame frame = new JFrame("Simple Swing Application");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create a panel
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);

// Set the frame visibility
frame.setVisible(true);
}

private static void placeComponents(JPanel panel) {
panel.setLayout(null);

JLabel userLabel = new JLabel("User:");
userLabel.setBounds(10, 20, 80, 25);
panel.add(userLabel);

JTextField userText = new JTextField(20);
userText.setBounds(100, 20, 165, 25);
panel.add(userText);

JButton loginButton = new JButton("Login");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);

// Add action listener to the button
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Username: " + userText.getText());
}
});
}
}
```

2. Introduction to JavaFX

*JavaFX* is the modern framework for building rich desktop applications. It provides more advanced features than Swing and is designed to work with modern UI elements.

#### Key Features of JavaFX:
Modern UI controls (buttons, charts, tables).
CSS styling for UI components.
FXML for declarative UI design.
Support for 2D and 3D graphics.

#### Basic JavaFX Components
**Stage**: The main window.
**Scene**: Contains all content in a window.
**Node**: A base class for all JavaFX components (like buttons, labels, etc.).

#### Example: Simple JavaFX Application

```java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SimpleJavaFXApp extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Simple JavaFX Application");

// Create components
Label userLabel = new Label("User:");
TextField userText = new TextField();
Button loginButton = new Button("Login");

// Action event for button
loginButton.setOnAction(e - {
System.out.println("Username: " + userText.getText());
});

// Layout
VBox vbox = new VBox(10, userLabel, userText, loginButton);
Scene scene = new Scene(vbox, 300, 200);
primaryStage.setScene(scene);

// Show the stage
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}
```

3. Event Handling in GUIs

Both Swing and JavaFX utilize an event-driven programming model, allowing applications to respond to user actions.

#### Event Handling in Swing

Use action listeners to handle button clicks, mouse movements, etc.
Implement the `ActionListener` interface and override the `actionPerformed` method.

#### Event Handling in JavaFX

Use lambda expressions or method references to handle events.
Set action handlers directly on UI components (e.g., `setOnAction` for buttons).




Conclusion

Developing graphical user interfaces in Java can be done using Swing or JavaFX, each offering unique advantages. Swing is suitable for traditional desktop applications, while JavaFX provides modern capabilities for rich user experiences. Understanding the components, event handling, and layout management will enable you to create effective and interactive applications in Java.