文本区域 javaFx 颜色
Textarea javaFx Color
我正在尝试开发一个看起来像终端控制台的应用程序,我为此使用了 TextArea,我希望拥有黑色背景和绿色文本,
我想在不使用任何ccs模板的情况下做到这一点
我知道我的问题在这里看起来像是重复的:
javafx textarea background color not css
或
JavaFX CSS styling of TextArea does not work
但在阅读了这些内容并尝试了他们的建议后,我发现没有运气来解决我的问题
到目前为止我尝试了什么(未成功):
在 FXML 中:
<TextArea
fx:id="terminalTextArea"
layoutX="14.0"
layoutY="85.0"
prefHeight="64.0"
prefWidth="402.0"
style="-fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; -fx-background-color:#000000;"
text="Terminal"
AnchorPane.leftAnchor="10.0"
AnchorPane.rightAnchor="10.0">
<font>
<Font name="System Bold" size="14.0" />
</font>
</TextArea>
并在源代码中:
@Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println("init here");
terminalTextArea.setStyle("-fx-text-fill: black;");
}
我唯一得到的是边框颜色,如下图所示:
推荐的方法是使用外部 CSS 文件,如您链接的示例中所示。
如果出于某种原因您不想这样做,请尝试
style="-fx-control-inner-background:#000000; -fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; "
在您的 FXML 文件中,或等效地
terminalTextArea.setStyle("-fx-control-inner-background:#000000; -fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; ");
在控制器的 initialize()
方法中。
SSCCE:
StyledTextArea.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.text.Font?>
<HBox xmlns:fx="http://javafx.com/fxml/1" >
<padding>
<Insets top="12" left="12" right="12" bottom="12"/>
</padding>
<TextArea
prefHeight="64.0"
prefWidth="402.0"
style="-fx-control-inner-background:#000000; -fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; "
text="Terminal">
<font>
<Font name="System Bold" size="14.0" />
</font>
</TextArea>
</HBox>
和一个测试class:
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class StyledTextArea extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
primaryStage.setScene(new Scene(
FXMLLoader.load(getClass().getResource("StyledTextArea.fxml"))
));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
很简单,只需在您想要的元素中输入一个 id 名称,例如:
AnchorPane id="AnchorPane" prefHeight="180.0" prefWidth="430.0"
或在 Scene Builder 中标题 JavaFX CSS 正下方的属性中,在您的 CSS 文件中写入此元素的名称和样式。
如果您尝试使用 CSS 设置 textArea 的背景,您应该具有 CSS .content 的这个属性。视口 //additional .scroll-pane
代码:
#tContent{
-fx-pref-width: 180;
-fx-pref-height: 110;
}
.content {
-fx-background-color: transparent;
}
.viewport{
-fx-background-color: transparent;
}
我正在尝试开发一个看起来像终端控制台的应用程序,我为此使用了 TextArea,我希望拥有黑色背景和绿色文本,
我想在不使用任何ccs模板的情况下做到这一点
我知道我的问题在这里看起来像是重复的:
javafx textarea background color not css
或
JavaFX CSS styling of TextArea does not work
但在阅读了这些内容并尝试了他们的建议后,我发现没有运气来解决我的问题
到目前为止我尝试了什么(未成功):
在 FXML 中:
<TextArea
fx:id="terminalTextArea"
layoutX="14.0"
layoutY="85.0"
prefHeight="64.0"
prefWidth="402.0"
style="-fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; -fx-background-color:#000000;"
text="Terminal"
AnchorPane.leftAnchor="10.0"
AnchorPane.rightAnchor="10.0">
<font>
<Font name="System Bold" size="14.0" />
</font>
</TextArea>
并在源代码中:
@Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println("init here");
terminalTextArea.setStyle("-fx-text-fill: black;");
}
我唯一得到的是边框颜色,如下图所示:
推荐的方法是使用外部 CSS 文件,如您链接的示例中所示。
如果出于某种原因您不想这样做,请尝试
style="-fx-control-inner-background:#000000; -fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; "
在您的 FXML 文件中,或等效地
terminalTextArea.setStyle("-fx-control-inner-background:#000000; -fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; ");
在控制器的 initialize()
方法中。
SSCCE:
StyledTextArea.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.HBox?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.text.Font?>
<HBox xmlns:fx="http://javafx.com/fxml/1" >
<padding>
<Insets top="12" left="12" right="12" bottom="12"/>
</padding>
<TextArea
prefHeight="64.0"
prefWidth="402.0"
style="-fx-control-inner-background:#000000; -fx-font-family: Consolas; -fx-highlight-fill: #00ff00; -fx-highlight-text-fill: #000000; -fx-text-fill: #00ff00; "
text="Terminal">
<font>
<Font name="System Bold" size="14.0" />
</font>
</TextArea>
</HBox>
和一个测试class:
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class StyledTextArea extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
primaryStage.setScene(new Scene(
FXMLLoader.load(getClass().getResource("StyledTextArea.fxml"))
));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
很简单,只需在您想要的元素中输入一个 id 名称,例如:
AnchorPane id="AnchorPane" prefHeight="180.0" prefWidth="430.0"
或在 Scene Builder 中标题 JavaFX CSS 正下方的属性中,在您的 CSS 文件中写入此元素的名称和样式。
如果您尝试使用 CSS 设置 textArea 的背景,您应该具有 CSS .content 的这个属性。视口 //additional .scroll-pane
代码:
#tContent{
-fx-pref-width: 180;
-fx-pref-height: 110;
}
.content {
-fx-background-color: transparent;
}
.viewport{
-fx-background-color: transparent;
}