0

I'm working on this example which is not working properly:

public class test extends Application
{

    private void init(Stage primaryStage)
    {

        Group root = new Group();
        primaryStage.setScene(new Scene(root));

        String pillButtonCss = DX57DC.class.getResource("PillButton.css").toExternalForm();

        // create 3 toggle buttons and a toogle group for them
        ToggleButton tb1 = new ToggleButton("Left Button");
        tb1.setId("pill-left");
        ToggleButton tb2 = new ToggleButton("Center Button");
        tb2.setId("pill-center");
        ToggleButton tb3 = new ToggleButton("Right Button");
        tb3.setId("pill-right");

        final ToggleGroup group = new ToggleGroup();
        tb1.setToggleGroup(group);
        tb2.setToggleGroup(group);
        tb3.setToggleGroup(group);
        // select the first button to start with
        group.selectToggle(tb1);

        //////////////////////////////////////////

        final VBox vbox = new VBox();

        final Rectangle rect1 = new Rectangle(300, 300);
        rect1.setFill(Color.ALICEBLUE);
        final Rectangle rect2 = new Rectangle(300, 300);
        rect2.setFill(Color.AQUA);
        final Rectangle rect3 = new Rectangle(300, 300);
        rect3.setFill(Color.AZURE);

        tb1.setUserData(rect1);
        tb2.setUserData(rect2);
        tb3.setUserData(rect3);

        group.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
        {
            @Override
            public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle)
            {
                if (new_toggle == null)
                {
                    //rect.setFill(Color.WHITE);
                }
                else
                {
                    vbox.getChildren().addAll((Node[]) group.getSelectedToggle().getUserData());
                    //rect.setFill((Color) group.getSelectedToggle().getUserData());
                }
            }
        });


        ///////////////////////////////////////////


        HBox hBox = new HBox();
        hBox.getChildren().addAll(tb1, tb2, tb3);
        hBox.setPadding(new Insets(20, 20, 260, 20));
        hBox.getStylesheets().add(pillButtonCss);



        vbox.getChildren().add(hBox);
        //vbox.getChildren().add(rect);

        root.getChildren().add(vbox);
    }

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        init(primaryStage);
        primaryStage.show();
    }

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

I want to create several Rectangles(or object in which or object) in which I want to store data. I want to switch the Rectangles(objects) which are displayed in front of the user using the buttons. The example which I implemented is not working properly. Can you tell me what is the proper way to implement this?

4

1 回答 1

1

您可以创建一个带有 Rectangle 的 Stackpane 和一个带有 Text 的标签(如果那是您要存储的数据)。或者,您也可以将任何窗格的背景设置为具有彩色矩形。

然后将此窗格作为用户数据添加到相应的按钮,然后在切换时将按钮用户数据添加到您的 VBox。

final StackPane rect1pane = new StackPane();
final Rectangle rect1 = new Rectangle(300, 300);
rect1pane.getChildren().add(rect1);
rect1pane.getChildren().add(new Label("Some text"));
tb1.setUserData(rect1pane);

切换属性监听器:

...
else{
//Delete rectangles added before ( or check if this one isnt already dispayed)
if(group.getSelectedToggle().getUserData() instanceof Node)
     vbox.getChildren().add((Node)group.getSelectedToggle().getUserData());
}

如果您只想让示例代码正常工作,请更改:

vbox.getChildren().addAll((Node[]) group.getSelectedToggle().getUserData());

vbox.getChildren().addAll((Node) group.getSelectedToggle().getUserData());

因为您只是添加了 Selected ToggleButton 的 Rectangle 它只是一个而不是一个数组。

单击以查看矩形后使窗口变大(260px 底部填充没有帮助,因为即使 hbox 下面的空间是空的,它仍然是 hbox 的一部分并且不能被添加的矩形使用)或者只是移动

 group.selectToggle(tb1);

到你的 init 方法的最后一行;)

于 2013-07-15T05:57:11.583 回答