javafx基础代码

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class Main extends Application {
    
    public void start(Stage stg) {
        Pane pane = new Pane();
        Scene scene = new Scene(pane, 680, 450);
        
        // 图片
        Image image = new Image("./data/bg.jpg");
        pane.getChildren().add(new ImageView(image));
        
        // 标签
        Label label = new Label("显示标签文字");
        label.setFont(Font.font("System", FontWeight.BOLD, 25));
        label.setTextFill(Color.ORANGE);
        label.setLayoutX(50);
        label.setLayoutY(30);
        
        // 按钮
        Button button = new Button("进入系统");
        button.setStyle("-fx-font-size: 20;");
        button.setPadding(new Insets(10, 25, 10, 25));
        button.setLayoutX(100);
        button.setLayoutY(100);
        button.setOnAction(e -> {
            // 打开另一个新的窗口
            // 新的窗口代码类似,但只有start方法,没有main方法
//            LoginWindow loginWin = new LoginWindow();
//            loginWin.start(new Stage());
            stg.close();
        });
        
        // 输入方框
        TextField textField = new TextField();
        textField.setLayoutX(100);
        textField.setLayoutY(200);
//        String text = textField.getText();
        
        // 密码方框
        PasswordField passwordField = new PasswordField();
        passwordField.setLayoutX(100);
        passwordField.setLayoutY(250);
//        String password = passwordField.getText();
        
        // 下拉菜单
        String[] userType = {"个人账号", "活动发起人", "系统管理员"};
        ComboBox<String> cbox = new ComboBox<>();
        cbox.setValue("个人账号");
        ObservableList<String> items = FXCollections.observableArrayList(userType);
        cbox.getItems().addAll(items);
        cbox.setPadding(new Insets(-1, 5, -1, 5));
        cbox.setStyle("-fx-font-size: 16");
        cbox.setLayoutX(320);
        cbox.setLayoutY(258);
//        int type = items.indexOf(cbox.getValue());
        
        pane.getChildren().addAll(label, button, textField, passwordField, cbox);
        
        // 设置快捷键
        scene.setOnKeyPressed(e -> {
            if(e.getCode() == KeyCode.ENTER) {
                button.fire();
            }
        });
        
        stg.setResizable(false);        // 设置窗口不可伸缩
        stg.getIcons().add(new Image("./data/icon.jpg"));  // 设置窗口图标
        stg.setTitle("窗口标题");
        stg.setScene(scene);
        stg.show();
    }
    
    public static void main(String[] args) {
        Application.launch(args);
    }
}

 

posted @ 2022-04-25 21:02  HanselHuang  阅读(191)  评论(0编辑  收藏  举报