用SceneBuilder编写程序时如何切换一个AnchorPane

如图,当点击按钮时希望把嵌套的AnchorPane换成另一个已经写好的AnchorPane。请问如何实现。急,非常感谢。

 

 

下面分别是fxml文件,control类和主函数类原码。。。求求了

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane fx:id="base_pane" prefHeight="577.0" prefWidth="280.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="launchTools.OrderingController">
   <children>
      <AnchorPane fx:id="home_pane" prefHeight="515.0" prefWidth="332.0">
         <children>
            <ImageView fx:id="homeimage" fitHeight="203.0" fitWidth="319.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@../NoFoodNow.png" />
               </image>
            </ImageView>
         </children>
      </AnchorPane>
      <HBox fx:id="optionHBox" layoutY="518.0" prefHeight="59.0" prefWidth="300.0" style="-fx-background-color: #66ccff;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="518.0">
         <children>
            <Button fx:id="home_button" mnemonicParsing="false" onMouseClicked="#click_Home_Button" prefHeight="59.0" prefWidth="60.0">
               <graphic>
                  <ImageView fx:id="home_view" fitHeight="46.0" fitWidth="41.0">
                     <image>
                        <Image url="@../images/home_clicked.jpg" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
            <Button fx:id="menu_button" mnemonicParsing="false" onAction="#click_Menu_Button" prefHeight="59.0" prefWidth="60.0">
               <graphic>
                  <ImageView fx:id="menu_view" fitHeight="44.0" fitWidth="45.0">
                     <image>
                        <Image url="@../images/Menu_unclicked.jpg" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
            <Button id="home_button" fx:id="shop_button" mnemonicParsing="false" onMouseClicked="#click_Shop_Button" prefHeight="61.0" prefWidth="65.0" wrapText="true">
               <opaqueInsets>
                  <Insets />
               </opaqueInsets>
               <graphic>
                  <ImageView fx:id="shop_view" fitHeight="60.0" fitWidth="45.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../images/shop_unclicked.jpg" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
            <Button fx:id="order_button" alignment="CENTER" mnemonicParsing="false" onMouseClicked="#click_Order_Button" prefHeight="59.0" prefWidth="60.0">
               <graphic>
                  <ImageView fx:id="order_view" fitHeight="60.0" fitWidth="45.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../images/order_unclicked.jpg" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
            <Button fx:id="mine_button" mnemonicParsing="false" onMouseClicked="#click_Mine_Button" prefHeight="59.0" prefWidth="60.0">
               <graphic>
                  <ImageView fx:id="mine_view" fitHeight="60.0" fitWidth="45.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@../images/mine_unclicked.jpg" />
                     </image>
                  </ImageView>
               </graphic>
            </Button>
         </children>
      </HBox>
   </children>
</AnchorPane>
package launchTools;


import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;

import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class OrderingController{
	private Image home_clicked = new Image("home_clicked.jpg");
	private Image home_unclicked = new Image("home_unclicked.jpg");
	private Image menu_clicked = new Image("menu_clicked.jpg");
	private Image menu_unclicked = new Image("menu_unclicked.jpg");
	private Image shop_clicked = new Image("shop_clicked.jpg");
	private Image shop_unclicked = new Image("shop_unclicked.jpg");
	private Image order_clicked = new Image("order_clicked.jpg");
	private Image order_unclicked = new Image("order_unclicked.jpg");
	private Image mine_clicked = new Image("mine_clicked.jpg");
	private Image mine_unclicked = new Image("mine_unclicked.jpg");
	
	@FXML
	private AnchorPane base_pane;
	@FXML
	private AnchorPane home_pane;
	@FXML
	private AnchorPane mine_pane;
	@FXML
	private HBox optionHBox;
	@FXML
	private ImageView homeimage;
	@FXML
	private Button home_button;
	@FXML
	private ImageView home_view;
	@FXML
	private Button menu_button;
	@FXML
	private ImageView menu_view;
	@FXML
	private Button shop_button;
	@FXML
	private ImageView shop_view;
	@FXML
	private Button order_button;
	@FXML
	private ImageView order_view;
	@FXML
	private Button mine_button;
	@FXML
	private ImageView mine_view;
	
	public void click_Home_Button() {
		
		home_view.setImage(home_clicked);
		menu_view.setImage(menu_unclicked);
		shop_view.setImage(shop_unclicked);
		order_view.setImage(order_unclicked);
		mine_view.setImage(mine_unclicked);
	
	}
	public void click_Menu_Button() {
		
		home_view.setImage(home_unclicked);
		menu_view.setImage(menu_clicked);
		shop_view.setImage(shop_unclicked);
		order_view.setImage(order_unclicked);
		mine_view.setImage(mine_unclicked);
	
	}

	public void click_Shop_Button() {
		
		home_view.setImage(home_unclicked);
		menu_view.setImage(menu_unclicked);
		shop_view.setImage(shop_clicked);
		order_view.setImage(order_unclicked);
		mine_view.setImage(mine_unclicked);
	
	}
	public void click_Order_Button() {
		
		home_view.setImage(home_unclicked);
		menu_view.setImage(menu_unclicked);
		shop_view.setImage(shop_unclicked);
		order_view.setImage(order_clicked);
		mine_view.setImage(mine_unclicked);
	
	}
	public void click_Mine_Button() {
		
		home_view.setImage(home_unclicked);
		menu_view.setImage(menu_unclicked);
		shop_view.setImage(shop_unclicked);
		order_view.setImage(order_unclicked);
		mine_view.setImage(mine_clicked);
	
	}
}
/**
 * 
 */
package launchTools;
import javafx.application.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/**
 * @author Lenovo
 *
 */
public class Launch extends Application{

	/**
	 * @param args
	 */
	@Override
	public void start(Stage primaryStage) {
		try {
			Parent root = FXMLLoader.load(getClass().getResource("Ordering.fxml"));
			Scene scene = new Scene(root, 309.0, 577.0);
			scene.getStylesheets().add(getClass().
					getResource("launchTools.css").toExternalForm());
			
			primaryStage.setScene(scene);
			primaryStage.setResizable(false);
			primaryStage.setTitle("Ordering");
			primaryStage.show();
		}catch(Exception e) {
			e.printStackTrace();
		}
//		initiator.InitialInterface.setInterface();
		//primaryStage.show();
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
			launch(args);
	}

}

 

感谢大神!

 

你看看有没有触发事件。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632