从Swing转换到JavaFX?

从Swing转换到JavaFX?,java,swing,graphics,javafx,Java,Swing,Graphics,Javafx,将Swing项目转换为JavaFx项目时,JavaFx中的哪些类与Swing类匹配 1-1的对决是最好的 我不是在寻找与Swing的集成,而是一个完整的改头换面。以下是一些基本类: Swing->JavaFx JFrame->Application&Stage(如果您在主类中扩展了JFrame,那么Application.launch()可以用来启动一个扩展应用程序的类)。Stage是显式的JavaFx,是创建窗口所必需的。您将需要使用它的实例,而不是JFrame的实例 JPanel->Sce

将Swing项目转换为JavaFx项目时,JavaFx中的哪些类与Swing类匹配

1-1的对决是最好的


我不是在寻找与Swing的集成,而是一个完整的改头换面。

以下是一些基本类:

  • Swing->JavaFx
  • JFrame->Application&Stage(如果您在主类中扩展了JFrame,那么Application.launch()可以用来启动一个扩展应用程序的类)。Stage是显式的JavaFx,是创建窗口所必需的。您将需要使用它的实例,而不是JFrame的实例
  • JPanel->Scene&Pane(Scene与JPanel匹配,但您的类应该扩展窗格。然后从窗格中构建场景。窗格是JavaFX中的布局)
  • JButton->Button
  • JLabel->Label
  • 在很多情况下,去掉J就可以了
  • JOptionPane->Dialog(但并不总是)
  • ActionListener->EventHandler)(需要在JavaFx中为您需要的每个动作创建一个新的方法/类,这与Swing的“一个方法完成全部任务”不同)
更新现在使用Java 8+时,使用事件处理程序应该更容易,并增加了功能接口的功能,JavaFX现在支持这些功能

示例Swing(我制作的游戏的基本界面)

JavaFx示例(与JavaFx中的示例几乎相同)

