JavaFX弹出对话框,选择后再进行其他操作

public class Main extends Application
{
	public static void main(String[] args) throws Exception
	{
		Application.launch(args);
	}
	
	@Override
	public void start(final Stage primaryStage) throws Exception 
	{
		Pane pane = new Pane();
		
		ButtonPane bp = new ButtonPane(pane, "进入游戏", 80, 40);
		bp.setLayoutXY(260, 200);
		bp.addToPane();
		bp.button.setOnAction(new EventHandler()
		{
			@Override
			public void handle(ActionEvent event)
			{
				NewStage stage = new NewStage(primaryStage);
				stage.show();
			}
		});
		
		// 背景颜色
		pane.setBackground(new Background(new BackgroundFill(Color.rgb(200, 200, 200), null, null)));
		
		// ---------------------------窗口----------------------------
		Scene scene = new Scene(pane, 600, 500);
		primaryStage.setTitle("Test");
		primaryStage.setScene(scene);
		primaryStage.show();
		primaryStage.setAlwaysOnTop(true);
		
		// 右上角点击关闭时, 结束子线程
		primaryStage.setOnCloseRequest(new EventHandler() 
		{
		    public void handle(WindowEvent event) 
		    {
		        System.exit(0);
		    }
		});
		
		// 两次ESC退出程序
		primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler() 
		{
			int count = 0;
			
			@Override
			public void handle(KeyEvent event)
			{
				if (event.getCode() == KeyCode.ESCAPE)
				{
					++count;
					if (count == 2)
					{
						System.exit(0);
					}
				}
				else
				{
					count = 0;
				}
			}
		});
		
		primaryStage.setResizable(false);
		// -----------------------------------------------------------
	}
}


class NewStage
{
	public Stage stage; // 窗口
	public Scene scene; // 场景图
	public Pane pane; // 面板
	
	public ButtonPane individual;
	public ButtonPane network;
	
	public Stage primaryStage;
	
	public NewStage(Stage primaryStage)
	{
		this.primaryStage = primaryStage;
	}
	
	public void show()
	{
		pane = new Pane();
		pane.setBackground(new Background(new BackgroundFill(Color.rgb(200, 200, 200), null, null)));
		
		stage = new Stage();	
		stage.initOwner(primaryStage);
		stage.initModality(Modality.WINDOW_MODAL);
		
		stage.setWidth(400);
		stage.setHeight(300);
		stage.setTitle("选择游戏模式");
		
		scene = new Scene(pane, 600, 500);
		stage.setScene(scene);
		
		individual = new ButtonPane(pane, "单人", 80, 40);
		individual.setLayoutXY(100, 100);
		individual.addToPane();
		individual.button.setOnAction(new EventHandler()
		{
			
			@Override
			public void handle(ActionEvent event)
			{
				stage.close();
				primaryStage.setTitle("单人游戏");
			}
		});
		
		network = new ButtonPane(pane, "网络", 80, 40);
		network.setLayoutXY(220, 100);
		network.addToPane();
		network.button.setOnAction(new EventHandler()
		{
			
			@Override
			public void handle(ActionEvent event)
			{
				stage.close();
				primaryStage.setTitle("网络游戏");
			}
		});
		
		stage.show();
	}
}

JavaFX弹出对话框,选择后再进行其他操作_第1张图片

NewStage stage = new NewStage(primaryStage);
stage.show();

如果点击按钮,就会运行这两行代码:

JavaFX弹出对话框,选择后再进行其他操作_第2张图片

 动图效果:

JavaFX弹出对话框,选择后再进行其他操作_第3张图片

你可能感兴趣的:(JavaFX)