java在javafx中改变应用程序主题

5lhxktic  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(275)

如果我的问题不好,我很抱歉。我是初学者,请原谅我。
目标:通过按下一个按钮来改变我的简单计算器应用程序的主题。
问题:不知道怎么做:)
细节:
基本上,我已经创建了两个单独的css文件,分别命名为“light.css”和“dark.css”。我把这些文件放在一个叫做“样式表”的文件夹里。它们每一个都对应于一个特定的主题(光明代表光明主题,黑暗代表黑暗主题)。我决定使用一种方法来控制我的应用程序的主题,这种方法可以在用户每次按下与此方法相关联的按钮时更改应用程序的主题。我在scene builder中创建了一个按钮,并给了它一个名为“changetheme”的方法。
我的想法如下(如我所说,我是初学者,所以请原谅我,如果这是一个愚蠢的解决办法):
1-创建一个名为“changetheme”的方法,每次用户按下与此方法关联的按钮时都会调用该方法。
2-该方法将在名为“controller.java”的控制器类中调用。
3-该方法将调用另一个名为“setscene(new scene(root))”的方法。
(根目录(类型为“parent”)使用方法“getstylesheets().add”(“path/to/css”)”获取所需的样式表)
4-在调用方法时更改“getstylesheets().add”(“path/to/css”)”的路径,希望它能更改它们的名称。
最后:它不起作用:d
(我犯了很多错误,认为我的方法是完全错误的,所以我决定在这里问我的问题,并完全改变我的解决方案)。
结论:我的问题有解决办法吗(如果你能帮助我,我将非常感激。)
顺便说一句,我确实试着寻找其他的解决办法。其中一个只使用css,按下按钮后改变元素的颜色。但我不喜欢。
以下是有关我的代码的所有信息:
主类名称:“main.java”
控制器类名称:“controller.java”
fxml文件名:“fxml.fxml”(位于名为“resources”的文件夹中)
css文件名:“light.css”和“dark.css”(位于名为“stylesheets”的文件夹中)
代码如下:
主要内容:

package calculator;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Main extends Application
{
    public static void main(String[] args)
    {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("resources/FXML.fxml"));
        root.getStylesheets().add("/calculator/stylesheets/Dark.css");

        stage.getIcons().add(new Image("/calculator/icons/Calculator.png"));
        stage.setTitle("Calculator");
        stage.setScene(new Scene(root));
        stage.show();
    }
}

控制器:

package calculator;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import java.util.Arrays;

public class Controller
{
    private int numbers[] = new int[30];
    private String operands[] = new String[15];
    private String temporary = new String("");

    public int finalAnswer = 0;

    private Button clickedButton = null;

    private int check = 0;
    private int n = 0; // Number
    private int o = 0; // Operand

    @FXML
    private TextField textField;

    @FXML
    private void OnNumberClick(MouseEvent event)
    {
        GetButton(event);
        DisplayNumberButton();
        ConcatinateToTemporary();
    }

    @FXML
    private void OnOperatorClick(MouseEvent event)
    {
        GetButton(event);
        DisplayOperandButton();
        AssignNumber();
        AssignOperand();
        EmptyTemporary();
    }

    @FXML
    private void OnEqualClick(MouseEvent event)
    {
        CalculateAnswer(event);
    }

    @FXML
    private void OnACClick(MouseEvent event)
    {
        ClearArrays();
        EmptyTemporary();
        ClearIntegers();
        ClearTextField();
    }

    // Defined Methods

    private void GetButton(MouseEvent event)
    {
        clickedButton = (Button) event.getSource();
    }

    private void DisplayNumberButton()
    {
        textField.setText(textField.getText() + clickedButton.getText());
    }

    private void DisplayOperandButton()
    {
        textField.setText(textField.getText() + " " + clickedButton.getText() + " ");
    }

    private void DisplayException(Exception exception)
    {
        textField.setText("Error! Please retry");
    }

    private void DisplayZeroException(Exception exception)
    {
        textField.setText("Cannot divide by zero!");
    }

    private void DisplayNumberException(Exception exception)
    {
        textField.setText("Please insert a number before selecting operation!");
    }

    private void ConcatinateToTemporary()
    {
        temporary += clickedButton.getText();
    }

    private void AssignNumber()
    {
        try
        {
            numbers[n] = Integer.valueOf(temporary);
            if(n < 30)
                n++;
        }

        catch (Exception numberException)
        {
            DisplayNumberException(numberException);
        }
    }

    private void EmptyTemporary()
    {
        temporary = "";
    }

    private void AssignOperand()
    {
        operands[o] = clickedButton.getText();
        if(o < 15)
            o++;
    }

    private void ClearArrays()
    {
         Arrays.fill(numbers,0);
         Arrays.fill(operands,null);
    }

    private void ClearIntegers()
    {
        finalAnswer = 0;
        check = 0;
        n = 0;
        o = 0;
    }

    private void ClearTextField()
    {
        textField.setText("");
    }

    private void EqualSignText()
    {
        textField.setText("=" + Integer.toString(finalAnswer));
    }

