Use Java core framework to achieve graphic user interface development
Use Java core framework to achieve graphic user interface development
With the popularization of software applications, the importance of graphics user interface (GUI) is becoming more and more prominent.Java is a powerful programming language. It has a rich graphical user interface development library that can realize a variety of user interface design.In Java, there are several main core frameworks for graphic user interface development, including Swing, Javafx, and AWT.
Swing is part of the Java platform, which provides a set of customized graphical user interface components, such as buttons, text boxes, labels, and tables.Swing allows developers to create custom interface elements by inheriting and expanding existing components.The following is an example of code using Swing to create a simple window:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
public static void main(String[] args) {
// Create a top window
JFrame frame = new JFrame("Hello World");
// Set the window size
frame.setSize(300, 200);
// Add a tag component
JLabel label = new JLabel("Hello, Java Swing!");
frame.getContentPane().add(label);
// Set the window to close the operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Display window
frame.setVisible(true);
}
}
Javafx is another graphical user interface framework of the Java platform, which provides a set of modern, customable interface controls.Javafx uses Java programming language and has a strong layout and style.Here are a code example using Javafx to create a simple window:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Create a root layout
StackPane root = new StackPane();
// Create a tag component
Label label = new Label("Hello, JavaFX!");
// Add the label to the root layout
root.getChildren().add(label);
// Create a scene and add the root layout to the scene
Scene scene = new Scene(root, 300, 200);
// Set the stage title
primaryStage.setTitle("Hello World");
// Set the scene to the stage of the stage
primaryStage.setScene(scene);
// Display stage
primaryStage.show();
}
}
AWT (abstract window tool set) is the earliest graphic user interface framework of Java, which provides a set of cross -platform native interface components.AWT is part of the Java platform and interacts with the graphic user interface of the operating system.The following is an example of code using AWT to create a simple window:
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Main {
public static void main(String[] args) {
// Create a window
Frame frame = new Frame("Hello World");
// Create a tag component
Label label = new Label("Hello, AWT!");
// Add the label to the window
frame.add(label);
// Add the window to close the event processing procedure
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Set the window size
frame.setSize(300, 200);
// Display window
frame.setVisible(true);
}
}
The above is some examples of using the Java core framework to implement the graphic user interface.Developers can choose suitable frameworks and components according to their needs to create rich and interactive user interfaces.Whether Swing, Javafx or AWT, Java provides rich tools and library to simplify the development process of graphic user interface.