公共类接口扩展应用程序{
公共静态void main(字符串[]args){
Application.launch(Interface.class,args);
}
专用静态最终整数宽度=750;
专用静态最终内部高度=500;
私有静态阶段;
私有静态引入开始;
专用静态主显示器;
私人静态场景startScene;
私有静态场景显示场景;
公共接口(){
开始=新的介绍();
显示=新的主显示();
startScene=新场景(开始、宽度、高度);
displayScene=新场景(显示、宽度、高度);
}
@凌驾
public void start(Stage primaryStage)引发异常{
Interface.stage=primaryStage;
stage.setTitle(Constants.TITLE);
阶段。可设置大小(假);
舞台设置宽度(宽度);
舞台设置高度(高度);
第二阶段:新世(新世);
stage.show();
}
公共类介绍扩展了BorderPane{
专用静态最终整数负载=0;
私有静态最终整数保存=1;
私用按钮;
私人按钮保存;
公开介绍(){
设置();
}
私有无效设置(){
这是个挫折(
新背景(新背景填充(Color.BLACK,null,null));
VBox菜单=新建VBox();
菜单设置间隔(10);
标签标题=新标签(Constants.title.toUpperCase());
title.setFont(Font.Font(“Arial”,fontwweight.THIN,60));
标题.setTextFill(颜色.红色);
menu.getChildren().add(title);
int btnWidth=300;
背景菜单KG=新背景(新背景填充(null,null,null));
边框菜单顺序=新边框(新边框笔划(Color.RED,
BorderStrokeStyle.SOLID,新拐角半径(10),空);
按钮开始=新按钮(Constants.start.toUpperCase());
setFont(Font.Font(“Times New Roman”,16));
start.setTextFill(Color.WHITE);
start.setPrefWidth(btnWidth);
开始.倒退(menuBkg);
start.setboorder(菜单顺序);
start.setOnAction(newstarthandler());
toGame=new按钮(Constants.CONTINUE.toUpperCase());
setFont(Font.Font(“Times New Roman”,16));
toGame.setTextFill(颜色:白色);
toGame.setPrefWidth(btnWidth);
toGame.挫折背景(menuBkg);
toGame.setboorder(菜单顺序);
toGame.setDisable(true);
toGame.setOnAction(新的ContinueHandler());
按钮加载=新按钮(Constants.load.toUpperCase());
load.setFont(Font.Font(“Times New Roman”,16));
load.setTextFill(颜色为白色);
load.setPrefWidth(btnWidth);
立根台载荷(menuBkg);
加载顺序(菜单顺序);
setOnAction(新的LoadHandler());
保存=新建按钮(Constants.save.toUpperCase());
save.setFont(Font.Font(“Times New Roman”,16));
save.setTextFill(Color.WHITE);
save.setPrefWidth(btnWidth);
保存.设置背景(menuBkg);
保存订单(菜单订单);
save.setDisable(true);
save.setOnAction(新的SaveHandler());
按钮排名=新按钮(Constants.ranking.toUpperCase());
setFont(Font.Font(“Times New Roman”,16));
排名。setTextFill(颜色。白色);
排名.setPrefWidth(btnWidth);
排名.挫折背景(menuBkg);
排名顺序(菜单顺序);
setOnAction(新RankingHandler());
按钮退出=新按钮(Constants.exit.toUpperCase());
exit.setFont(Font.Font(“Times New Roman”,16));
exit.setTextFill(颜色为白色);
exit.setPrefWidth(btnWidth);
出口.倒退(menuBkg);
退出命令(菜单命令);
setTooltip(新工具提示(常量.exit_工具提示));
setOnAction(新的ExitHandler());
menu.getChildren().addAll(开始、toGame、加载、保存、排序、退出);
菜单设置对齐(位置中心);
这个.setCenter(菜单);
}
专用void performIO(整数选项){
FileChooser chooser=新建FileChooser();
ExtensionFilter ext=新的ExtensionFilter(Constants.ext_DESCRIPTION,“.pt”);
选择器.setSelectedExtensionFilter(ext);
如果(选项==加载){
选择器.setTitle(常量.LOAD);
File choice=chooser.showOpenDialog(阶段);
}else if(选项==保存){
chooser.setTitle(Constants.SAVE);
File choice=chooser.showsavedilog(stage);
}
}
私有布尔验证重启(){
Alert Alert=新警报(AlertType.WARNING、Constants.START\u WARNING\u消息、,
public class Interface extends JFrame {

public static void main(String[] args) {
    new Interface();
}

private CardLayout layout;
private JPanel manager;
private Introduction start;
private MainDisplay display;

public Interface() {
    super("Demo");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setMinimumSize(new Dimension(750, 500));
    this.setPreferredSize(new Dimension(750, 500));
    this.setMaximumSize(new Dimension(750, 500));

    setUp();

    this.pack();
    this.setVisible(true);
}

private void setUp() {
    layout = new CardLayout();
    manager = new JPanel(layout);
    this.add(manager);

    start = new Introduction();
    manager.add(start, INTRODUCTION);

    display = new MainDisplay();
    manager.add(display);

    layout.show(manager);
}

public class Introduction extends JPanel implements ActionListener {

    private static final int LOAD = 0;
    private static final int SAVE = 1;
    private JButton start;
    private JButton toGame;
    private JButton load;
    private JButton save;
    private JButton exit;

    public Introduction() {
        setUp();
    }

    private void setUp() {
        SpringLayout manager = new SpringLayout();
        this.setLayout(manager);

        JLabel title = new JLabel("Demo");
        title.setFont(new Font(Font.SERIF, Font.BOLD, 60));
        manager.putConstraint(SpringLayout.HORIZONTAL_CENTER, title, 0,
                SpringLayout.HORIZONTAL_CENTER, this);
        manager.putConstraint(SpringLayout.NORTH, title, 10, SpringLayout.NORTH,
                this);
        this.add(title);

        start = new JButton("New Game");
        start.setFont(new Font(Font.SERIF, Font.PLAIN, 16));
        start.addActionListener(this);
        manager.putConstraint(SpringLayout.HORIZONTAL_CENTER, start, -250,
                SpringLayout.HORIZONTAL_CENTER, this);
        manager.putConstraint(SpringLayout.NORTH, start, 50, SpringLayout.SOUTH,
                title);
        this.add(start);

        toGame = new JButton("Continue");
        toGame.setFont(new Font(Font.SERIF, Font.PLAIN, 16));
        toGame.addActionListener(this);
        toGame.setEnabled(false);
        manager.putConstraint(SpringLayout.HORIZONTAL_CENTER, toGame, -250,
                SpringLayout.HORIZONTAL_CENTER, this);
        manager.putConstraint(SpringLayout.NORTH, toGame, 10, SpringLayout.SOUTH,
                start);
        this.add(toGame);

        load = new JButton("Load Game");
        load.setFont(new Font(Font.SERIF, Font.PLAIN, 16));
        load.addActionListener(this);
        manager.putConstraint(SpringLayout.HORIZONTAL_CENTER, load, -250,
                SpringLayout.HORIZONTAL_CENTER, this);
        manager.putConstraint(SpringLayout.NORTH, load, 10, SpringLayout.SOUTH,
                toGame);
        this.add(load);

        save = new JButton("Save Game");
        save.setFont(new Font(Font.SERIF, Font.PLAIN, 16));
        save.addActionListener(this);
        save.setEnabled(false);
        manager.putConstraint(SpringLayout.HORIZONTAL_CENTER, save, -250,
                SpringLayout.HORIZONTAL_CENTER, this);
        manager.putConstraint(SpringLayout.NORTH, save, 10, SpringLayout.SOUTH, load);
        this.add(save);

        exit = new JButton("Exit Game");
        exit.setFont(new Font(Font.SERIF, Font.PLAIN, 16));
        exit.setToolTipText("Saves the current game, and exits the program.");
        exit.addActionListener(this);
        manager.putConstraint(SpringLayout.HORIZONTAL_CENTER, exit, -250,
                SpringLayout.HORIZONTAL_CENTER, this);
        manager.putConstraint(SpringLayout.NORTH, exit, 10, SpringLayout.SOUTH, save);
        this.add(exit);
    }

    private void perform(int option) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new TextFileFilter());
        if (option == LOAD) {
            int choice = chooser.showDialog(this, "Load Game");
            if (choice == JFileChooser.APPROVE_OPTION)
                chooser.getSelectedFile();
        } else if (option == SAVE) {
            int choice = chooser.showDialog(this, "Save Game");
            if (choice == JFileChooser.APPROVE_OPTION)
                chooser.getSelectedFile();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (display.isRunning()) {
            if (e.getSource() == start || e.getSource() == load) {
                String verify = "A game is currently running.  Are you sure you want to start a new game?"
                        + "\r\nAny previous game data will be deleted.";
                int choice = JOptionPane.showConfirmDialog(this, verify,
                        "New Game - Warning", JOptionPane.YES_NO_OPTION,
                        JOptionPane.WARNING_MESSAGE);

                if (choice == JOptionPane.NO_OPTION)
                    return;
            }
        }

        if (e.getSource() == start) {
            display.start();
            layout.show(manager, MAIN_DISPLAY);
            toGame.setEnabled(true);
            save.setEnabled(true);
        } else if (e.getSource() == toGame)
            layout.show(manager, MAIN_DISPLAY);
        else if (e.getSource() == load)
            perform(LOAD);
        else if (e.getSource() == save)
            perform(SAVE);
        else if (e.getSource() == exit) {
            perform(SAVE);
            Interface.this.setVisible(false);
            Interface.this.dispose();
        }
    }

}

public class MainDisplay extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;
    private boolean running;
    private JButton overview;
    private JButton map;
    private JButton ranking;
    private JButton reports;
    private JButton home;

    public MainDisplay() {
        running = false;
        setUp();
    }

    public boolean isRunning() {
        return running;
    }

    private void setUp() {
        SpringLayout manager = new SpringLayout();
        this.setLayout(manager);

        ranking = new JButton("Ranking");
        ranking.setFont(new Font(Font.SERIF, Font.PLAIN, 10));
        ranking.addActionListener(this);
        manager.putConstraint(SpringLayout.HORIZONTAL_CENTER, ranking, 0,
                SpringLayout.HORIZONTAL_CENTER, this);
        manager.putConstraint(SpringLayout.NORTH, ranking, 10, SpringLayout.NORTH,
                this);
        this.add(ranking);

        map = new JButton("Map");
        map.setFont(new Font(Font.SERIF, Font.PLAIN, 10));
        map.addActionListener(this);
        manager.putConstraint(SpringLayout.EAST, map, -10, SpringLayout.WEST,
                ranking);
        manager.putConstraint(SpringLayout.NORTH, map, 10, SpringLayout.NORTH, this);
        this.add(map);

        overview = new JButton("Overview");
        overview.setFont(new Font(Font.SERIF, Font.PLAIN, 10));
        overview.addActionListener(this);
        manager.putConstraint(SpringLayout.EAST, overview, -10, SpringLayout.WEST,
                map);
        manager.putConstraint(SpringLayout.NORTH, overview, 10, SpringLayout.NORTH,
                this);
        this.add(overview);

        reports = new JButton("Reports");
        reports.setFont(new Font(Font.SERIF, Font.PLAIN, 10));
        reports.addActionListener(this);
        manager.putConstraint(SpringLayout.WEST, reports, 10, SpringLayout.EAST,
                ranking);
        manager.putConstraint(SpringLayout.NORTH, reports, 10, SpringLayout.NORTH,
                this);
        this.add(reports);

        home = new JButton("Home");
        home.setFont(new Font(Font.SERIF, Font.PLAIN, 10));
        home.addActionListener(this);
        manager.putConstraint(SpringLayout.WEST, home, 10, SpringLayout.EAST,
                reports);
        manager.putConstraint(SpringLayout.NORTH, home, 10, SpringLayout.NORTH, this);
        this.add(home);
    }

    public void start() {
        running = true;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == home) {
            layout.show(manager, INTRODUCTION);
        }
    }

}

public static final class TextFileFilter extends FileFilter {

    private static final String FILE_EXTENSION = "tra";

    @Override
    public boolean accept(File f) {
        if (f.isDirectory())
            return true;
        if (f.getName().toLowerCase().endsWith(FILE_EXTENSION))
            return true;
        return false;
    }

    @Override
    public String getDescription() {
        return "Demo";
    }
}

}
public class Interface extends Application {

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

private static final int WIDTH = 750;
private static final int HEIGHT = 500;
private static Stage stage;
private static Introduction start;
private static MainDisplay display;
private static Scene startScene;
private static Scene displayScene;

public Interface() {
    start = new Introduction();
    display = new MainDisplay();
    startScene = new Scene(start, WIDTH, HEIGHT);
    displayScene = new Scene(display, WIDTH, HEIGHT);
}

@Override
public void start(Stage primaryStage) throws Exception {
    Interface.stage = primaryStage;

    stage.setTitle(Constants.TITLE);
    stage.setResizable(false);
    stage.setWidth(WIDTH);
    stage.setHeight(HEIGHT);

    stage.setScene(startScene);
    stage.show();
}

public class Introduction extends BorderPane {

    private static final int LOAD = 0;
    private static final int SAVE = 1;
    private Button toGame;
    private Button save;
    public Introduction() {
        setUp();
    }

    private void setUp() {

        this.setBackground(
                new Background(new BackgroundFill(Color.BLACK, null, null)));

        VBox menu = new VBox();
        menu.setSpacing(10);

        Label title = new Label(Constants.TITLE.toUpperCase());
        title.setFont(Font.font("Arial", FontWeight.THIN, 60));
        title.setTextFill(Color.RED);
        menu.getChildren().add(title);

        int btnWidth = 300;
        Background menuBkg = new Background(new BackgroundFill(null, null, null));
        Border menuBorder = new Border(new BorderStroke(Color.RED,
                BorderStrokeStyle.SOLID, new CornerRadii(10), null));

        Button start = new Button(Constants.START.toUpperCase());
        start.setFont(Font.font("Times New Roman", 16));
        start.setTextFill(Color.WHITE);
        start.setPrefWidth(btnWidth);
        start.setBackground(menuBkg);
        start.setBorder(menuBorder);
        start.setOnAction(new StartHandler());

        toGame = new Button(Constants.CONTINUE.toUpperCase());
        toGame.setFont(Font.font("Times New Roman", 16));
        toGame.setTextFill(Color.WHITE);
        toGame.setPrefWidth(btnWidth);
        toGame.setBackground(menuBkg);
        toGame.setBorder(menuBorder);
        toGame.setDisable(true);
        toGame.setOnAction(new ContinueHandler());

        Button load = new Button(Constants.LOAD.toUpperCase());
        load.setFont(Font.font("Times New Roman", 16));
        load.setTextFill(Color.WHITE);
        load.setPrefWidth(btnWidth);
        load.setBackground(menuBkg);
        load.setBorder(menuBorder);
        load.setOnAction(new LoadHandler());

        save = new Button(Constants.SAVE.toUpperCase());
        save.setFont(Font.font("Times New Roman", 16));
        save.setTextFill(Color.WHITE);
        save.setPrefWidth(btnWidth);
        save.setBackground(menuBkg);
        save.setBorder(menuBorder);
        save.setDisable(true);
        save.setOnAction(new SaveHandler());

        Button ranking = new Button(Constants.RANKING.toUpperCase());
        ranking.setFont(Font.font("Times New Roman", 16));
        ranking.setTextFill(Color.WHITE);
        ranking.setPrefWidth(btnWidth);
        ranking.setBackground(menuBkg);
        ranking.setBorder(menuBorder);
        ranking.setOnAction(new RankingHandler());

        Button exit = new Button(Constants.EXIT.toUpperCase());
        exit.setFont(Font.font("Times New Roman", 16));
        exit.setTextFill(Color.WHITE);
        exit.setPrefWidth(btnWidth);
        exit.setBackground(menuBkg);
        exit.setBorder(menuBorder);
        exit.setTooltip(new Tooltip(Constants.EXIT_TOOLTIP));
        exit.setOnAction(new ExitHandler());

        menu.getChildren().addAll(start, toGame, load, save, ranking, exit);
        menu.setAlignment(Pos.CENTER);
        this.setCenter(menu);
    }

    private void performIO(int option) {
        FileChooser chooser = new FileChooser();
        ExtensionFilter ext = new ExtensionFilter(Constants.EXT_DESCRIPTION, ".pt");
        chooser.setSelectedExtensionFilter(ext);
        if (option == LOAD) {
            chooser.setTitle(Constants.LOAD);
            File choice = chooser.showOpenDialog(stage);
        } else if (option == SAVE) {
            chooser.setTitle(Constants.SAVE);
            File choice = chooser.showSaveDialog(stage);
        }
    }

    private boolean verifyRestart() {
        Alert alert = new Alert(AlertType.WARNING, Constants.START_WARNING_MESSAGE,
                ButtonType.YES, ButtonType.NO);
        alert.setTitle(Constants.START_WARNING);
        alert.showAndWait();

        if (alert.getResult() == ButtonType.YES)
            return true;
        return false;
    }

    private class StartHandler implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent event) {
            if (display.isRunning()) {
                if (!verifyRestart())
                    return;
            }

            display.start();
            stage.setScene(displayScene);
            toGame.setDisable(false);
            save.setDisable(false);
        }

    }

    private class ContinueHandler implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent event) {
            stage.setScene(displayScene);
        }

    }

    private class LoadHandler implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent event) {
            if (display.isRunning()) {
                if (!verifyRestart())
                    return;
            }

            performIO(LOAD);
        }

    }

    private class SaveHandler implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent event) {
            performIO(SAVE);
        }

    }

    private class RankingHandler implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent event) {
            stage.setScene(displayScene);
        }

    }

    private class ExitHandler implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent event) {
            performIO(SAVE);
            Platform.exit();
        }

    }

}

public static class MainDisplay extends AnchorPane {

    private boolean running;

    public MainDisplay() {
        running = false;
        tower = new Tower();
        setUp();
    }

    private void setUp() {
        Color blue = new Color(34 / 255, 37 / 255, 47 / 255, 1.0);

        this.setBackground(new Background(new BackgroundFill(blue, null, null)));

        Background bkg = new Background(new BackgroundFill(null, null, null));
        Border menuBorder = new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID,
                new CornerRadii(10), null));

        Button home = new Button(Constants.HOME);
        home.setFont(Font.font("Times New Roman", 10));
        home.setTextFill(Color.WHITE);
        home.setBackground(bkg);
        home.setBorder(menuBorder);
        home.setOnAction(new HomeHandler());
        this.getChildren().add(home);
        AnchorPane.setTopAnchor(home, 10.0);
        AnchorPane.setRightAnchor(home, 10.0);
    }

    public void start() {
        running = true;
    }

    public boolean isRunning() {
        return running;
    }

    private class HomeHandler implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent event) {
            stage.setScene(startScene);
        }

    }

}

}