在场景中使用其他类

时间:2017-01-08 17:31:11

标签: java class user-interface javafx scene

伙计们,我是JavaFX的新手,所以我知道这将是愚蠢的(或易于解决),但我在一个类中创建了一个Togglebuttons菜单,我想把它放到Main。

我的LeftMenu类看起来像这样:

package application;

import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;

public class LeftMenu extends BorderPane{

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

        public LeftMenu() {

    ToggleGroup group = new ToggleGroup();


    ToggleButton sr = new ToggleButton("SR");
    sr.setTooltip(new Tooltip("SR"));
    sr.setPrefWidth(40);
    sr.setPrefHeight(40);
    sr.setOnAction(e-> {
        if(sr.isSelected())
        System.out.println(sr.getText()  + " ON");
        else
        System.out.println(sr.getText() + " OFF");
    });


    ToggleButton mh = new ToggleButton("MH");
    mh.setTooltip(new Tooltip("MH"));
    mh.setPrefWidth(40);
    mh.setPrefHeight(40);
    mh.setOnAction(e-> {
        if(mh.isSelected())
        System.out.println(mh.getText()  + " ON");
        else
        System.out.println(mh.getText() + " OFF");
    });

    ToggleButton ssfha = new ToggleButton("SSFHA");
    ssfha.setTooltip(new Tooltip("SSFHA"));
    ssfha.setPrefWidth(40);
    ssfha.setPrefHeight(40);
    ssfha.setOnAction(e-> {
        if(ssfha.isSelected())
        System.out.println(ssfha.getText()  + " ON");
        else
        System.out.println(ssfha.getText() + " OFF");
    });

    sr.setToggleGroup(group);
    mh.setToggleGroup(group);
    ssfha.setToggleGroup(group);

    //Menu
    VBox level = new VBox();
    level.getStyleClass().addAll("vbox");
    level.getChildren().addAll(sr, mh, ssfha);

 }
}

我想把它放在这里 (我没有任何错误,所以我不知道这是什么情况):

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class Main extends Application {

Stage window;

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

@Override
public void start(Stage primaryStage) throws Exception{
    {
        Stage window = primaryStage;
        window.setTitle("Menu Test");
        window.setMaximized(true);

        StackPane root = new StackPane();

        BorderPane lMenu = new LeftMenu();          
        BorderPane rightPane = new BorderPane();

        root.getChildren().addAll(lMenu,rightPane);

        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        window.setScene(scene);
        window.show();

    }
}
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

首先,使用适当的布局窗格,允许您同时查看两个子节点,而不是将一个放在另一个上面的StackPane(隐藏底部节点):

// StackPane root = new StackPane();
HBox root = new HBox();

其次,您似乎无法向自定义类添加任何内容:

import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;

public class LeftMenu extends BorderPane{

    // why on earth is this here? Get rid of it:
    //public static void main(String[] args) {
    //}

    public LeftMenu() {

        ToggleGroup group = new ToggleGroup();


        ToggleButton sr = new ToggleButton("SR");
        sr.setTooltip(new Tooltip("SR"));
        sr.setPrefWidth(40);
        sr.setPrefHeight(40);
        sr.setOnAction(e-> {
            if(sr.isSelected())
            System.out.println(sr.getText()  + " ON");
            else
            System.out.println(sr.getText() + " OFF");
        });


        ToggleButton mh = new ToggleButton("MH");
        mh.setTooltip(new Tooltip("MH"));
        mh.setPrefWidth(40);
        mh.setPrefHeight(40);
        mh.setOnAction(e-> {
            if(mh.isSelected())
            System.out.println(mh.getText()  + " ON");
            else
            System.out.println(mh.getText() + " OFF");
        });

        ToggleButton ssfha = new ToggleButton("SSFHA");
        ssfha.setTooltip(new Tooltip("SSFHA"));
        ssfha.setPrefWidth(40);
        ssfha.setPrefHeight(40);
        ssfha.setOnAction(e-> {
            if(ssfha.isSelected())
            System.out.println(ssfha.getText()  + " ON");
            else
            System.out.println(ssfha.getText() + " OFF");
        });

        sr.setToggleGroup(group);
        mh.setToggleGroup(group);
        ssfha.setToggleGroup(group);

        //Menu
        VBox level = new VBox();
        level.getStyleClass().addAll("vbox");
        level.getChildren().addAll(sr, mh, ssfha);

        // add level to the center of this pane:

        setCenter(level);

    }
}