2015-04-29 34 views
1

我是JavaFX的新手。我想在TextArea中的下图中做类似的事情。我认为这可以通过使用标签并设置它的背景颜色来完成。但是如何?在textArea中设置每个字符的背景颜色

image

+0

未与'TextArea'。你需要它是可编辑的吗? –

+0

@James_D嗨....感谢您的答复....对不起,我没有得到你..我有字符串,我想显示在我的容器内的上述格式字符串我现在正在使用文本区域举行字符串.....我们可以打印标签上的字符串字符,并把标签系列内的容器是有可能的......等待 –

+0

我只是说,你不能用'TextArea'做到这一点,通常用于编辑文本。如果你不需要它可编辑的话,那么你可以用'Label'作为'HBox',因为Uluk已经显示出你的拟南芥序列。 –

回答

1

它可以通过将Label到一些布局容器来完成,说HBox

private final Random random = new Random(); 

private final Color[] colors = 
{ 
    Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW 
}; 


@Override 
public void start(final Stage primaryStage) 
{ 
    HBox hbox = new HBox(); 
    String str = "my-string-val"; 
    for (String s : str.split("")) 
    { 
     Label l = new Label(s); 
     l.setBorder(new Border(new BorderStroke(Color.BLACK, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT))); 
     l.setBackground(new Background(new BackgroundFill(colors[random.nextInt(colors.length)], CornerRadii.EMPTY, Insets.EMPTY))); 
     l.setPrefWidth(20); 
     l.setAlignment(Pos.CENTER); 
     l.setFont(font("Arial", FontWeight.BOLD, 16)); 
     hbox.getChildren().add(l); 
    } 

    final Scene scene = new Scene(hbox, 800, 600); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 

} 
+0

嘿男人它的工作谢谢....非常 –