    private void IncrementIndexes(int number)
    {
        number += 2;
        if(check <= 13)
            check++;
    }

    private void CalculateAnswer(MouseEvent event)
    {
        try
        {
            AssignNumber();
            for(int index=0; index <= operands.length; index++)
            {
                int number = 0;
                if(operands[check] != null)
                {
                    switch(operands[check])
                    {
                        case "x":
                            finalAnswer += numbers[number] * numbers[(number + 1)];
                            break;
                        case "/":
                            finalAnswer += numbers[number] / numbers[(number + 1)];
                            break;
                        case "+":
                            finalAnswer += numbers[number] + numbers[(number + 1)];
                            break;
                        case "-":
                            finalAnswer += numbers[number] - numbers[(number + 1)];
                            break;
                    }
                }
                else
                    break;
                IncrementIndexes(number);
            }
            GetButton(event);
            EqualSignText();
        }
        catch (java.lang.ArithmeticException zeroException)
        {
            DisplayZeroException(zeroException);
        }
        catch (Exception exception)
        {
            DisplayException(exception);
        }

    }
}

fxml地址:

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

<?import javafx.geometry.Insets?>
<?import javafx.geometry.Point3D?>
<?import javafx.geometry.Rectangle2D?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="calculator.Controller">
   <top>
      <Button id="theme_button" maxWidth="1.7976931348623157E308" mnemonicParsing="false" prefHeight="35.0" styleClass="number_button" BorderPane.alignment="CENTER">
         <graphic>
            <ImageView fitHeight="40.0" fitWidth="40.0" pickOnBounds="true" preserveRatio="true">
               <viewport>
                  <Rectangle2D />
               </viewport>
               <image>
                  <Image url="@../icons/Theme.png" />
               </image>
            </ImageView>
         </graphic></Button>
   </top>
   <bottom>
      <GridPane gridLinesVisible="true" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="375.0" BorderPane.alignment="CENTER">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnEqualClick" styleClass="operand_button" text="=" textAlignment="JUSTIFY" GridPane.columnIndex="2" GridPane.rowIndex="3" />
            <Button layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnNumberClick" styleClass="number_button" text="0" textAlignment="JUSTIFY" GridPane.columnIndex="1" GridPane.rowIndex="3" />
            <Button id="AC_operand" layoutX="10.0" layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnACClick" styleClass="operand_button" text="AC" textAlignment="JUSTIFY" GridPane.rowIndex="3" />
            <Button layoutX="10.0" layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnOperatorClick" styleClass="operand_button" text="+" textAlignment="JUSTIFY" GridPane.columnIndex="3" GridPane.rowIndex="2" />
            <Button layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnNumberClick" styleClass="number_button" text="9" textAlignment="JUSTIFY" GridPane.columnIndex="2" GridPane.rowIndex="2" />
            <Button layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnNumberClick" styleClass="number_button" text="8" textAlignment="JUSTIFY" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <Button layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnNumberClick" styleClass="number_button" text="7" textAlignment="JUSTIFY" GridPane.rowIndex="2" />
            <Button layoutX="10.0" layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnOperatorClick" styleClass="operand_button" text="/" textAlignment="JUSTIFY" GridPane.columnIndex="3" GridPane.rowIndex="1" />
            <Button layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnNumberClick" styleClass="number_button" text="6" textAlignment="JUSTIFY" GridPane.columnIndex="2" GridPane.rowIndex="1" />
            <Button layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnNumberClick" styleClass="number_button" text="5" textAlignment="JUSTIFY" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <Button layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnNumberClick" styleClass="number_button" text="4" textAlignment="JUSTIFY" GridPane.rowIndex="1" />
            <Button layoutX="10.0" layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnOperatorClick" styleClass="operand_button" text="x" textAlignment="JUSTIFY" GridPane.columnIndex="3" />
            <Button layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnNumberClick" styleClass="number_button" text="3" textAlignment="JUSTIFY" GridPane.columnIndex="2" />
            <Button layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnNumberClick" styleClass="number_button" text="2" textAlignment="JUSTIFY" GridPane.columnIndex="1" />
            <Button layoutY="328.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnNumberClick" styleClass="number_button" text="1" textAlignment="JUSTIFY" />
            <Button id="minus_operand" layoutX="10.0" layoutY="44.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onMouseClicked="#OnOperatorClick" styleClass="operand_button" text="-" textAlignment="JUSTIFY" GridPane.columnIndex="3" GridPane.rowIndex="3" />
         </children>
         <opaqueInsets>
            <Insets />
         </opaqueInsets>
         <padding>
            <Insets left="-1.0" right="-1.0" />
         </padding>
      </GridPane>
   </bottom>
   <center>
      <TextField fx:id="textField" editable="false" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" BorderPane.alignment="CENTER">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
         <BorderPane.margin>
            <Insets />
         </BorderPane.margin>
         <rotationAxis>
            <Point3D x="-2.0" z="1.0" />
         </rotationAxis>
      </TextField>
   </center>
</BorderPane>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题