簡體   English   中英

使用CSS樣式化Javafx

[英]Styling Javafx with CSS

我一直在使用javafx,並已開始着手設計表單的樣式。 我已經創建了一個程序,該程序將為您的文本創建弱加密,但還沒有完全完成。

這是Java代碼-

public class encryption extends Application {

    private static StringBuffer password;
    private static int key;

    public void setValue(String value) {
        password = new StringBuffer(value);
    }

    public void encryption() {      
        RNG();

        for(int i = 0; i < password.length(); i++) {
            int cValue = (int)password.charAt(i);

            int nValue = cValue ^ key;

            password.setCharAt(i, (char)nValue);
        }   
        System.out.println(password);
        System.out.println("");
    }

    public void RNG() {
         Random rand = new Random();

            // nextInt is normally exclusive of the top value,
            // so add 1 to make it inclusive
            key = rand.nextInt(((222 - 8) + 1) + 8) ^ 26;

    }

    public void decryption() {
        for(int i = 0; i < password.length(); i++) {
            int nValue = key ^ password.charAt(i);

            password.setCharAt(i, (char)nValue);

        }
        System.out.println(password);
    }

    public void start(Stage arg0) throws Exception {

        StringBuffer eLabel = new StringBuffer();

        Scene scene;

        VBox container = new VBox();
        HBox root = new HBox();
        HBox rootA = new HBox();
        HBox rootB = new HBox();

        Label instructions;
        Button submit;
        Button reveal;
        PasswordField message;
        Label encryption;
        Label typed;

        instructions = new Label("Submit Label that you want to encrypt");  

        root.getChildren().add(instructions);

        message = new PasswordField();
        message.setOnKeyPressed(new EventHandler<KeyEvent>() {

            public void handle(KeyEvent e) {
                if(e.getCode() == KeyCode.ENTER) {
                    System.out.println(message.getText());
                    setValue(message.getText());
                    encryption();
                    decryption();
                }
            }

        });
        submit = new Button("Click to Submit");

        typed = new Label();
        typed.setId("value");

        rootA.getChildren().add(message);
        rootA.getChildren().add(submit);

        reveal = new Button("Show Text");
        reveal.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent arg0) {
                typed.setText(message.getText());
            }

        });

        rootB.getChildren().add(reveal);
        rootB.getChildren().add(typed);

        container.getChildren().add(root);
        container.getChildren().add(rootA);
        container.getChildren().add(rootB);

        scene = new Scene(container, 1000, 500);

        String css = "encryption.css";
        scene.getStylesheets().add(css);

        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setTitle("Javafx Encryption");

        stage.show();
    }


    public static void main(String[] args) {

        launch();
    }

}

這是CSS-

.root {
    -fx-text-fill: rgb(49, 89, 23);
    -fx-background-color: #202020;
}

.button {
    -fx-background-color: red;
    -fx-text-fill: black;
}

.vbox {
    -fx-background-color: white;
    -fx-spacing: 10;
    -fx-height: 100%;
}

.hbox {
    -fx-background-color: #502576;
    -fx-spacing: 10;
}

#value {
    -fx-font-size: 12px;    
}

.label {
    -fx-font-size: 20px;
    -fx-text-fill: white;
}

hbox不會與vbox一起應用已應用的更改。

為什么hbox和vbox無法從CSS獲得更改?

在大多數情況下,只有Control子類具有默認的樣式類。 此外,場景的根獲取樣式類root

因此,您需要手動添加vboxhbox樣式類:

    VBox container = new VBox();
    container.getStyleClass().add("vbox");
    HBox root = new HBox();
    root.getStyleClass().add("hbox");
    HBox rootA = new HBox();
    rootA.getStyleClass().add("hbox");
    HBox rootB = new HBox();
    rootB.getStyleClass().add("hbox");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM