【Java系列】Java GUI - FX/Swing

发布于:2025-05-01 ⋅ 阅读:(20) ⋅ 点赞:(0)

Java

Java GUI

  • AWT(Abstract Window Toolkit)
    • 一组原生GUI组件,依赖于操作系统本地窗口系统 !!!
  • Swing

FX

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

FX 入门
package com.company;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Notes extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        //场景
       Scene scene = new Scene(root,800,600);
        // 舞台
        // 舞台属性/行为
        stage.setTitle("舞台标题");
        // 把场景挂到舞台上去
        stage.setScene(scene);
		// 展示
        stage.show();
    }

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

helloWord


import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class HelloWorld extends Application {

    @Override public void start(Stage stage) {
        Text text = new Text(10, 40, "Hello World!");
        text.setFont(new Font(40));
        Scene scene = new Scene(new Group(text));

        stage.setTitle("Welcome to JavaFX!");
        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();
    }

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

Stage

Class Stage
Package javafx.stage
The JavaFX Stage class is the top level JavaFX container. The primary Stage is constructed by the platform. Additional Stage objects may be constructed by the application.
Stage objects must be constructed and modified on the JavaFX Application Thread.
Stage 是fx的最高水平的容器。 The primary Stage是由平台进行搭建的。
其他/另外的Stage对象可能是通过application进行构建的。
一定是在fx应用线程构造或修改Stage对象的。
Style
A stage has one of the following styles:

  • StageStyle.DECORATED - a stage with a solid white background and platform decorations.
  • StageStyle.UNDECORATED - a stage with a solid white background and no decorations.
  • StageStyle.TRANSPARENT - a stage with a transparent background and no decorations.
  • StageStyle.UTILITY - a stage with a solid white background and minimal platform decorations.
    The style must be initialized before the stage is made visible.

Owner

A stage can optionally have an owner Window. When a window is a stage’s owner, it is said to be the parent of that stage.

Tab

Tabs are placed within a TabPane, where each tab represents a single ‘page’.。

‌HBox‌()

HBox‌是JavaFX中的一个布局容器,用于将多个子节点水平排列。HBox布局类将JavaFX子节点放在水平行中,新的子节点附加到右侧的末尾。默认情况下,HBox布局尊重子节点的首选宽度和高度。

水平布局:内容都是自动水平摆放的

  HBox hBox = new HBox();

设置填充和间距‌:

  • setPadding(new Insets(10,10,10,10));:设置填充。
  • setSpacing(10);:设置子节点之间的间距
  • hbox.setAlignment(Pos.CENTER); // 设置HBox中子节点的对齐方式为居中
  • hbox.getChildren().addAll(button1, button2); //向HBox中添加两个按钮

Swing

ImageIcon

在这里插入图片描述

构造方法

在这里插入图片描述

方法简要

在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到