更改javafx中相同文本的颜色和大小的问题

时间:2018-12-14 19:53:31

标签: java javafx fxml

这是我用于构建文本编辑器的代码。一切正常,但问题是当我更改所选文本的字体大小,并且要更改同一文本的颜色后,它将文本大小更改回默认值。我想知道如何解决此问题。

有人建议定义一个哈希图,在这里我们保存在文本区域中输入的每个字符并保存其样式。但是我无法弄清楚。

sample.FXML

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

<?import java.lang.*?>
<?import javafx.collections.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<?import org.fxmisc.richtext.InlineCssTextArea?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" 
        prefHeight="564.0" prefWidth="790.0" 
        xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
        fx:controller="sample.Controller">
  <center>
    <InlineCssTextArea  fx:id="textArea" BorderPane.alignment="CENTER" 
        onKeyTyped="#handleTextAreaAction">
      <BorderPane.margin>
        <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
      </BorderPane.margin>
      <padding>
        <Insets bottom="1.0" left="1.0" right="1.0" top="1.0" />
      </padding>
    </InlineCssTextArea>
  </center>
  <top>
    <HBox BorderPane.alignment="CENTER">
      <children>
        <Button mnemonicParsing="false" onAction="#UploadButtonClicked" 
            text="Upload">
          <HBox.margin>
            <Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
          </HBox.margin>
        </Button>
        <ComboBox fx:id="familyComboBox" onAction="#familyOnAction" 
            promptText="Font Theme">
          <HBox.margin>
            <Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
          </HBox.margin>
          <items>
            <FXCollections fx:factory="observableArrayList">
              <String fx:value="Arial" />
              <String fx:value="Times new Roman" />
              <String fx:value="Calibri"/>
            </FXCollections>
          </items>
        </ComboBox>
        <ComboBox fx:id="sizeComboBox" layoutX="62.0" layoutY="10.0" 
             onAction="#sizeOnAction" promptText="Font Size">
          <HBox.margin>
            <Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
          </HBox.margin>
          <items>
            <FXCollections fx:factory="observableArrayList">
              <String fx:value="11" />
              <String fx:value="12" />
              <String fx:value="14" />
              <String fx:value="18" />
              <String fx:value="24" />
            </FXCollections>
          </items>
        </ComboBox>
        <ComboBox fx:id="ColorBoxColor" layoutX="180.0" layoutY="12.0" 
            onAction="#colorOnAction" promptText="Font Color">
          <HBox.margin>
            <Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
          </HBox.margin>
          <items>
            <FXCollections fx:factory="observableArrayList">
              <String fx:value="Red" />
              <String fx:value="Black" />
              <String fx:value="Green" />
              <String fx:value="Blue" />
            </FXCollections>
          </items>
        </ComboBox>
        <Button fx:id="bold" layoutX="10.0" layoutY="10.0" 
            mnemonicParsing="false" onAction="#boldOnAction" text="Bold">
          <HBox.margin>
            <Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
          </HBox.margin>
        </Button>
        <Button fx:id="italic" layoutX="487.0" layoutY="10.0" 
             mnemonicParsing="false" onAction="#italicOnAction" text="Italic">
          <HBox.margin>
            <Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
          </HBox.margin>
        </Button>
      </children>
    </HBox>
  </top>
</BorderPane>

Controller.java

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.IndexRange;
import javafx.scene.input.KeyEvent;
import javafx.scene.control.TextArea;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import org.fxmisc.richtext.InlineCssTextArea; 
import java.awt.*;
import java.net.URL;    
import java.util.HashMap;
import java.util.Map;

public class Controller {
    Map<Integer,String> list = new HashMap<>();

    @FXML private ComboBox ColorBoxColor;
    @FXML private InlineCssTextArea textArea ;
    @FXML private ComboBox sizeComboBox;
    @FXML private ComboBox familyComboBox;

    String fontType ;
    int fontSize;

    CharacterStyle style = new CharacterStyle();
    @FXML
    public void handleTextAreaAction(KeyEvent key) {          

    }

    public void italicOnAction(ActionEvent event) {

    }

    public void boldOnAction(ActionEvent event) {
    }
    @FXML
    public void colorOnAction(ActionEvent event) {

        try {
            int last = (textArea.getText().lastIndexOf(textArea.getSelectedText()))+textArea.getSelectedText().length();
            int start = last - textArea.getSelectedText().length();

            if (ColorBoxColor.getValue().equals("Red")) {
                textArea.setStyle(start,last,"-fx-fill: "+style.getColor("red"));
            }
            else if (ColorBoxColor.getValue().equals("Black")){
                textArea.setStyle(start,last,"-fx-fill: "+style.getColor("black"));
            }
            else if (ColorBoxColor.getValue().equals("Green")) {
                textArea.setStyle(start,last,"-fx-fill: " +style.getColor("green"));
            }else{
                textArea.setStyle(start,last,"-fx-fill: "+ style.getColor("blue"));
            }
        }catch (Exception ex) {
            System.out.println("Error");
        }
    }
    @FXML
    public void sizeOnAction(ActionEvent event) {

        try {
            int last = (textArea.getText().lastIndexOf(textArea.getSelectedText()))+textArea.getSelectedText().length();
            int start = last - textArea.getSelectedText().length();

            if (sizeComboBox.getValue().equals("11"))
                fontSize = style.getSize(11);
            else if (sizeComboBox.getValue().equals("12"))
                fontSize = style.getSize(12);
            else if (sizeComboBox.getValue().equals("14"))
                fontSize = style.getSize(14);
            else if (sizeComboBox.getValue().equals("16"))
                fontSize = style.getSize(16);
            else if (sizeComboBox.getValue().equals("18"))
                fontSize = style.getSize(18);
            else
                fontSize = style.getSize(24);

            textArea.setStyle(start,last,"-fx-font-size: " + style.getSize(fontSize));

        } catch (Exception ex) {

            System.out.println("Error");   
        }
    }

    public void familyOnAction(ActionEvent event) {
        try {
            int last = (textArea.getText().lastIndexOf(textArea.getSelectedText()))+textArea.getSelectedText().length();
            int start = last - textArea.getSelectedText().length();

            if (familyComboBox.getValue().equals("Times New Roman")) {
                fontType = style.getFont("Times New Roman");
            } else if(familyComboBox.getValue().equals("Arial")) {
                fontType = style.getFont("Arial");
            }
            else
                fontType = style.getFont(("Calibri"));

            textArea.setStyle(start,last,"-fx-font-family: " +fontType);

        } catch (Exception ex) {
            System.out.println("Error");
        }
    }

    public void UploadButtonClicked(ActionEvent event) {
    }  
}

我想找出我错过了什么,因此当我在特定文本上应用多个样式时,它不会恢复默认设置。

0 个答案:

没有答案