一个JavaFX的简单Demo

  • 1.登录界面
  • package Login;
    
    import Music.MusicPlayerController;
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.PasswordField;
    import javafx.scene.control.TextField;
    import javafx.scene.effect.Reflection;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.paint.Color;
    import javafx.scene.text.Font;
    import javafx.scene.text.FontWeight;
    import javafx.scene.text.Text;
    import javafx.scene.text.TextAlignment;
    import javafx.stage.Stage;
    
    import java.time.LocalDateTime;
    
    public class LoginController extends Application {
    
        public static final String USERNAME = "admin";
    
        public static final String PASSWORD = "123456";
    
        private TextField userTextField;
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage stage) {
            try {
                //设置窗体的标题
                stage.setTitle("登录");
                //网格布局方式,创建一个GridPane面板
                GridPane grid = new GridPane();
                //改变grid的默认位置,默认情况下,grid的位置是在其父容器的左上方,此处父容器是Scene,现在将grid移至Scene的中间
                grid.setAlignment(Pos.CENTER);
                //是用来设置该网格每行和每列之间的水平间距和垂直间距的
                grid.setHgap(10);
                grid.setVgap(10);
                //设置了环绕在该网格面板上的填充距离,这里网格默认被设为在场景容器中居中,这里的填充距离是表示网格边缘距离里面内容的距离。
                // 设置内边距,传入的是一个Insets对象,该insets对象的参数是:上,左,下,右
                grid.setPadding(new Insets(25, 25, 25, 25));
    
                //<span style="font-size:14px">
                Text sceneTitle = new Text("    想听就听");
                sceneTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
                sceneTitle.setFill(Color.ROYALBLUE);
    
                //设置倒影
                Reflection r = new Reflection();
                r.setFraction(0.7);
                sceneTitle.setEffect(r);
    
                //grid.add()方法,该方法,有五个参数,该参数不是每个都必填的,分别是要放入的组件,以及第几列,第几行,跨几列和跨几行。
                // 因此我们将Text组件放在第一行,第一列并且跨两列和跨一行;将‘UserName’的Label组件放在第二行第一列,TextField放在第二行第二列,
                // 将‘Password’的Lable组件放在第三行第一列,PasswordField放在第三行第二列。在grid中行和列是从0开始算起的。
                grid.add(sceneTitle, 1, 0, 2, 1);
    
                //用户名
                Label userName = new Label("用户名:");
                grid.add(userName, 0, 3);
    
                //用户名输入文本框
                userTextField = new TextField();
                grid.add(userTextField, 1, 3);
    
                //密码
                Label password = new Label("密   码:");
                grid.add(password, 0, 4);
    
                //密码输入文本框
                PasswordField passwordField = new PasswordField();
                grid.add(passwordField, 1, 4);
                //</span>
    
                //登录 按钮
                Button loginButton = new Button("登录");
                HBox hBox = new HBox(10);
                hBox.setAlignment(Pos.BOTTOM_RIGHT);
                hBox.getChildren().add(loginButton);
                grid.add(hBox, 1, 6);
    
                //添加一个文本框,用于显示信息的控制
                Text actionTarget = new Text();
                actionTarget.setId("actionTarget");
                grid.add(actionTarget, 1, 8);
    
                //声明点击事件,点击显示文本信息
                loginButton.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        if (USERNAME.equals(userTextField.getText()) && PASSWORD.equals(passwordField.getText())) {
                            actionTarget.setFill(Color.GREENYELLOW);
                            actionTarget.setText("        欢迎登录...");
                            MusicPlayerController musicPlayerController = new MusicPlayerController();
                            stage.close();
                            Stage newStage = new Stage();
                            try {
                                musicPlayerController.start(newStage);
                            } catch (Exception e) {
    
                            }
                        }
                    }
                });
    
                //表示为该舞台创建一个场景,上面的网格就是被安置在这个场景中的,该场景的大小为300和275个像素,oracle官网推荐,
                // 在创建场景的时候,好的做法是给该场景设置大小,如果不设置大小则会默认自动更具场景中的内容调整场景的大小,
                // 此外该场景的大小也直接决定于外围舞台的大小。
                Scene scene = new Scene(grid, 300, 275);
                //场景引入css文件
                scene.getStylesheets().add(LoginController.class.getResource("LoginController.css").toExternalForm());
                //将场景加入舞台中
                stage.setScene(scene);
                //让窗体显示
                stage.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static String getUSERNAME() {
            return USERNAME;
        }
    
        public static String getPASSWORD() {
            return PASSWORD;
        }
    
        public TextField getUserTextField() {
            return userTextField;
        }
    
        public void setUserTextField(TextField userTextField) {
            this.userTextField = userTextField;
        }
    }
    
  • 2.音乐播放
  • package Music;
    
    import Login.LoginController;
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.media.Media;
    import javafx.scene.media.MediaPlayer;
    import javafx.scene.paint.Color;
    import javafx.scene.text.Font;
    import javafx.scene.text.FontWeight;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    
    import java.io.File;
    
    /**
     * 音乐播放主页面
     */
    public class MusicPlayerController extends Application {
    
        //private Button playButton;
    
        private MediaPlayer mp1;
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage stage) throws Exception {
            try {
                stage.setTitle("播放");
                GridPane gridPane = new GridPane();
                gridPane.setAlignment(Pos.CENTER);
                gridPane.setVgap(10);
                gridPane.setVgap(10);
                gridPane.setPadding(new Insets(25, 25, 25, 25));
    
                //显示播放歌曲的窗口
                Text musicTitle = new Text();
                musicTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
                musicTitle.setFill(Color.BLUE);
                gridPane.add(musicTitle, 1, 0, 2, 1);
    
                Button playButton = new Button("播放");
                //playButton.setPadding(new Insets(5, 5, 5, 5));
                Button exitButton = new Button("注销");
                playButton.setPrefSize(100,20);
                exitButton.setPrefSize(100,20);
                //exitButton.setPadding(new Insets(5, 5, 5, 5));
                HBox hBox = new HBox(10);
                hBox.setPadding(new Insets(15, 12, 15, 12));
                hBox.setAlignment(Pos.CENTER);
                //hBox.getChildren().add(playButton);
                hBox.getChildren().addAll(playButton, exitButton);
                gridPane.add(playButton, 1, 6);
                gridPane.add(exitButton, 12, 6);
    
                playButton.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        String musicName = "外婆的澎湖湾.mp3";
                        String s1 = MusicPlayerController.class.getResource(musicName).toString();
                        Media media1 = new Media(s1);
                        mp1 = new MediaPlayer(media1);
                        mp1.play();
                        String[] arr = musicName.split("\\.");
                        System.out.println("arr:" + arr.length);
                        musicTitle.setText(arr[0]);
                    }
                });
    
                exitButton.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        mp1.stop();
                        stage.close();
                        LoginController loginController = new LoginController();
                        Stage newStage = new Stage();
                        loginController.start(newStage);
                    }
                });
    
    
                /*Button exitButton = new Button("注销");
                HBox exitButtonHBox = new HBox(10);
                exitButtonHBox.setAlignment(Pos.BOTTOM_RIGHT);
                exitButtonHBox.getChildren().add(exitButton);
                gridPane.add(exitButton, 6, 6);*/
    
                Scene scene = new Scene(gridPane, 300, 275);
                scene.getStylesheets().add(MusicPlayerController.class.getResource("MusicPlayerController.css").toExternalForm());
                stage.setScene(scene);
                stage.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值