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); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律