在Java中,有多种方法可以实现界面的跳转。这里,我将向您展示如何使用Swing和JavaFX两种不同的方法来实现界面跳转。
在Swing中,您可以使用CardLayout来实现界面跳转。以下是一个简单的示例:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingNavigationExample {
    private JFrame frame;
    private JPanel mainPanel;
    private JButton button1;
    private JButton button2;
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                SwingNavigationExample window = new SwingNavigationExample();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    public SwingNavigationExample() {
        initialize();
    }
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));
        mainPanel = new JPanel(new CardLayout());
        frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
        button1 = new JButton("Go to Page 1");
        frame.getContentPane().add(button1, BorderLayout.NORTH);
        button2 = new JButton("Go to Page 2");
        frame.getContentPane().add(button2, BorderLayout.SOUTH);
        button1.addActionListener(e -> mainPanel.show(mainPanel, "Page1"));
        button2.addActionListener(e -> mainPanel.show(mainPanel, "Page2"));
    }
}
在这个示例中,我们创建了一个包含两个按钮的简单Swing应用程序。当用户点击这些按钮时,将显示不同的页面。这是通过使用CardLayout实现的,它允许我们在同一个位置上显示多个组件。
在JavaFX中,您可以使用Scene和Stage来实现界面跳转。以下是一个简单的示例:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class JavaFXNavigationExample extends Application {
    private Stage primaryStage;
    private Scene scene1, scene2;
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        // Page 1
        Label label1 = new Label("This is Page 1");
        Button button1 = new Button("Go to Page 2");
        button1.setOnAction(e -> primaryStage.setScene(scene2));
        StackPane root1 = new StackPane();
        root1.getChildren().addAll(label1, button1);
        scene1 = new Scene(root1, 300, 250);
        // Page 2
        Label label2 = new Label("This is Page 2");
        Button button2 = new Button("Go to Page 1");
        button2.setOnAction(e -> primaryStage.setScene(scene1));
        StackPane root2 = new StackPane();
        root2.getChildren().addAll(label2, button2);
        scene2 = new Scene(root2, 300, 250);
        primaryStage.setTitle("JavaFX Navigation Example");
        primaryStage.setScene(scene1);
        primaryStage.show();
    }
}
在这个示例中,我们创建了一个包含两个页面的简单JavaFX应用程序。每个页面都有一个标签和一个按钮,当用户点击按钮时,将显示另一个页面。这是通过使用Scene和Stage实现的,它们允许我们在同一个窗口中显示不同的场景。